Results 1 to 2 of 2
-
6th Dec 2010, 08:04 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comUser 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);
PHP Code:$Guest ->AssignPermission(UserPermissions::PUBLIC_DELETE);
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: 5Join 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
-
7th Dec 2010, 02:24 AM #2BannedWebsite's:
TechHive.iNFONice one... Thanks!!!
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
How to give a normal user root permissions
By Webmin in forum Server ManagementReplies: 7Last Post: 6th Apr 2012, 06:52 PM -
Ubuntu User/Folder Permissions
By Sp32 in forum Technical Help Desk SupportReplies: 1Last Post: 12th Apr 2011, 06:39 PM -
Forum permissions...
By Memphis in forum IP.BoardReplies: 2Last Post: 18th Oct 2008, 01:11 AM
themaRegister - register to forums...
Version 3.54 released. Open older version (or...