Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix audited on crash when run_conditional_check condition is true #697

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

malvads
Copy link

@malvads malvads commented Jan 31, 2024

The function run_conditional_check crash when condition is set to true, as it tries to convert true to symbol

      def run_conditional_check(condition, matching: true)
        return true if condition.blank?
        return condition.call(self) == matching if condition.respond_to?(:call)
        return send(condition) == matching if respond_to?(condition.to_sym, true
 #<NoMethodError: undefined method `to_sym' for true:TrueClass
Did you mean?  to_s>
E, [2024-01-31T15:43:09.682642 #2655809] ERROR -- : /usr/lib/rvm/gems/ruby-2.7.5@web/gems/audited-5.4.2/lib/audited/auditor.rb:412:in `run_conditional_check'
/usr/lib/rvm/gems/ruby-2.7.5@web/gems/audited-5.4.2/lib/audited/auditor.rb:403:in `auditing_enabled'
/usr/lib/rvm/gems/ruby-2.7.5@web/gems/audited-5.4.2/lib/audited/auditor.rb:359:in `write_audit'
/usr/lib/rvm/gems/ruby-2.7.5@web/gems/audited-5.4.2/lib/audited/auditor.rb:331:in `audit_create'

a good aproach is to set return to true if condition :if is true

  audited on: [:create, :update, :destroy, :apply], if: MyApp::CONFIG[:audit_my_model]
      def run_conditional_check(condition, matching: true)
        return true if condition.blank? or condition == true
        return condition.call(self) == matching if condition.respond_to?(:call)
        return send(condition) == matching if respond_to?(condition.to_sym, true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant