if_attribute sur autorisation déclarative
-
19-09-2019 - |
Question
J'ai plusieurs à-plusieurs comme ceci: A organisations has_many utilisateur grâce à des affiliations et vice-versa.
J'utilise les organisations déclaratives et je veux que l'utilisateur de modifier une organisation particulière s'il est affilié et l'attribut affiliationtype d'affiliation est une valeur particulière.
affiliations a 3 colonnes, user_id, organization_id et affiliationtype_id
Je peux le faire:
o = Organization.find(:first)
o.affiliatons[0].user and get the user
maintenant je veux faire ceci:
has_permission_on [:organizations], :to => :edit do
if_attribute (...)
end
Ce if_attribute devrait voir si l'utilisateur actuel est le [?] De organization.affiliation. Utilisateur et si le organization.affiliation [?]. Affiliationtype_id = "3"
J'espère que ce problème est la syntaxe ... Je dois vraiment obtenir ce travail.
La solution
EDIT:
Vous pouvez restreindre le type d'affiliation avec intersects_with (et bloc) :
has_permission_on [:organizations], :to => :edit do
if_attribute :affiliations => intersects_with {
user.affiliations.with_type_3
}
end
Pourquoi ne pas créer un named_scope pour trouver des affiliations dont affiliationtype_id = 3?
De :
Pour réduire la redondance dans les blocs de has_permission_on, une règle peut dépendre des autorisations sur des objets associés:
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