In a Laravel application I have users
, roles
and permissions
. Roles are attached to users, and permissions are attached to roles.
Users:
id | name | email
______________________
1 | saba | saba@gmail.com
2 | nika | nika@gmail.com
3 | gio | gio@gmail.com
Roles:
id | name
______________________
1 | Admin
2 | Client
3 | Service_provider
Permissions:
id | name
______________________
1 | add_admin
2 | delete_admin
3 | approve_order
4 | delete_order
User_roles:
id | user_id | role_id
______________________
1 | 1 | 1
2 | 2 | 2
Role_permissions:
id | role_id | permission_id
____________________________
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
In this example user
with id
1, is Admin
and he can add_admin
, delete_admin
, approve_order
But I need different admins to have different permissions. In this structure if I attach admin
role to user
with id
2, his permissions would be the same as user
with id
1 has.
I need different admins to have different permissions, different Clients to have different permissions.
There is a little scenario:
Imagine there is a two user A and B, they want to register as clients, so when they register I will attach Client roles to them. They have same permissions, but if user A will upload his documents I need to attach new permission to user A, but not user B.
My solution is to create a new table where I attach permission to users
User_permissions:
Id | user_id | permission_id
____________________________
1 | 1 | 4
So user's all permissions will be his role's permissions + permissions from table I described above
I need to decide whether this solution is good or bad, or if there any other way better then this. Is there a best practice for this kind of problem?