リソースがさまざまな場所に属することができるリソース データベースがあります。ユーザーとグループ (自己参照ユーザー テーブル) は、異なる場所で異なるロールを持つことができます。グループは、他のグループ内に存在できます。承認は、'if_attribute' を使用して、ユーザーが表示、編集などを許可されている location_id の中に location_id があるかどうかを確認する単一ユーザーに対してうまく機能します。
has_permission_on :locations do
to [:show, :new, :create, :edit, :update, :destroy]
if_attribute :id => is_in { (user.permissions.where(:role => "admin").collect {|i| Location.find_by_id(i.location_id).subtree_ids}.flatten.uniq )}
end
グループは相互に「ネスト」できるため、すべての「正当な」ロケーション ID を見つけるには再帰的な方法を使用する必要があると考えました。私はこれを試しました:
has_permission_on :locations do
to [:show, :new, :create, :edit, :update, :destroy]
if_attribute :id => is_in { (user.permissions.where(:role => "admin").collect {|i| Location.find_by_id(i.location_id).subtree_ids} + find_group_location_ids(user)).flatten.uniq }
end
'authorization do' ルーチンの外で定義されたメソッドを使用:
def find_group_location_ids(user)
location_ids = []
nested_group_location_ids(user)
def nested_group_location_ids(user)
user.group_memberships.each do |gm|
location_ids = location_ids + gm.user.location.id
nested_group_location_ids(gm.user)
end
end
return location_ids
end
問題は、メソッド呼び出しでメソッドが見つからないことです。次のエラーが表示されます。
NoMethodError (未定義のメソッド `find_group_location_ids' for (Authorization::Engine::AttributeValidator:0x7fb63448e2b0)
メソッド定義をさまざまな場所に配置しようとしましたが、うまくいきませんでした。
if_attribute を使用して、id が再帰メソッドからの配列内にあるかどうかを確認するにはどうすればよいですか?