Created
August 16, 2013 21:17
-
-
Save pumatertion/6253589 to your computer and use it in GitHub Desktop.
Bitmask Example
Read/Write/Execute/Delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* * | |
* This script belongs to the TYPO3 Flow framework. * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU Lesser General Public License, either version 3 * | |
* of the License, or (at your option) any later version. * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
/** | |
* Class BitMaskPermission | |
* | |
* @author Carsten Bleicker | |
*/ | |
class BitMaskPermission { | |
/** | |
* Bitmasks as decimal values | |
* 1 = 00000001 | |
* 2 = 00000010 | |
* 4 = 00000100 | |
* 8 = 00001000 | |
* 16 = 00010000 | |
* 32 = 00100000 | |
* 64 = 01000000 | |
* 128 = 10000000 | |
*/ | |
const READ = 1, WRITE = 2, EXECUTE = 4, DELETE = 8; | |
/** | |
* The current permission storage | |
* | |
* @var integer | |
*/ | |
private $storage; | |
/** | |
* Basicaly the Permission is 0 | |
* | |
* @return void | |
*/ | |
public function __construct() { | |
$this->storage = 0; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function grantRead() { | |
$this->addPermission(self::READ); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function denyRead() { | |
$this->removePermission(self::READ); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function grantWrite() { | |
$this->addPermission(self::WRITE); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function denyWrite() { | |
$this->removePermission(self::WRITE); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function grantExecute() { | |
$this->addPermission(self::EXECUTE); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function denyExecute() { | |
$this->removePermission(self::EXECUTE); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function grantDelete() { | |
$this->addPermission(self::DELETE); | |
return $this; | |
} | |
/** | |
* @return BitmaskPermission $this | |
*/ | |
public function denyDelete() { | |
$this->removePermission(self::DELETE); | |
return $this; | |
} | |
/** | |
* Adding a Permission | |
* | |
* @see http://php.net/manual/de/language.operators.bitwise.php | |
* @example The permission is actualy set to READ permission wich is representated by decimalvalue: 1 (see constants of BitMaskPermission::READ) | |
* and we want also want to add WRITE permission wich is a decimalvalue 2 | |
* 1 is in Binary 00000001 | |
* 2 is in Binary 00000010 | |
* What happens here is that we merge them together to the decimalvalue 3 by simply enable the 7th + 8th bit to: | |
* 3 is in Binary 00000011 wich is our for read+write access | |
* @param integer $bitmask | |
*/ | |
private function addPermission($bitMaskAsDecimal) { | |
$this->storage |= $bitMaskAsDecimal; | |
} | |
/** | |
* Removing a Permission | |
* | |
* @see http://php.net/manual/de/language.operators.bitwise.php | |
* @example The permission is actualy set to READ + WRITE permission wich is the decimalvalue 3 (BitMaskPermission::READ + BitMaskPermission::WRITE) (1 + 2) | |
* and we want also want to remove WRITE permission wich is a decimalvalue 2 | |
* 3 is in Binary 00000001 | |
* 2 is in Binary 00000010 | |
* What happens here is that we disable the 7th digit of decimalvalue 2 in our storage: | |
* 1 is in Binary 00000001 wich is our bitmask for read access | |
* @param integer $bitmask | |
*/ | |
private function removePermission($bitMaskAsDecimal) { | |
$this->storage &= ~$bitMaskAsDecimal; | |
} | |
} | |
$permission = new BitMaskPermission(); | |
print_r($permission->grantDelete()); | |
print_r($permission->grantRead()); | |
print_r($permission->grantWrite()); | |
print_r($permission->denyExecute()); | |
print_r($permission->denyRead()); | |
print_r($permission->grantExecute()); | |
?> | |
##### OUTPUT #### | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 8 | |
) | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 9 | |
) | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 11 | |
) | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 11 | |
) | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 10 | |
) | |
BitMaskPermission Object | |
( | |
[storage:BitMaskPermission:private] => 14 | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment