acl9的角色表的列应该是什么

时间:2012-12-19 23:21:43

标签: ruby-on-rails acl9

可能是一个愚蠢的问题,但我非常接近实施acl9。稍微不确定角色表中的数据应该是什么样的:

id | name  | authorizable_type | authorizable_id | created_at | updated_at 
---+-------+-------------------+-----------------+------------+------------ 

我假设名字是“admin”等等。

我不确定authorizable_typeauthorizable_id指的是什么。

2 个答案:

答案 0 :(得分:1)

authorizable_typeauthorizable_id的主要目的是映射`authorizable_object'。希望这通常有助于您在应用程序中创建角色表,您的迁移应如下所示:

class CreateRoles < ActiveRecord::Migration
  def self.up
    create_table :roles, :force => true do |t|
      t.string   :name
      t.string   :authorizable_type
      t.integer  :authorizable_id
      t.timestamps
    end
    add_index :roles, :name
    add_index :roles, [:authorizable_id, authorizable_type]
    add_index :roles, [:name, :authorizable_id, :authorizable_type], :unique => true
  end

  def self.down
    remove_index :roles, [:name, :authorizable_id, :authorizable_type]
    remove_index :roles, [:authorizable_id, :authorizable_type]
    remove_index :roles, :name
    drop_table :roles
  end
end

答案 1 :(得分:0)

首先,acl9版本1.2附带了一个用于角色表所需迁移的生成器(以及其他一些好东西),有关详细信息,请参阅bin/rails g acl9:setup -h

其次,为了解释这些字段,Rails有一个称为多态关联(read more about it here)的功能,它允许您将一个模型与任何其他模型相关联。 acl9使用此功能使您可以在任何其他模型上应用角色,在acl9术语中我们称之为其他模型“objects”read more about that here)。您需要做的就是在模型中包含acts_as_authorization_object调用,并允许您执行以下操作:

user.has_role! :admin, school

该用户具有该:admin的{​​{1}}角色(仅适用于该特定学校)。根据多态关联功能,school将包含您数据库中该学校的主键,authorizable_id将包含类名authorizable_type

相关问题