简体   繁体   中英

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?

Thanks

Use only one user and assign a role to the user with a specific column like role_id . Then you can have different roles, each one with a different 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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