views:

210

answers:

2

Very basic user model, I wish the admin user to :manage all

else cannot :index, User and some other options, but when I try and block non admin users from viewing the user index, the admin user also has not access.

this is my ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new #guest user

    can :manage, :all if user.role == "admin" #if user.admin? can :manage, :all
    can :assign_role, User 

    else
      can :read, :all
      can :create, User


      cannot :assign_role, User
      cannot :index, User

      can [:show, :edit, :update], User do |current_user|
              user.id == current_user.id || user.role == "admin"
            end



  end
end

What can I do to stop all users being blocked from User index?

Regards

Dan

A: 

Something wrong with if-else in code.

if user.role == "admin"
  can :manage, :all
  can :assign_role, User 

else
  can :read, :all
  can :create, User


  cannot :assign_role, User
  cannot :index, User
  can [:show, :edit, :update], User do |current_user|
    user.id == current_user.id || user.role == "admin"
  end

end

And also you don't have to deny non-admin user to assign role obviously (cannot :assign_role, User).

uzzz
Am I right in thinking the IF statement for admin stop right after the first "Can" because I didn't state with IF and started withCAN :MANAGE, :all IF USER.ROLE == "ADMIN"Anyway thank UZZZ, great help!
MrThomas
yes, "if" statement works only for one line of code if it's situated in the end of a line
uzzz
A: 

I got almost the same problem, maybe someone can help me!

I am using CanCan with Authlogic, i need to restric the access of the users so the current_user can only edit his own account and not somebody else account.

i used the solution presented by uzzz, but when i try to show the current_user i am always getting the current_user as nil and in my user's controller i am using authorize_resource.

Thiago Diniz