Rails - CanCan - 如何设置和分配角色?

4

如何为CanCan设置角色,以及如何将这些角色分配给用户?我希望至少在注册时有一个下拉菜单供用户选择他们的角色。我不确定所有的文档是否都包含了这个信息,但任何帮助都将不胜感激!

3个回答

4
这是一种适合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

现在,您的能力文件看起来大致如下:

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

需要明确的是,这并没有回答原帖中如何创建角色的问题。但由于用户、角色和用户角色现在都是完整的Rails模型,您可以使用标准的CRUD工具来管理它们。但90%的时间,您只需要将管理员权限分配给一两个用户,在种子文件中轻松完成,像这样:

# 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

6
我很失望目前还没有人评论我在代码示例中关于NSA的恶作剧... ;) - fearless_fool

3

我大部分都弄清楚了。

  1. 关于设置角色,实际上是在定义能力类时进行设置的。

比如:

    if user.has_role? :admin
        can :manage, :all
    else
        can :read, :all
    end

这基本上是“设置”管理员角色。出于某种原因,我认为你还需要在其他地方初始化该角色。

  1. To assign the roles, following most tutorials I've seen, you need to go into the rails console and use something similar to:

    user = User.find(1)
    user.add_role :admin # sets a global role
    user.has_role? :admin
    => true
    
第一行查找用户,第二行添加角色。第三行用于检查用户是否被分配到该角色。还有其他在注册时添加的方法以及其他几种方法。我会尽量列出它们,但希望这能消除未来某个人的困惑。 :)

0
我通过在用户表中放置一个角色字段来处理这个问题,用逗号分隔字符串字段的角色。我让管理员管理用户角色,并通过编程检查配置文件中的有效性(使用如valid_role?,has_role?等函数)进行验证。然后,我在用户模型中编写了一些可以由CanCan调用的函数,例如user_can_see_...。
不确定如何为自行管理的角色系统工作,但如果您感兴趣,可以在以下链接找到我的代码:tws_auth on github

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接