Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store user permissions?

Designing a fairly complicated site with a lot of ajax running on a single page. I have reached the point where some user's need to have specific permission to do things and some need to be stopped from the action. I have set up user roles in my database and all is working fine, but I wonder if there is an easier/safer method for me to store each permission.

Currently, when a user logs in their specific permissions are grabbed from the db and loaded into a session array. To check if the user has permission, I simply check to see if the permission is contained in the array. This seems sluggish, and almost like I am missing a better solution.

Also, sessions can apparently be edited by the user... is there a safer method?

I have thought running a query for each check, but that could greatly increase the load time for a simple ajax request.

I am open to any and all ideas. Thanks.

like image 873
Capt Otis Avatar asked Sep 09 '10 04:09

Capt Otis


People also ask

How do you store user roles?

One way to keep track of a user's role would be to add their role to the ID Token or Access Token when they authenticate so that the frontend can decode the token and know the user's role. Here are some other resources related to Role-Based Access Control (RBAC):

How do you store access rights to a database?

Assign a numerical value to a user which represents their permission level, say 1 = read, 2= write/read, 3 = modify/write/read. Then in your code, check for proper permission level before allowing a user to perform a specific task.

How do you define user permissions?

User permissions, part of the overall user management process, are access granted to users to specific resources such as files, applications, networks, or devices.

Which type of user has full permission?

A participant (user, group, organization, or role) granted the Full Control (All) permission is granted all permissions currently defined and any defined in the future. Therefore, if new permission types are defined, you do not have to write rules that specifically grant them to participants with full control access.


1 Answers

First and foremost, the user cannot edit Session variables. The only thing that is saved on the user's machine is a Session ID. That ID is then used by the server to grab key/value pairs that are stored ONLY on the server. From a client's standpoint, it is impossible to change values on a whim.

Second, I would not worry too heavily on a database connection. Avoid repeating yourself, but don't worry too much about the first connection.

Finally, my favorite way to do multiple permissions without creating roles is to use binary math. Some people like this, some people don't, but I find it useful.

To use this method, imaging that we define the following values:

CAN_EDIT_SOMETHING        = 1     // Powers of 2
CAN_SEE_SOMETHING_ELSE    = 2
CAN_DO_ADMIN_STUFF        = 4
...                       = 8

To give people multiple permissions, use binary OR

PERMISSIONS = CAN_EDIT_SOMETHING | CAN_DO_ADMIN_STUFF

To illustrate how this works, we can look at the bits:

   0b0001
OR 0b0100
---------
   0b0101

To check if someone has a permission, use binary AND

if( PERMISSIONS & CAN_EDIT_SOMETHING != 0 ) {
}

To see how this works, we look at the bits again

    0b0101
AND 0b0001
----------
    0b0001  // Not equal to 0. They must have that permission!

The final benefit of this method is that it allows you to combine multiple permissions easily into "meta-permissions"

// If both EDIT_SOMETHING and ADMIN_STUFF are tasks that an admin
// can perform, we can combine them easily
//
IS_FULL_ADMIN     = CAN_EDIT_SOMETHING | CAN_DO_ADMIN_STUFF


// We can then use this value exactly as we do any other permission
//
PERMISSIONS       = IS_FULL_ADMIN | CAN_SEE_SOMETHING ELSE

Use it if you want, but it is a nice trick to have in your arsenal.

like image 167
riwalk Avatar answered Oct 04 '22 04:10

riwalk