alejandrodevs / voltex

Dynamic permissions authorization with Rails.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

multi roles for a user

Sahar-Remodeling opened this issue · comments

I know how exactly role stored in the database , as a column containing the role id in the User table.
But I need to know if there is a possibility to have multi roles per user? as this scenario is applicable in our business!
can the set_current_permissions get the permissions for the roles of the current user?

Yes, It's possible and it's pretty simple.

You need to create the roles_users table. Your migration needs to look like this...

create_table(:roles_users) do |t|
  t.references :role
  t.references :user
end

Change users relation to has_and_belongs_to_many in your role model...

class Role < ApplicationRecord
  ...
  has_and_belongs_to_many :users
  ...
end

In your user model you need to change two things, your role relation needs to be has_and_belongs_to_many and your permissions relation needs to be through roles like this...

class User < ApplicationRecord
  ...
  has_and_belongs_to_many :roles
  has_many :permissions, through: :roles
  ...
end

And the rest it's going to work as expected.
We are considering to support this as a configuration in the future.
I hope this can be helpful. Let us know.

@alejandrodevs, Thank you very much. Yes it works as expected after I made the new association.

Appreciated :)