简体   繁体   English

acl9:提前确定授权

[英]acl9: Determine authorization in advance

I am using acl9 on top of authlogic on one of my projects. 我在一个项目的authlogic上使用acl9。 I like the approach acl9's approach but I guess I am trying to do something that is just not that simple (not to say impossible). 我喜欢acl9的方法,但是我想我正在尝试做的事情不是那么简单(并不是说不可能)。

For reasons of usability I would like to have an inline admin panel. 出于可用性的原因,我希望有一个嵌入式管理面板。 So for example say I got a resource auction and a nested resource auction/bid. 例如,说我得到了一个资源拍卖和一个嵌套的资源拍卖/出价。 When a normal user is on auctions/1 (1=id) he should see the "normal" stuff (description, price...), a privileged user on the same site should have additional links for editing an auction (= auctions/1/edit). 当普通用户参加拍卖/ 1(1 = id)时,他应该看到“普通”内容(说明,价格...),同一站点上的特权用户应具有用于编辑拍卖的其他链接(=拍卖/ 1 /编辑)。 And this is where the fun starts. 这就是乐趣的开始。

Maybe I did not look close enough or in the wrong places but I could not find way to determine wether my current user is authorized for the edit action. 也许我看起来不够近或放置在错误的位置,但是我无法找到确定当前用户是否有权进行编辑操作的方法。 Obviously I could display the link to everyone and let the edit action reject the normal user, but that is not a viable option. 显然,我可以显示指向所有人的链接,并允许编辑操作拒绝普通用户,但这不是一个可行的选择。 What bugs me here is that the information is already in the system (see the access_control block below) and I can't seem to find a way to use it. 让我感到烦恼的是,信息已经在系统中了(请参阅下面的access_control块),我似乎找不到使用它的方法。 This is not DRY on so many levels. 在许多级别上都不是DRY。

 access_control do
  allow :privileged, :to => [:index, :show, :edit, :update]
  allow anonymous, :to => [:new, :show, :create]
 end

It becomes even worse if I want to determine which rights a user has on auctions/1/bids/2/edit because this is in a totally different controller. 如果我要确定用户对auctions/1/bids/2/edit拥有的权利,情况将变得更加糟糕,因为这是完全不同的控制器。

How can I access that information in advance and then decide whether to display a link to edit at all? 如何预先获取该信息,然后决定是否显示要编辑的链接? Is it just not possible (without changing acl9 itself) or did I not look hard enough? 只是不可能(不更改acl9本身)还是我看起来不够努力? Are there any authorization plugins that meet my requirements better? 是否有任何授权插件可以更好地满足我的要求?

what do you mean by accessing it "in advance"? “提前”访问意味着什么? Do you want to render different templates for the different users, or just have conditionals in the template to hide/show parts of it depending on the user's access levels? 您是要为不同的用户呈现不同的模板,还是要根据用户的访问级别在模板中添加条件来隐藏/显示其中的一部分? I believe it should be as simple as having something like this in the code or template: 我相信它应该像在代码或模板中包含以下内容一样简单:

if current_user.has_role?(:privileged)  
  # here goes the stuff that should be displayed to privileged user only  
end

If you need to access your specific set of rules in the template (like "have I allowed this user to access the edit action?") you can't do it with acl9. 如果您需要访问模板中的特定规则集(例如“我是否允许该用户访问编辑操作?”),则无法使用acl9进行操作。 And I believe you shouldn't do it anyway, as it doesn't seem good to tie your business logic and auth control to your controller - if you decide to rename the controller tomorrow, your logic with fail. 而且我认为您无论如何都不应这样做,因为将业务逻辑和身份验证控制与控制器绑定似乎不太好-如果您明天决定重命名控制器,则逻辑将失败。 But it can see two ways to solve it. 但是它可以看到两种解决方法。

  1. Split your roles into more detailed ones. 将您的角色分成更详细的角色。 For example, split "privileged" into "editor", "creator", "deleter", etc. Thus you can build your access rules with these smaller rules and use them in the template for a fine control. 例如,将“特权”划分为“编辑者”,“创建者”,“删除者”等。因此,您可以使用这些较小的规则构建访问规则,并在模板中使用它们进行精细控制。

  2. Delegate roles check to your objects. 将角色委托给您的对象。 That's the way I usually do it in my projects, as it allows to work only with global roles and let the models decide whether the user is allowed to do something to them - like if @auction.allows_edit_for?(current_user) {} , for example. 这就是我通常在项目中执行的方式,因为它只允许与全局角色一起使用,并让模型决定是否允许用户对他们执行某些操作,例如if @auction.allows_edit_for?(current_user) {} ,例如例。 It requires a couple of overrides for the acl9, I've blogged about this method here some time ago. 它要求一对夫妇将覆盖acl9的,我在博客中关于这个方法在这里前一段时间。

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

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