CanCanの役割を設定するにはどうすればよいですか?また、それらの役割をユーザーに割り当てるにはどうすればよいですか?少なくとも今のところ、ユーザーが自分の役割を選択できるように、登録時にドロップダウンメニューを用意したいと思います。私はこれを見逃しているように見えるすべてのドキュメントについてはよくわかりませんが、どんな助けでも大歓迎です!
3 に答える
これが、ロールを管理するためのRailsに適した確実な方法です。私はそれを使用します。それはいいですね。基本的に、has-and-belongs-to-manyテーブルを作成して、ユーザーとロールを関連付け、単一の関数を定義しUser#has_role?
て、関連付けを確認します。
# file: app/models/user.rb
class User < ActiveRecord::Base
has_many :user_roles, :dependent => :destroy
has_many :roles, :through => :user_roles
def has_role?(name)
roles.pluck(:name).member?(name.to_s)
end
end
# file: app/models/role.rb
class Role < ActiveRecord::Base
has_many :user_roles, :dependent => :destroy
has_many :users, :through => :user_roles
end
# file: app/models/user_role.rb
class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
移行/スキーマファイル:
# file: db/migrate/xxx_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :email, :null => false, :default => ""
# ... any other fields you want ...
t.timestamps
end
end
end
# file: db/migrate/xxx_create_roles.rb
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name
t.timestamps
end
end
end
# file: db/migrate/xxx_create_user_roles.rb
class CreateUserRoles < ActiveRecord::Migration
def change
create_table :user_roles do |t|
t.references :user
t.references :role
t.timestamps
end
add_index :user_roles, [:user_id, :role_id], :unique => true
end
end
これで、Abilityファイルは次のようになります。
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # support guest user
if user.has_role?(:admin)
can :manage, :all
elsif user.has_role?(:nsa)
can :read, :all
elsif user.has_role?(:editor)
# ... add other abilities here
end
end
end
明確にするために、これは、そもそもどのように役割を作成するかについてのOPの質問に答えるものではありません。ただし、users、roles、およびuser_rolesは完全なRailsモデルになっているため、標準のCRUDツールを使用してそれらを管理できます。ただし、90%の場合、管理者権限を1人または2人のユーザーに割り当てるだけです。これは、次のようなシードファイルで簡単に実行できます。
# file: db/seeds.rb
# create a role named "admin"
admin_role = Role.create!(:name => "admin")
# create an admin user
admin_user = User.create!(:email => "admin@admin.com")
# assign the admin role to the admin user. (This bit of rails
# magic creates a user_role record in the database.)
admin_user.roles << admin_role
私はほとんどの部分でこれを理解しました。
- 役割を設定するために、能力クラスでそれらを定義するときに実際に設定されます。
例えば:
if user.has_role? :admin
can :manage, :all
else
can :read, :all
end
これは基本的に、管理者の役割を「設定」することです。何らかの理由で、他の場所でもその役割を初期化する必要があると思いました。
役割を割り当てるには、これまでに見たほとんどのチュートリアルに従って、railsコンソールに移動し、次のようなものを使用する必要があります。
user = User.find(1) user.add_role :admin # sets a global role user.has_role? :admin => true
最初の行はユーザーを検索し、2番目の行は役割を追加します。3つ目は、ユーザーがその役割に割り当てられているかどうかを確認するために使用されます。登録時にこれを追加する方法もあり、他にもいくつかあります。私はそれらに出くわしたときにここにそれらをリストしようとしますが、うまくいけば、これは将来誰かのための混乱を取り除くでしょう。:)
これを処理するには、ユーザーテーブルにロールフィールドを配置し、文字列フィールドでロールをコンマで区切ります。管理者にユーザーロールを管理させ、構成ファイルに対して有効かどうかをプログラムでチェックします(valid_role?、has_role?などの関数を使用します。次に、CanCanがuser_can_see_...などの呼び出し可能な関数をユーザーモデルに記述しました。
自己管理型のロールシステムでどのように機能するかはわかりませんが、興味がある場合は、私のコードはgithubのtws_authにあります。