Rails - CanCan - Set&分配角色?

时间:2012-08-21 09:03:24

标签: cancan

如何为CanCan设置角色,以及如何将这些角色分配给用户?我想现在至少在注册时有一个下拉菜单供用户选择他们的角色。我不太确定我一直希望错过的所有文档,但是非常感谢任何帮助!

3 个答案:

答案 0 :(得分: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

现在,您的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关于如何创建角色的问题。但由于用户,角色和user_roles现在是完整的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

答案 1 :(得分:2)

我在很大程度上想到了这一点。

  1. 对于设置角色,它们实际上是在能力类中定义时设置的。
  2. 例如:

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

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

    1. 要分配角色,按照我见过的大多数教程,你需要进入rails控制台并使用类似的东西:

      user = User.find(1)
      user.add_role :admin # sets a global role
      user.has_role? :admin
      => true
      
    2. 第一行找到用户,第二行添加角色。第三个用于检查用户是否被分配到该角色。还有一些方法可以在注册时添加此功能,还可以添加其他一些功能。当我遇到它们时,我会尝试在这里列出它们,但希望这可以解决将来对某人的任何困惑。 :)

答案 2 :(得分:0)

我通过在用户表中放置一个角色字段来处理这个问题,并在字符串字段中以逗号分隔角色。我让管理员管理用户角色,对配置文件进行编程检查是有效的(使用诸如valid_role?,has_role等函数。然后我在CanCan可以调用的用户模型中编写了一些函数,例如user_can_see _...

不确定这对自我管理的角色系统有何影响,但如果您有兴趣,可以在以下网址找到我的代码:tws_auth on github