所以我要直截了当地说。
我正在使用ActiveAdmin
宝石,所以我的应用程序中有AdminUser
模型.现在我从我的客户端获得了一个要求,即“超级管理员”必须能够控制其他管理员的权限。
因此,例如,如果我有资源:Message
、Client
和Country
,“超级管理员”应该能够将管理消息的任务分配给AdminUser,将管理客户端的任务分配给另一个AdminUser,将管理国家的任务分配给另一个AdminUser。
为此,我考虑将几个boolean
属性添加到admin_users
表中。例如,一个名为“boolean
”的super_admin属性将用于确定该AdminUser
是否可以更改其他AdminUsers
的权限,而另一个名为message的属性将用于确定该AdminUser
是否具有控件(可以编辑、读取、删除等)。在消息上,将使用另一个名为country的属性来确定此AdminUser
是否具有控件(可以编辑、读取、删除等)。在国家的上空……
,有什么问题吗?,我不能在模型中access
to current_admin_user,所以我不能做这样的事情:
ActiveAdmin.register Message do
if (current_admin_user.message)
permit_params Commune.attribute_names.map(&:to_sym)
end
end
那我能做什么呢?我必须构建这个功能!
发布于 2018-09-29 03:47:12
编辑我发现这个gem将角色添加到active admin role中
为什么要做模特?代码看起来应该放在控制器中,permit_params.
我会用专家。我可以看到,,can,can,,是五年前更新的。它们是相似的。
专家回购:https://github.com/varvet/pundit
它使用策略,因此您为每个模型创建策略。
class PostPolicy < ApplicationPolicy
def update?
user.admin? or not record.published?
end
end
你可以在更新、创建、显示或者其他任何东西时检查你的旗帜.
实际上,您使用的是类似于以下授权@post :update?之类的东西
引用他们医生的话
在这种情况下,您可以想象授权会做这样的事情:
unless PostPolicy.new(current_user, @post).update?
raise Pundit::NotAuthorizedError, "not allowed to update? this #{@post.inspect}"
end
希望它能帮上忙
你需要更复杂的解决方案。我会创建角色模型,在这里我可以指定模型、读、写权限。我会将它与我的用户链接到has_many角色,并在策略中执行如下操作:
class PostPolicy < ApplicationPolicy
def get_role
user.roles.where(model: "Post").first
end
def update?
user.get_role.write? or not record.published?
end
end
或者有更好的方法在政策模型中使用.
https://stackoverflow.com/questions/52565127
复制