I am currently building an application in PHP using the Symphony 2 library, but I guess this question is applicable to any sort of web application. Here is the basic infrastructure I would like to implement:
- every user is part of one or multiple groups
- every group implements one or more roles
- roles implemented by a group apply to all users in that group
- a user can implement additional roles not in its group
An example
- a group 'writers' implement the 'writer' role and the 'comment moderator' role
- an group 'administrators' implements the 'admin' role
- a user 'Henry' is part of the writer group, and the administrator group
- a user 'Henry' implements the 'owner' role
The roles affective for that user would be 'writer', 'comment moderator', 'administrator' and 'owner'.
Edit
Is it a good practice or not to have this behavior : user can inherit roles from its own group and can have individual roles as well. And if so, how to make it real ?
I thought of 5 tables :
Users :
- id
- name
Role :
- id
- name
UserRole :
- id_user (FK)
- id_role (FK)
Group :
- id
- name
GroupRole :
- id_group (FK)
- id_role (FK)
UserGroup :
- id_user (FK)
- id_group (FK)
This could work and the main problem would be to prevent adding an individual role a user already has from the groups it belongs to.
Be it seems to be a little complicated. Is there any better way to do so ?
Thanks