if_attribute 关于声明性授权

发布于 2024-08-25 05:10:01 字数 571 浏览 6 评论 0原文

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

我正在使用声明性组织,并且我只希望用户在有隶属关系并且隶属关系的 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"

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

I have a many-to-many relationship like this:
A user has_many organizations through affiliations and vice-versa.

I'm using declarative organizations and I only want a user to edit a particular organization if he is affiliated and the affiliationtype attribute of affiliation is a particular value.

So affiliations has 3 columns , user_id, organization_id and affiliationtype_id

I can do:

o = Organization.find(:first)
o.affiliatons[0].user and get the user

now I wish to do this:

has_permission_on [:organizations], :to => :edit do
  if_attribute (...)
end

That if_attribute should see if the current user is the organization.affiliation[?].user and if the organization.affiliation[?].affiliationtype_id = "3"

I hope this is syntax issue ... I really need to get this working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2024-09-01 05:10:01

编辑:

您可以使用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

EDIT:

You can restrict the type of affiliation with intersects_with(&block) :

  has_permission_on [:organizations], :to => :edit do
    if_attribute :affiliations => intersects_with {
      user.affiliations.with_type_3
    }
  end

Why not create a named_scope to find affiliations whose affiliationtype_id = 3?


From declarative_authorization documentation:

To reduce redundancy in has_permission_on blocks, a rule may depend on permissions on associated objects:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文