Come up with authenticate_user

help me with this question:

I have 2 models (admin and user) -> created using development, and I have post_controller:

and the question arises:

If I have one model (user.rb) -> in my controller, I will put this:

before_filter :authenticate_user!, :except => [:show, :index] 

but I have 2 models, and I want the User to have access to the "show" and "index" actions of the post-controller, and the administrator has access to all actions.

and I am doing something like this:

  before_filter :logged_in . . . private def logged_in if admin_signed_in? else authenticate_user! end end 

but I want to change my line:

authenticate_user!

like that:

:authenticate_user!, :except => [:show, :index]
but besides links to before_filter

how can i do this (without cancan gem)

+6
source share
1 answer

Try using two in front of filters - one for actions only for the administrator, and the other for actions of the administrator or user.

 # ensure admin for other actions before_filter :check_admin_logged_in!, :except => [:show, :index] # ensure user or admin logged in for these actions (:only option is optional) before_filter :check_user_logged_in!, :only => [:show, :index] private def check_admin_logged_in! # admin must be logged in authenticate_admin! end def check_user_logged_in! # if admin is not logged in, user must be logged in if !admin_signed_in? authenticate_user! end end 
+13
source

Source: https://habr.com/ru/post/924616/


All Articles