问题描述
我正在尝试使用Rails 4制作应用.
我正在研究角色管理,并希望使用Rolify,因为它支持实例级角色分配.
对于其他在看同样问题的人,下面有2个非常好的答案(我只能打一个,但我都打了).查看lorefnon& Cyb3rDud3回答如下).我仍在弄清楚,但是已经使用数组(如lorefnon所示)和控制器/路由功能(如Cyb3rDud3所示)进行了迁移.
让我感到困惑的是,Rolify gem的所有文档都使用控制台来定义角色.
如何在代码中定义角色?
该委员会的其他成员提出了一些问题,这些问题暗示着他们在db:seeds文件中定义角色.我不想这样做,因为我想控制谁比谁可以创建角色更紧密地使用我的种子文件.
您在哪里做?
所有示例都显示它是通过控制台完成的.我想定义一个角色列表,然后再给角色授予权限(我想在这部分使用Pundit).
我有一个用户模型.我看过的另一个宝石是榜样.它要求您在用户模型中创建一系列角色.显然,您应该在Rolify中执行此操作-没有文档提供此步骤?
您在哪里定义角色?
Rolify文档未使用控制台定义角色-演示了如何在纯红宝石中添加角色.这非常强大,因为您可以在可以运行ruby的任何地方定义角色.您不受限于在某些配置文件或数据库表中定义的静态角色列表.
您需要问的第一个问题是何时创建角色?
最常见的用例分为两类:
1.角色是静态的.
角色是由应用程序开发人员,支持人员或公司高管在应用程序安装/部署期间创建的,并且在运行的应用程序的生命周期中,这些预先创建的角色已分配给不同的用户.
此类角色最常见的用例包括对公司中的人员(开发人员,经理,支持人员等)进行建模建模,或对先前已知的职责(编辑人员,管理员,查看者等)进行建模
如果您的角色确实属于此类用例,则必须决定下一步-创建和修改角色是谁的责任.通常有两种可能性:
1.1..应用程序开发人员本人是必须添加/删除/修改角色的人.在这种情况下,最好依靠种子数据或Rails迁移.
迁移的优点是,您可以根据需要轻松地回滚数据.这种迁移是rolify生成器生成的迁移的补充,而rolify生成器为您创建了角色相关表的架构(请参见下图).
这样的迁移可能看起来像:
db/migrate/20151204083556_create_application_roles.rb
class CreateApplicationRoles < ActiveRecord::Migration
def up
['admin', 'support', 'editor'].each do |role_name|
Role.create! name: role_name
end
end
def down
Role.where(name: ['admin', 'support', 'editor']).destroy_all
end
end
有些人正确地认为具有模式更改和更改的反模式数据更改均由迁移管理. 数据迁移是一个让您将以数据为中心的迁移与模式迁移分开的瑰宝./p>
在这种情况下以及角色实际分配以下的所有其他情况下,将通过rolify提供的add_role
或remove_role
方法根据用户操作或应用程序事件发生,这将在运行应用程序的生命周期中发生而不是在应用程序安装期间.
1.2 :添加/删除/修改角色的任务由支持团队或技术主管完成.在这种情况下,需要提供一个管理界面来管理角色.
在这种情况下,您将有一个Rails控制器来管理角色.创建动作将用于创建角色,显示动作将在那里展示角色等.这些动作将具有随附的视图,这些视图将提供图形用户界面以供最终用户管理角色.
2.角色是动态的
此类别涵盖了一些用例,在这些用例中,角色更像类别或标签,并且可以由最终用户创建/修改/删除.例如,图书馆员可以为特定类型的书籍分配某些角色/类别.
这种情况类似于1.2,因为您必须通过rails控制器处理创建/删除/更新角色.
下一部分是表中信息的结构.
Rolify需要一个特定的架构(在一定程度上可以自定义),但是该预期的架构足够灵活以处理所有上述用例.
I am trying to make an app with Rails 4.
I am looking at role management and want to use Rolify because it supports instance level role assignment.
For others looking at the same problem, there are 2 really good answers below (I can only tick one but I used both). Check out lorefnon & Cyb3rDud3 answers below). I'm still figuring it out, but have made a migration with an array (as lorefnon shows) and the controller/routes functions (as Cyb3rDud3 shows).
What is completely baffling me is that all the documents for the Rolify gem use the console to define roles.
How do I define roles in my code?
Others on this board have asked questions which allude to them defining roles in their db:seeds file. I don't want to do that because I want to control who uses my seeds file more tightly than who can create roles.
Where do you do it?
All the examples show it being done from the console. I want to define a list of roles and then I want to give roles permissions (I want to use pundit for this part).
I have a user model. The other gem I looked at was role model. It asks you to create an array of roles in the user model. It it just so obvious that you're supposed to do that in Rolify- that none of the documents give you this step?
Where do you define the roles?
Rolify documentation does not use the console to define roles - it demonstrates how roles can be added in pure ruby. This is amazingly powerful because you candefine roles whereever you can run ruby. You are not restricted to a static list of roles defined in some configuration file or in some database table.
The first question you need to ask is when do the roles get created ?
The most common use cases fall into two groups:
1. Roles are static.
Roles are created once by the application developer, support staff or company executives during application installation/deployment and during the lifetime of the running application these pre-created roles are assigned to different users.
The most common use cases of such roles includes modelling designations of a people in a company (developer, manager, support etc.), or modelling priorly known responsibilities (editor, administrator, viewer etc.)
If your roles do fall into such use cases, next thing you have to decide - whose responsibility is it to create and modify the roles. There are typically two possibilities:
1.1. Application developer himself is the person who has to add/remove/modify roles. In such cases it is best to rely on seed data or Rails migrations.
Advantage of migrations is that you can rollback the data easily if need be. This migration is additional to migration generated by rolify generators which create the schema of roles related tables for you (refer to the diagram below).
Such a migration might look like:
db/migrate/20151204083556_create_application_roles.rb
class CreateApplicationRoles < ActiveRecord::Migration
def up
['admin', 'support', 'editor'].each do |role_name|
Role.create! name: role_name
end
end
def down
Role.where(name: ['admin', 'support', 'editor']).destroy_all
end
end
Some people rightly consider it an antipattern to have schema changes and data changes both managed by migrations. data-migrate is a gem that allows you to separate the data centric migrations from your schema migrations.
In this case and all the other cases below actual assignment of roles will happen based on user actions or application events through add_role
or remove_role
methods provided by rolify.This will happen during the course of the lifecycle of running application and not during application installation.
1.2 The task of adding/removing/modifying roles is done by a support team or technical executives. In such cases it would be required to provide an administrative interface for managing roles.
In this case you will have a rails controller to manage the roles. The create action will be used for creating role, show action will be there to present the role etc. These actions will have accompanying views that will provide a graphical user interface to end user to manage the roles.
2. Roles are dynamic
This category covers use cases where roles are treated more like categories or tags and can be created/modified/deleted by end users. For example a librarian can assign some role/category to a particular genre of books.
This case is similar to 1.2 because you have to handle creating/deleting/updating roles through rails controllers.
Next part is how the information is structured in your tables.
Rolify expects a specific schema (customizable to certain extent) but the expected schema is flexible enough to handle all the above use cases.
这篇关于用Rolify定义角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!