Devise authenticate_user

19,293

Try using two before filters - one for admin only actions, and another for admin or user actions.

# 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
Share:
19,293
Dmytro Vasin
Author by

Dmytro Vasin

Updated on June 04, 2022

Comments

  • Dmytro Vasin
    Dmytro Vasin almost 2 years

    help my in that question:

    i have 2 models ( admin and user ) -> created with devise, and i have post_controller:

    and the question arises:

    if i have one model ( user.rb ) -> in my controller i put that:

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

    but i have 2 models and i want to User have access to 'show' and 'index' action of post controller and Admin have access to all actions.

    and i do something like that:

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

    but i want change my string:

    authenticate_user!

    to something like that:

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

    how can I do it ( without 'cancan' gem )