Using comparators such as
if (admin) {do x}
else if (user) {do y}
Is a bad idea as it is inflexible and doesn't scale well.
There are a few options. If your permissions are hierarchical, meaning that one user can do everything and lower users have less permissions than the user above e.g.
admin - 1, 2, 3
accountant - 1, 2
user - 1
Then you can give them numbers and say
if (permissionValue => 500)
{
action2();
}
And have a table:
admin 1000
accountant 500
user 250
This allows you to add in a new user type (say moderator) between the admin and accountant, but will not work if you have:
admin 1, 2, 3
accountant 1, 2
moderator 1, 3
user 1
As the permissions for moderator and accountant are on the same level.
By far the best solution is using bitwise operators and assigning binary values to your protected areas.
Take a simple example where a user can read content, a moderator can read and write content, and an admin can read, write and delete content. You would have the following table (the values are constructed using bitwise or operator)
Users: Read Write Delete
admin: 7 - (0b001 | 0b010 | 0b100) = 0b111 = 7
moderator: 3 - (0b001 | 0b010 | NO ) = 0b011 = 3
user: 1 - (0b001 | NO | NO ) = 0b001 = 1
You can then do:
//Permissions:
define('READ', 1);
define('WRITE', 2);
define('DELETE', 4);
if ($userPermissions & READ)
{
//Allowed to Read
}
if ($userPermissions & WRITE)
{
//Show write form
}
These are examples of bitwise operators. It means if x contains bit y return true. For the write function
User Permission
User Write
1 & 2
0b001 & 0b010 -- returns false
Admin Write
7 & 2
0b111 & 0b010 -- returns true
In the first example, 0b001 does not contain the bit 0b010 so returns false. 0b111 does contain the bit 0b010 and returns true.
Read more Here