Last active
December 28, 2015 20:28
-
-
Save zachflower/7557194 to your computer and use it in GitHub Desktop.
PHP Authorization Class
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 | |
/** | |
* 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