【发布时间】:2016-06-02 14:24:00
【问题描述】:
我正在尝试使用 Rails 4 制作应用程序。
我已经用 Rolify gem 定义了一系列角色。
现在,我想使用 pundit 允许具有角色的用户执行某些操作。在不止一种类型的角色可以做某事的情况下,我定义了一组角色。
在我的 application_policy 中,我定义了私有方法,这些方法设置了我想在权威权限中使用的角色组。
我的应用程序策略实例化用户和记录。然后我将记录定义为相关模型的名称(与该模型的策略名称相同)。
我有:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
false
end
def new?
create?
end
def update?
false
end
def edit?
update?
end
def destroy?
false
end
def scope
Pundit.policy_scope!(user, record.class)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope
end
end
private
def cf_legal
[ :Admin, :CF_Legal, :CF_Policy_Manager ]
end
def cf_content
[ :Admin, :CF_Author, :CF_Editor ]
end
end
然后在我的内容政策中,我想说:
def record
content
end
def create
user.has_role? :cf_content
end
当我保存并尝试时,我看不到我应该看到的内容(作为具有作者角色的用户。
任何人都可以看到如何做到这一点?
【问题讨论】:
标签: ruby-on-rails permissions roles pundit rolify