Skip to content
WJunction - Webmaster Forum

User Permissions using Bitwise / Bitmask

Status
Not open for further replies.
Heya guys,

Recently i have been extremely busy with personal things, so i thought I would drop in and create a tutorial for you guys to wrap your head around.

Firstly, Binary, Bitwise,Bits,Maths, Integers, Bit masks.. These words may be slightly technical but if there not really hard to learn.

Firstly let me explain what they do.

A Bit mask is a combination of binary data in a logical algorithm., hmm little too much, Basically we have a a series of 0's and 1's and we shift them about to make a unique combination

Firstly we need to set a series of Bits, each but should increment double of itself, so if you have 128 then the next should be 256 and so on, a little like ram.

So lets create a small interface holding some constants for our userPermissions:


PHP:
interface IUserPermissions
{
    /*
        * Public VIEW,CREATE,UPDATE,DELETE
    */
    const PUBLIC_VIEW    = 1;
    const PUBLIC_CREATE    = 2;
    const PUBLIC_EDIT    = 4;
    const PUBLIC_DELETE    = 8;
    const PUBLIC_GLOBAL    = 15; //1 | 2 | 4 | 8;
    
    /*
        * Admin  VIEW,CREATE,UPDATE,DELETE
    */
    const ADMIN_VIEW    = 16;
    const ADMIN_CREATE    = 32;
    const ADMIN_EDIT    = 64;
    const ADMIN_DELETE    = 128;
    const ADMIN_GLOBAL    = 192; //16 | 32 | 64 | 128;
    
    const SUPER_USER    = 255; //1 | 2 | 4 | 8 | 16 | 32 | 64 | 128;
}


The following PUBLIC_GLOBAL,ADMIN_GLOBAL are the values of there previous constants combined, this way thay hold a unique value for all there constants for that area.

  • PUBLIC_GLOBAL = PUBLIC_VIEW + PUBLIC_CREATE + PUBLIC_EDIT + PUBLIC_DELETE
  • ADMIN_GLOBAL = ADMIN_VIEW + ADMIN_CREATE + ADMIN_EDIT + ADMIN_DELETE
  • SUPER_USER = PUBLIC_GLOBAL + ADMIN_GLOBAL

Ok so that out the way we can go ahead and create the main class:


PHP:
class UserPermissions implements IUserPermissions
{
    private $Mask = 0;
    
    public function __construct($Mask = 0)
    {
        $this->Mask = $Mask;
    }
    
    public function InvokePermission($Bit)
    {
        return $this->Mask & $Bit;
    }
    
    public function RevokePermission($Bit)
    {
        $this->Mask &= ~$Bit;
    }
    
    public function AssignPermission($Bit)
    {
        $this->Mask |= $Bit;
    }
    
    public function GetStorableMask()
    {
        return decbin($this->Mask);
    }
}


there's 5 methods in the above class, below is what there used for:

  • __construct : used when you first initiate the class, and can set a mask from the database of a user
  • InvokePermission : use to check if a user has got permissions for a certain Bit.
  • RevokePermission : Removes the permission bit from the mask
  • AssignPermission : Add a permission bit to the mask.
  • GetStorableMask : returns a binary string to store within the database as Binary.

There several peices of code within these methods that are called Bitwise Operators, you can read about them here http://php.net/manual/en/language.operators.bitwise.php

Ok so an example:

PHP:
$Guest = new UserPermissions(UserPermissions::PUBLIC_VIEW);

What the above code does is create a permission set that only has the permission PUBLIC VIEW., if you wanted to extend the permission you can do the following:

PHP:
$Guest ->AssignPermission(UserPermissions::PUBLIC_DELETE);

and so forth, a nice example of how these permissions are affective is, above where we combined PUBLIC_GLOBAL and ADMIN_GLOBAL to create SUPER_USER, the super user inherits all permissions from public and admin, so doing the following:


PHP:
$SuperUser = new UserPermissions(UserPermissions::SUPER_USER);

if($SuperUser->InvokePermission(UserPermissions::PUBLIC_VIEW))
{
    echo "Can View";

}


Hope you enjoyed this and if your thinking about implementing such thing then just reply to this thread and i should be able to help you.
 

1 comment

Status
Not open for further replies.

About the author

L
Senior Member · Joined
I am a skilled programmer within PHP,HTML,CSS,MYSQL and also the owner of several websites Programming, Football...
1,367
Messages
1
Reactions
38
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom