Activity Stream
48,167 MEMBERS
6774 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 2 of 2
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default User Permissions using Bitwise / Bitmask

    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 Code: 
    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 Code: 
    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 Code: 
    $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 Code: 
    $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 Code: 
    $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.
    litewarez Reviewed by litewarez on . User Permissions using Bitwise / Bitmask 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 Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Banned
    Website's:
    TechHive.iNFO
    Nice one... Thanks!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to give a normal user root permissions
    By Webmin in forum Server Management
    Replies: 7
    Last Post: 6th Apr 2012, 06:52 PM
  2. Ubuntu User/Folder Permissions
    By Sp32 in forum Technical Help Desk Support
    Replies: 1
    Last Post: 12th Apr 2011, 06:39 PM
  3. Forum permissions...
    By Memphis in forum IP.Board
    Replies: 2
    Last Post: 18th Oct 2008, 01:11 AM

Tags for this Thread

BE SOCIAL