if_attribute上声明性授权
-
19-09-2019 - |
题
我有这样一个多到多的关系: 通过从属关系,反之亦然用户的has_many组织。
我使用的声明组织和我只希望用户编辑特定的组织,如果他是下属和隶属关系的affiliationtype属性是一个特定的值。
因此隶属有3列,USER_ID,的organization_ID和affiliationtype_id
我可以这样做:
o = Organization.find(:first)
o.affiliatons[0].user and get the user
现在我要做到这一点:
has_permission_on [:organizations], :to => :edit do
if_attribute (...)
end
这if_attribute应该看到,如果当前用户是organization.affiliation [?]。用户,并且如果organization.affiliation [?]。affiliationtype_id = “3”
我希望这是语法问题......我真的需要得到这个工作。
解决方案
修改强>
可以限制从属关系的类型与的 intersects_with(块)强>:
has_permission_on [:organizations], :to => :edit do
if_attribute :affiliations => intersects_with {
user.affiliations.with_type_3
}
end
为什么不创建一个named_scope找到的隶属关系,其affiliationtype_id = 3?
从 declarative_authorization文档:
为了减少has_permission_on块冗余,规则可以依赖于权限相关联的对象:
authorization do
role :branch_admin do
has_permission_on :branches, :to => :manage do
if_attribute :managers => contains {user}
end
has_permission_on :employees, :to => :manage do
if_permitted_to :manage, :branch
# instead of
#if_attribute :branch => {:managers => contains {user}}
end
end
end
不隶属于 StackOverflow