[英]Rails - CanCanCan, Rolify, Define - Defining Abilities for associated model
我正在使用Devise進行身份驗證,並使用CanCanCan / Rolify進行授權。
我想通過各種關聯將每個模型的記錄限制為創建它的用戶。
即/ sales / 1應該只能由屬於該銷售組織的用戶查看。
User
has_many :organisations
Organisation
belongs_to :user
has_many :sales
Sale
belongs_to :organisation
has_many :payments
Payment
belongs_to :sale
直接將組織限制為屬於它的用戶:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
elsif user.has_role? :standard
can [:create, :read, :update, :destroy], Organisation, :user_id => user.id
end
end
end
如何為關聯的銷售以及隨后的關聯付款實現相同的目的?
can [:manage], Sale, ?
can [:manage], Payment, ?
注意:Rails 5.0.0.1,Ruby 2.3.1
您可以在payment.rb中添加關聯
belongs_to :sale
has_one :organisation, through: :sale
然后您可以添加capability.rb
can :manage, Sale, :organisation => { :user_id => user.id }
can :manage, Payment, :organisation => { :user_id => user.id }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.