PHP ACL Example


Often, many web apps especially large scale web apps will have user roles and permissions (access control list),

admin – having full control over the system

privileged user  -  having some control over the system as assigned by  admin

guest user / limited user – having only few permissions.

like that.

In the following example, I have used Bitwise operators

Let us set permissions for each actions like add,edit,delete…

//Access Permissions:
 $writeData = 1;
 $readData = 2;
 $deleteData = 4;
 $addUser = 8;
 $deleteUser = 16;

Now Let us assign permissions to different user groups,

// User groups:
 $root = $writeData | $readData | $deleteData | $addUser | $deleteUser;
 $powerUser = $readData | $deleteData | $deleteUser;
 $user = $writeData | $readData;
 $guestUser = $readData;

Let us write a function to check whether user has rights,

function checkRights($user, $permission) {
 if($user & $permission) {
 return true;
 } else {
 return false;
 }
 }

Now we apply all these,

if(checkRights($root, $addUser)) {
  //do some action
  echo "Hello I am Root - Few have more power...";
 }
 else
 { 
 echo "Ooops U r Not an Root User";
 }

Output:

Hello I am Root - Few have more power...
About these ads
Categories: PHP | Tags: , , , , , , | 1 Comment

Post navigation

One thought on “PHP ACL Example

  1. sridevi

    good keep it up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Blog at WordPress.com. Theme: Adventure Journal by Contexture International.

Follow

Get every new post delivered to your Inbox.

Join 188 other followers

%d bloggers like this: