非常基本的用户模型,我希望管理员用户:管理所有
else 不能:index、User 和其他一些选项,但是当我尝试阻止非管理员用户查看用户索引时,管理员用户也没有访问权限。
这是我的能力.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
我能做些什么来阻止所有用户被用户索引阻止?
问候
担
最佳答案
代码中的 if-else 有问题。
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
而且您不必明显拒绝非管理员用户分配角色(不能:assign_role,用户)。
关于ruby-on-rails - CanCan gem |不能 :index, 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2367169/