非常基本的用户模式,我希望管理员用户:管理所有

否则不能:索引,用户和一些其他的选择,但是当我尝试和查看用户索引阻断非管理员的用户,管理用户也无法访问。

这是我的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

我能做些什么来阻止用户索引被阻止所有用户?

此致

有帮助吗?

解决方案

有点毛病的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,用户)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top