[英]What should be in the columns of the roles table for acl9
Probably a stupid question but I am pretty close to getting acl9 implemented. 可能是一个愚蠢的问题,但我已经接近实现ACL9。 Just a little unsure what the data should look like in the roles table:
只是有点不确定角色表中的数据应该是什么样子:
id | name | authorizable_type | authorizable_id | created_at | updated_at
---+-------+-------------------+-----------------+------------+------------
I assume name would be "admin" and such. 我假设名称将是“ admin”之类的。
I'm not certain what authorizable_type
and authorizable_id
are referring to. 我不确定
authorizable_type
和authorizable_id
是指什么。
The main purpose of the authorizable_type
and the authorizable_id
map the `authorizable_object'. authorizable_type
和authorizable_id
的主要用途是映射“ authorizable_object”。 Hope this helps usually if you are creating a roles table in your application your migration should look like the following: 希望这通常会有所帮助,如果您在应用程序中创建角色表,则迁移应如下所示:
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
First of all, version 1.2 of acl9 ships with a generator for the migration needed for the roles table (as well as a few other goodies), see bin/rails g acl9:setup -h
for more details. 首先, acl9的 1.2版附带了一个生成器,用于角色表(以及其他一些好东西)所需的迁移,有关更多详细信息,请参见
bin/rails g acl9:setup -h
。
Secondly, to explain these fields, Rails has a feature called polymorphic associations ( read more about it here ), which allow you to associate one model with any other model. 其次,为了解释这些字段,Rails具有一个称为多态关联的功能( 在此处了解更多信息 ),该功能允许您将一个模型与任何其他模型关联。 acl9 uses this feature to enable to you apply a role on any other model you wish, in acl9 terminology we call those other models "objects" ( read more about that here ).
acl9使用此功能使您可以将角色应用于所需的任何其他模型,在acl9术语中,我们称这些其他模型为“对象” ( 在此处了解更多信息 )。 All you need to do is include the
acts_as_authorization_object
call in your model and that allows you to do something like this: 您需要做的就是在模型中包含
acts_as_authorization_object
调用,这允许您执行以下操作:
user.has_role! :admin, school
And that user has the :admin
role for that school
(and only for that particular school). 该用户对该
school
(且仅对该特定学校)具有:admin
角色。 As per the polymorphic association feature the authorizable_id
will contain the primary key of that school from your DB, and authorizable_type
will contain the class name "School"
. 根据多态关联功能,
authorizable_id
将包含您数据库中该学校的主键, authorizable_type
将包含类名称"School"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.