繁体   English   中英

声明性授权的if_attribute

[英]if_attribute on declarative authorization

我有这样的多对多关系:用户通过附属关系拥有多个组织,反之亦然。

我正在使用声明性组织,我只希望用户编辑特定组织(如果他是附属的)并且affiliation的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 [?]。user以及organization.affiliation [?]。affiliationtype_id =“3”

我希望这是语法问题......我真的需要让它工作。

编辑:

您可以使用intersects_with(&block)限制从属关系的类型:

  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM