4

So I am in the process of attempting to create a basic user system and within this system I want users to be able to have multiple roles.

Say for example I have the roles as follows: Administrator, Events Organiser, Donator

What is the best way to assign these multiple roles to a user and then check if they have any of these roles for showing certain permissions.

If it was only one role per person then it wouldn't be a problem as I'd just assign say Administrator = 10, Organiser = 5 and Donator = 1 and then do an if statement to check if the MySQL data is equal to any of those three numbers.

I can't imagine there is a way to add a MySQL Field and fill it with say "Administrator,Donator" and therefore that user would have both of those roles?

Is it just a case of I would need to create 3 separate fields and put a 0 or a 1 in those fields and check each one separately?

3 Answers 3

12

Use multiple tables and join them:

User
--------------
id    name
 1    test

Role
--------------
id    name
 1    Donator
 2    Organizer
 3    Administrator

User_Role
--------------
id    user_id    role_id
 1    1          1
 2    1          3


SELECT * FROM User u 
    LEFT JOIN User_Role ur ON u.id = ur.user_id
    LEFT JOIN Role r ON ur.role_id = r.id
WHERE r.name = "Administrator";

The query is easier if you know you only have 3 roles and they are easy to remember.

SELECT * FROM User u LEFT JOIN User_Role ur ON u.id = ur.user_id WHERE ur.role_id = 3;
2
  • Ok, so I now have it so that I can get the role IDs displaying in an array for a certain user with the query: SELECT role_id FROM wp_users u LEFT JOIN users_roles ur ON u.id = ur.user_id WHERE u.id ='$user_ID' How would I make it though so it showed the role name instead of the role_id.
    – Tenatious
    Oct 19, 2012 at 18:58
  • 1
    SELECT role.name AS name FROM wp_users u LEFT JOIN users_roles ur ON u.id = ur.user_id LEFT JOIN role r ON ur.role_id = r.id WHERE u.id = '$user_ID' - you should look into parameterized queries as well.
    – Benny Hill
    Oct 19, 2012 at 20:07
7

You will have a roles, users and users_roles tables:

The roles table will hold the various roles your users can have. In my example data I've declared Administrator and Donator roles.

roles
    id             unsigned int(P)
    description    varchar(15)

+----+---------------+
| id | description   |
+----+---------------+
|  1 | Administrator |
|  2 | Donator       |
| .. | ............. |
+----+---------------+

And of course you'll have to store information about your users.

users
    id          unsigned int(P)
    username    varchar(32)
    password    varbinary(255)
    etc.

+----+----------+----------+-----+
| id | username | password | ... |
+----+----------+----------+-----+
|  1 | bob      | ******** | ... |
|  2 | mary     | ******** | ... |
| .. | ........ | ........ | ... |
+----+----------+----------+-----+

Finally you'll tie the two together in the users_roles table. In my example data you can see that bob is a Donator and mary is both an Administrator and a Donator. The user_id and role_id are both foreign keys to their respective tables and together they form the primary key for this table.

users_roles
    user_id    unsigned int(F user.id)\_(P)
    role_id    unsigned int(F role.id)/

+----+---------+---------+
| id | user_id | role_id |
+----+---------+---------+
|  1 |       1 |       2 |
|  2 |       2 |       1 |
|  3 |       2 |       2 |
| .. | ....... | ....... |
+----+---------+---------+

This way a user can have an unlimited number of roles.

1
  • hah - you win for including data types and indexes/foreign keys.
    – kmfk
    Oct 19, 2012 at 18:25
2

If you don't want to create two new table for such small thing. You can utilize MySQL built in SET data type or you can store comma separated role in varchar and do query operation using FIND_IN_SET() Eg.

SELECT * FROM user WHERE FIND_IN_SET('admin', role)>0;

+----+----------+----------+----------------+ | id | username | password | role (varchar) | +----+----------+----------+-----------------+ | 1 | bob | ******** | admin | | 2 | mary | ******** | admin,role1 | | .. | ........ | ........ | role1,role2 | +----+----------+----------+-----------------+

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.