简体   繁体   English

具有CanCanCan的多个Devise用户或一个用户和权限?

[英]Multiple Devise users or one user and permissions with CanCanCan?

What is the preferred way to achieve two types of users (Client, Admin) with different attributes and behaviors? 实现具有不同属性和行为的两种类型的用户(客户端,管理员)的首选方法是什么?

Having two types of users created by Devise or having just one User model and setting permissions via CanCanCan? 是由Devise创建的两种类型的用户,还是只有一种User模型,并通过CanCanCan设置权限?

Thanks 谢谢

Use only one user and assign a role to the user with a specific column like role_id . 仅使用一个用户,并使用诸如role_id类的特定列为该用户分配角色。 Then you can have different roles, each one with a different id. 然后,您可以拥有不同的角色,每个角色都有不同的ID。

admin = 1

standard = 2

You can then define a method in the user like: 然后,您可以像这样在用户中定义一个方法:

def admin?
  role_id == 1
end

and in cancan you can use it like that: 在cancan中,您可以像这样使用它:

def initialize(user) HERE PERMISSIONS FOR NON LOGGED USERS if user HERE PERMISSIONS FOR LOGGED USERS if user.admin? HERE PERMISSIONS ONLY FOR ADMIN end end end

If you are using Rails >= 4.1 you can use an enum for that column. 如果您使用Rails> = 4.1,则可以对该列使用枚举。

I would say single model with permissions. 我会说具有权限的单一模型。 I made a detailed response on how to approach this here: 我对如何在这里进行处理做了详细的回应:

Setting up different User models and registration paths for Devise on Ruby on Rails 为Ruby on Rails上的Devise设置不同的用户模型和注册路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM