Rolify + CanCanCan:防止'admin'用户更改/创建'superadmin'



我正在使用rolify,CanCanCan和devise。

我有 3 种类型的用户:- 超级管理员-管理-正常

我想阻止"管理员"用户将用户更改为"超级管理员"角色

或创建/更改具有"超级管理员"角色的用户。

最好的方法是什么?在能力课上还是有某种before_filter?

这是我的能力文件,仅用用户部分进行了简化:

class Ability
    include CanCan::Ability
    def initialize(user)
       alias_action :create, :read, :update, :destroy, :to => :crud
       user ||= User.new # guest user (not logged in)
       if user.has_role? :normal
           can :read, User
       elsif user.has_role? :admin
           can :crud, User
       elsif user.has_role? :superadmin
           can :manage, :all
       else
           cannot :manage, :all
       end
   end
end

基于此处的文档

cannot :edit, User, User.all do |user|
    user.has_role?(:superadmin)
end

最新更新