Skip to content

Instantly share code, notes, and snippets.

@zachflower
Last active December 28, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachflower/7557194 to your computer and use it in GitHub Desktop.
Save zachflower/7557194 to your computer and use it in GitHub Desktop.
PHP Authorization Class
<?php
/**
* PHP Authorization Class
*
* Authorization class for PHP.
* Usage Example:
* $can = new Can();
* $can->user = $user;
*
* // DECLARED FUNCTION
* if ( $can->post() ) {
* // AUTHENTICATED USER CAN POST
* } else {
* // UNAUTHENTICATED USER CAN'T POST
* }
*
* // UNDECLARED FUNCTION
* if ( $can->whistle() ) {
* // ONLY EMPLOYEES, VIPS, AND BETA USERS CAN ACCESS THIS FUNCTION
* } else {
* // EVERYONE ELSE IS DENIED ACCESS
* }
*/
class Can {
private $data = array();
public function __call($name, $arguments) {
return $this->__beta();
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
private function __beta(){
if ( !$this->__authenticated() ){
return FALSE;
}
switch ( $this->data['user']->access_level ) {
case 'employee':
case 'vip':
case 'beta':
return TRUE;
default:
return FALSE;
}
return FALSE;
}
private function __authenticated(){
if ( empty($this->data['user']) ) {
return FALSE;
}
return TRUE;
}
public function post(){
if ( $this->__authenticated() ){
return TRUE;
}
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment