Forum Moderators: coopster

Message Too Old, No Replies

Cumbersome if's and else's

         

epsd

6:06 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



I've got a piece of code I wrote that goes like this:

$curdirtwo = substr(strrchr(getcwd(), '/'), 1); //gets current directory

if ($curdirtwo == 'somedirectory') {
$oktoworkindir = array( "billtest","johntest","joetest","janetest"); //users who are allowed to work in this dir
}

It gets the current directory. If the current directory is equal to a defined directory, a set of users are allowed to be in there working. Works fine. However, I'd have to do a whole bunch of 'else ifs' for each directory that has a unique set of users to work in it... Seems cumbersome and long. Is there any way to accomplish this that I'm not thinking of? Thanks for any input!

willybfriendly

7:41 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might try using a switch [us.php.net] statement. If nothing else it would make the code easier to read.

d40sithui

11:49 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



you can also try a system where you have groups of users with privileges in certain directories. this information would be stored in a db of course. all directories would have a unique id. you would then have another table that contains the group id and the directory id in which they are allowed in.
then its just as simple as checking the current logged in user, their group, and if the group has rights to whatever directory they're in.


//gets current directory
//queries for directory id (from db)
//queries database for user information and group membership
//queries database for privilege to current directory id
//if/else statement to allow or disallow current user.

ashishp

5:53 am on Nov 18, 2007 (gmt 0)

10+ Year Member



What your are trying to accomplish is called Role Based Access System, (RBAC). The US Government National Institute of Standards and Technology has more details if you wish.

Create an access control system in terms of users, groups and permissions (similar to WordPress's role based permissions)

Users are assigned to groups and the groups have permissions. In effect the permissions are not user based but group based.

If the user belongs to that group he inherits all the groups permissions.

The users can belong to many groups, which can have many users

and

a permission can be given to multiple groups and each group can have multiple permissions.

For more information try searching for "role based access system in php".

HTH