我有一个非静态 Controller ,我正在尝试使用 cancan 授权!应用权限的方法。
我有一个像这样开始的delete_multiple操作
def delete_multiple
@invoices = apparent_user.invoices.find(params[:invoice_ids])
我想检查用户是否有权删除所有这些发票,然后再继续。如果我使用
authorize! :delete_multiple, @invoices
权限被拒绝。我的能力.rb包括以下内容
if user.admin?
can :manage, :all
elsif user.approved_user?
can [:read, :update, :destroy, :delete_multiple], Invoice, :user_id => user.id
end
这是循环遍历我的数组并单独调用授权的问题还是有更聪明的方法?我开始觉得手动授权比使用 cancan 来处理复杂的非静态 Controller 更容易(尽管我的应用程序中有很多其他静态 Controller ,效果很好)。
最佳答案
这里有点晚了,但你可以把它写在你的能力类别中
can :delete_multiple, Array do |arr|
arr.inject(true){|r, el| r && can?(:delete, el)}
end
编辑
这也可以写成:
can :delete_multiple, Array do |arr|
arr.all? { |el| can?(:delete, el) }
end
关于ruby-on-rails - 如何使用cancan对一系列资源进行授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446654/