Audited Gem current_user_method not working
Asked Answered
H

2

8

I am using the Audited gem in my application for tracking user log. Everything is working fine except for current user tracking.

In my case, I have 2 models: Instructor and Student. Instructor will be the current_admin_user, and I need to find the student manually.

To overcome this issue, I tried to override current_user_method and create an audited.rb file in the initializers with below content:

Audited.current_user_method = :current_admin_user

This is working fine, but when I use any other method like current_user_or_student ...

Audited.current_user_method = :current_user_or_student

in application_controller.rb ...

def current_user_or_student
  current_admin_user || InstructorStudent.find_by_id(id)
end

it is not going into this method, even current_admin_user is also not storing in audits.

Why isn't my current_user_or_student method being called when overriding it in application_controller.rb?

Haman answered 6/1, 2018 at 9:10 Comment(6)
I checked your github profile, but I do not know which project are you talking aboutClippers
@FabrizioBertoglio this project is not in my github profile. Do you need project to solve my problem ?Haman
can you share the detailed logs.Rohde
I dont have any log, but when i user current_admin_user in audited.rb, it is storing user perfactly, when i use custom method current_user_or_student it is not storing. even exit is also not working for that methodHaman
Can you provide the source of current_admin_user? Are you setting Audited.current_user_method in an initializer? Or are you attempting to change it dynamically at runtime?Karlmarxstadt
@DerekPrior I setting Audited.current_user_method in audited.rb initializer. It is working fine. because current_admin_user is method of devise. i want to customize method like when current admin user change details it should show current_admin_user, but when any student change details it should show student changed detail. student is just model it is not resource of devise and instructor is resource of devise.Haman
H
2

Finally I resolved my issue, I am using STI (single table inheritance) . my other controllers of instructor was not inherite from application controller. so my current_user_or_student was not called for instructor login .

To overcome this issue, i create one controller concern called audited_user.rb

and write following method in concern

def current_user_or_student
  current_admin_user || InstructorStudent.find_by_id(id)
end

and included this concern in both my instructor base controller and application controller. now everything is working fine and my audited user is also save correctly.

I hope this will save any others day.

Haman answered 12/1, 2018 at 4:7 Comment(0)
K
0

You have defined current_user_or_student as such:

def current_user_or_student
  current_admin_user || InstructorStudent.find_by_id(id)
end

Assuming current_admin_user is nil, it will try to find InstructorStudent. You are using find_by_id which will return nil if the InstructorStudent with that id cannot be found. It's likely that is what is happening. It's not clear from this source what id is -- are you sure it's set to an id that can be found in instructor_students? I would encourage you to do the following to debug:

def current_user_or_student
  current_admin_user || InstructorStudent.find(id)
end

This will raise an error if the method's conditional falls through to the InstructorStudent.find branch and the id cannot be found. If my hypothesis is correct, this will prove that the id cannot be found, so your original code is returning nil (while this updated code now errors instead). The error message will also tell you what the value of id is which you can use for further debugging.

Alternatively, you can debug this without changing code by running a request that you expect to be audited but isn't and then inspecting the server logs. You will see the queries run there and may be able to debug that way as well.

Karlmarxstadt answered 9/1, 2018 at 15:16 Comment(2)
i think i already resolved this issue, i just need to add before_action :current_user_or_student ,in application_controller.rb everything else was working fine. and now it is also working fineHaman
It is not working in that way, i dont what is wrong with my application controller, even ` before_filter :audited_user` is also not working . and it is not calling before any action :(Haman

© 2022 - 2024 — McMap. All rights reserved.