Last active
December 16, 2015 16:40
-
-
Save mexitek/5464905 to your computer and use it in GitHub Desktop.
Sample class for bitmasked permissions
This file contains hidden or 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 | |
/** | |
* @author Arlo Carreon | |
* | |
*/ | |
class Permissions { | |
// Bitmasks | |
const REPORTS = 1; | |
const QUIZES = 2; | |
const CONTROL_PANEL = 4; | |
const GROUPS = 8; | |
const USERS = 16; | |
const DEPARTMENTS = 32; | |
// User role | |
private static $_role = null; | |
// User Role Definitions | |
private static $_allRoles = NULL; | |
public static function setRole( $role ) { | |
self::$_allRoles = array( | |
"Learner" => self::QUIZES , | |
"Reporter" => self::REPORTS + self::GROUPS, | |
"Trainer" => self::CONTROL_PANEL + self::GROUPS + self::USERS , | |
"Administrator" => self::CONTROL_PANEL + self::QUIZES + self::USERS + self::GROUPS + self::DEPARTMENTS | |
); | |
self::$_role = array_key_exists($role,self::$_allRoles) ? $role:"learner"; | |
} | |
/** | |
* Validates a permission against the user | |
* @param const $permission A value from the bitmasks | |
* @return boolean Whether the user has permission to given permission | |
*/ | |
public static function has( $permission ) { | |
return (self::$_allRoles[self::$_role] & $permission); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment