私はレール初心者であり、ここで初めてポスターを作成し、作成中の小さなタイムシートアプリでロールベースアクセス制限を実装するために宣言型承認を使用しています。
私のビューの 1 つで、ログインしているユーザーに管理者ロールが割り当てられている場合、time_registers の index.html.erb に詳細情報を表示する必要があります。
最初は、ユーザーがid == 1のユーザーであることを確認していました
<% if @current_user.id == 1 %>
しかし、id==1 のユーザーに制限するのではなく、admin ロールが割り当てられているすべてのユーザーが index.html.erb ファイルでもう少し閲覧できるようにしたいと考えています。
モデルが declarative_authorization でどのように設定されているか少し
class User < ActiveRecord::Base
has_many :assignments
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignment
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
私の承認ファイルは次のようになります。
authorization do
role :usuarios do
has_permission_on :users, :to => [:index, :show, :new, :create, :edit, :update]
end
role :reghoras do
has_permission_on :time_registers, :to => [:index, :show, :new, :create, :edit, :update]
has_permission_on :users do
to :show
if_attribute :id => is {user.id}
end
end
role :contactos do
has_permission_on :contacts, :to => [:index, :show, :new, :create, :edit, :update]
has_permission_on :users do
to :show
if_attribute :id => is {user.id}
end
end
role :admin do
has_permission_on :authorization_rules, :to => :read
has_permission_on [:time_registers, :contacts, :users, :roles], :to => [:index, :show, :new, :create, :edit, :update, :destroy]
end
role :guest do
has_permission_on [:time_registers, :contacts], :to => [:index, :show]
end
end
この質問に答えるために他に何が必要なのかわかりませんので、お気軽に詳細情報をリクエストしてください。