Dmytro Vasin
Dmytro Vasin

Reputation: 823

Devise authenticate_user

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 )

Upvotes: 7

Views: 19710

Answers (1)

ronalchn
ronalchn

Reputation: 12335

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

Upvotes: 16

Related Questions