Using before_filter for accessing two model authentication

73 Views Asked by At

I am working with Rails 4 application. I have Users model and Operators Model (this is like a super Admin). Have a controller say testcontroller which has the following filter to validate the user has been authenticated.

before_filter :authenticate_user!

Now I have to access this testcontroller as Operator too. (in my case the Authentication of User and Operator are different). When I try to access, as my session would be current_operator, the controller's before_filter which has autheticate_user! is stopping me.

I tried adding the blow code to the testcontroller

before_filter :check_operator
#before_filter :authenticate_user!

private

def check_operator
if current_operator!=nil      
  :authenticate_operator!
else
  :authenticate_user!
end

end

And still not able to access testcontroller as Operator. Any thoughts would help me a lot

1

There are 1 best solutions below

1
Samy Kacimi On

I may have some ideas on this :

  • Be sure that your controller does not inherit of another controller that calls authenticate_user!
  • Be careful : If you are authenticated as a User and not an operator, your code in check_operator will never fall in authenticate_user! as you will always fall in authenticate_operator!

NB : You should use before_action, it is the new syntax for before_filter :)