質問

リソースがさまざまな場所に属するリソースデータベースがあります。ユーザーとグループ(自己参照ユーザーテーブル)は、さまざまな場所で異なる役割を持つことができます。グループは他のグループ内にいることができます。承認は、「if_attribute」を使用して単一のユーザーに適しています。Location_idがユーザーが表示、編集などを許可されている場所_IDの1つであるかどうかを確認します。

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

「承認do」routineの外側で定義された方法で:

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 :: astributevalidator:0x7fb63448e2b0)

メソッド定義をさまざまな場所に配置しようとしましたが、運はありませんでした。

if_attributeを使用して、IDが再帰的な方法から配列内にあるかどうかを確認するにはどうすればよいですか?

役に立ちましたか?

解決

Google GroupsのSteffenbからSteffenbからSOMヘルプを受け取りました(彼は宣言_Authorizationの著者です)。解決策は、メソッドをユーザーモデルに移動することでした。

def find_group_location_ids
  location_ids = []
  nested_group_location_ids(self)
  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

そして、このようにautorization_rules.rbからそれを呼び出すために:

has_permission_on :locations do
  to [:show, :new, :create, :edit, :update, :destroy] 
  if_attribute :id => is_in { (user.memberships.where(:role_id => "admin").collect {|i| Location.find_by_id(i.location_id).subtree_ids}  + user.find_group_location_ids).flatten.uniq }
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top