Skip to content

Instantly share code, notes, and snippets.

@mexitek
Last active December 16, 2015 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mexitek/5464905 to your computer and use it in GitHub Desktop.
Save mexitek/5464905 to your computer and use it in GitHub Desktop.
Sample class for bitmasked permissions
<?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