|
|
|
|
|
|
| |
This class uses a bit mask to represent user permissions.
This will allow you to store a singe integer representing a users permission set.
You can then covert this integer back into an associative array with boolean
values showing the users permission set.
@author Keith Ganger <ganger2@adelphia.net>
@version 1.0
@since March 3 2004
|
<?php
class permissions
{
/**
* This array is used to represent the users permission in usable format.
*You can change remove or add valuesto suit your needs.
*Just ensure that each element defaults to false. Once you have started storing
*users permsisions a change to the order of this array will cause the
*permissions to be incorectly interpreted.
* @type Associtive array
*/
var $permissions=array("read"=>false,"write"=>false,"delete"=>false,"change_permissions"=>false,"admin"=>false);
/**
*This function will use an integer bitmask(as created by toBitmask())
*to populate the class vaiable
*$this->permissions with the users permissions as boolean values.
*@param int $bitmask an integer representation of the users permisions.
*This integer is created by toBitmask();
*@return an associatve array with the users permissions.
*/
function getPermissions($bitMask=0){
/*
*The following explains how this code works.
*
*This table shows how bitmasks will represent a particular permission.
*element bin number -- 2^i -- decimal equiv
*read 00000001 -- 2^0 -- 1
*write 00000010 -- 2^1 -- 2
*delete 00000100 -- 2^2 -- 4
*
*
*
*The following code block loops through the permissions it uses a bitwise AND(&)
*along with the terinary operator to assign the permissions.
*You may want to visit the documentation at www.php.net on the pow function,
*the terinary operator, and the bitwise AND(&).
*When using the bitwise AND(&) all bits that are set in both $bitMask and the
*return value of pow(2($i) are set to 1.
*
*For this example refer to the above table
*A user with read and delete permissions would be represented as 00000101
*which would be an integer bitmask of 5
*when this code is executed the following would happen.
*The bitmask of 5 would be passed into the function
*so $bitmask is set to 5 or in binary(00000101).
*The first time through the loop the function pow(2,0) returns 1 (00000001)
*the bitwise AND(&) then compares
*(00000001) to (00000101) and returns 00000001 this is not equal to 0
*so the first element in the array "read" is set to true.
*
*The second time through the loop the function pow(2,1) returns 2 (00000010)
*the bitwise AND(&) then compares
*(00000010) to (00000101) and returns 00000000 this is equal to 0
*so the second element in the array "write" is set to false.
*
**The second time through the loop the function pow(2,2) returns 4 (00000100)
*the bitwise AND(&) then compares
*(00000100) to (00000101) and returns 00000100 this is 4 and is not equal to 0
*so the third element in the array "delete" is set to false.
*
*The code will loop through the remaining elements setting all of them to false.
*/
$i=0;
foreach($this->permissions as $key=>$value){
$this->permissions[$key]= (($bitMask & pow(2,$i)) !=0) ? true: false;
//uncomment the next line if you would like to see what is happening.
//echo $key . " i= ".strval($i)." power=" . strval(pow(2,$i)). "bitwise & = " . strval($bitMask & pow(2,$i))."<br>";
$i++;
}
return $this->permissions;
}
/**This function will create and return and integer bitmask based on the permission values set in
*the class variable $permissions. To use you would want to set the fields in $permissions to true for the permissions you want to grant.
*Then call toBitmask() and store the integer value. Later you can pass that integer into getPermissions() to convert it back to an assoicative
*array.
*@return int an integer bitmask represeting the users permission set.
*/
function toBitmask(){
$bitmask=0;
$i=0;
foreach($this->permissions as $key=>$value){
if($value){
$bitmask+=pow(2,$i);
}
$i++;
}
return $bitmask;
}
}
//this code demonstrates how to use the class
//instanciate the class.
$perms = new permissions();
//the first example shows how you would use the class to set permissions for a user
$perms->permissions["read"]=true;
$perms->permissions["delete"]=true;
$bitmask=$perms->toBitmask();
//echo "bitmask for read and delete= ". $bitmask ."<br>";
$sql ="insert into user_permissions (userid,docid,permission) values($userid,$docid,$bitmask)";
//you would then execute code to insert your sql.
//the second example shows you how to use the bitmask to get the users permissions.
//you would execute some sql to retrieve the bitmask from the database like
//select bitmask from user_permissions where docid=$docid and userid=$userid
//$bitmask = $row["bitmask"];
//I will just set this to a hardcoded value of 5 (as used in all the examples
$bitmask=5;
$permarr=$perms->getPermissions($bitmask);
if($permarr["read"]){
echo "read permissions<br>";
}
//print array to show values
print_r($permarr);
?> | | |
|
| A damaged image generator (class) for validating text.
CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart Categories : PHP, PHP Classes, Security, GD image library, Security | | | Simple Password example Categories : PHP, Authentication, Security, HTTP | | | Antispoof - a class to help prevent people hi-jacking and misusing parts of a website Categories : PHP, PHP Classes, Security | | | Scan Apache access log files and report possible worms attack Categories : PHP, PHP Classes, Security, Apache, Log Files | | | Scramble Eggs - php class to scramble/encode Categories : PHP, PHP Classes, Security, Encryption | | | MD5 secured login Categories : PHP, Java Script, Authentication, Security | | | A simple PHP login script that you can modify to suite your needs. It use a session to store data in a session file submited by the page. Categories : PHP, Sessions, Security, Authentication | | | Forms protected from XSS attacks (FOPAXSS) Categories : PHP, PHP Classes, Form Processing, Security | | | PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character. Categories : PHP, Algorithms, Security, Authentication, Encryption | | | Password using php, Javascript, and html form Categories : Security, PHP, Authentication, Java Script | | | Access_user Class - an easy to use system for protecting pages and register users. Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication | | | .htpassword manager for apache Categories : PHP, PHP Classes, Authentication, Apache | | | Password protection for Phorum 3.1.x with userlevels and log. Categories : PHP, MySQL, Authentication, Security | | | Simple and fast user authentication Categories : PHP, PHP Classes, Authentication | | | Form Security - Match A Value For Success Categories : PHP, Authentication, HTML and PHP, Sessions, Security | |
| | | | Jose Santos wrote : 1058
Permissions is very useful in UNIX / LINUX !!
So that this examnple, is very important, when the server is UNIX / LINUX, to change or get permissions.
In UNIX / LINUX, permissions are for example:
-rwxrwxrwx notes.txt => that to map over binary 111111111 or decimal 777
-rwxr-xr-x script => that to map over binary 111101101 or decimal 755
| | | | Jon Ellis-Jones wrote :1223
nicely done. thanks for the example code, it`s a useful representation.
| |
|
|