views:

46

answers:

1

How do people manage permissions between their code base and the database? For example, my application is becoming littered with:

if($objects['username']['access_type'] == 'edit'){
    // print the HTML to edit the username
}

or in OO:

if($user->getPermission('username')->canEdit()){
    // print the HTML to edit the username
}

How do you keep track of where the object 'username' and the permission 'edit' are used and how do you maintain the link between these hard-coded permission 'tags' and their relevant entries in the permissions table in the database? Surely from time to time, some of these must get lost, renamed or misused? Any thoughts?

A: 

Cant you make a permission table?

permisionId - UserId - Module - Permissions:

The permissionId is autonr, the userId is the user, Module is for example 'username', permissions is the permission type.

You can use the permission field as int and use the 'AND' bitwise operator on it (like chmod does): 1 = read, 2=write, 1+2=3=read+write

This way, you don't have to make a row for eacht permission you want to give a module.

VeeWee
@VeeWee, hi there. Yes, I already have a `user`, `user-role`, `role`, `role-permission` and `permission` table. I'm wondering once I've written the module tag `username` in the code and I have a representation of it in my DB, how do people keep on top of what is where? For example, if I rename the module in the code, I have to rename it in the DB too...
Bendos
There is nothing you can do about this... You should name your modules in a way you can easily find/remember them. Or you could work with the value of the auto_increment field, but that isn't a good idea. One thing you can also do is make a array in php and link it with the auto increment field in mysql e.g: $mod['username'] = 1;. But I also don't think this is a good idea...
VeeWee
What if... the HTML/JavaScript code was actually in the `module` table in the database instead of in a file? A bit like the website content lives in a table in the CMS. Then conceivably, by joining my user-role-permission-module tables, for any user, I could return the code required from the DB and build the page bit by bit, using search and replace to fill in values.
Bendos
I don't think that is a problem? You load the name of the module, straight from the module table into your URL. Than in the loadmodule page you check the permissions to this module?
VeeWee