Domanda

Ho un piccolo problema con l'autorizzazione dichiarativa.Ho un utente e un modello di ruolo con un'associazione Has_and_Belongs_to_Many.

Ho creato un ruolo denominato: moderatore nella mia autorizzazione_rules.rb

È possibile che un utente con il moderatore ruolo abbia solo gli utenti che hanno il ruolo del moderatore assegnato ad esso ??-> utente.with_permissions_to (: indice)

Ho pensato che sarebbe stato possibile:

role :moderator do
  has_permission_on :users, :to => :index do
    if_attribute :roles => contains { ????? }
  end
end
.

Ho anche creato un nome_scope nel mio modello utente perché pensavo che avrebbe aiutato ...

class User
  has_and_belongs_to_many :roles
  named_scope :by_role, lambda { |role|
    {
      :include => :roles,
      :conditions => {"roles.name" => role}
    }
  }
end
.

Qualcuno sa se è possibile farlo con dichiarative_authorization?

Grazie per il tuo aiuto!

È stato utile?

Soluzione

I did something similar in one of my projects but found dec_auth really confusing at the time. I think this is what you need to do:

authorization_rules.rb:

role :moderator do
  has_permission_on :users, :to => :index
end

User Model:

class User < ActiveRecord::Base
  using_access_control
end

Controller:

@users = User.with_permissions_to(:index)

Let me know if that doesn't work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top