Da jeg alligevel havde en halv time jeg kunne undvære, tænkte jeg at dette kunne være rart med en opdatering på adgangskontrol...
Nedestående klasse er en simple access controller, som kan bruges til enormt mange ting. Der er naturligvis også plads til forbedringer.
- /**
- * ACL
- *
- * Simple Access Control Layer Class
- * Free to modify as you like.
- *
- * @author Michael Larsen
- * @link http://enia.dk
- */
- class ACL {
- /**
- * Rules construction container
- *
- * @var array
- */
- protected $access = null;
-
- /**
- * Rules container
- *
- * @var array
- */
- protected $rules = null;
-
- /**
- * Constructor
- *
- * @param array $access
- */
- public function __construct($access) {
- $this->access = $access;
- }
-
- /**
- * Returns true if role, resource and action is allowed.
- *
- * @param string $role
- * @param string $resource
- * @param string $action
- * @return boolean
- */
- public function isAllowed($role, $resource, $action) {
- if(!$this->rules) {
- $this->build();
- }
-
- return isset($this->rules[$role][$resource][$action]) && $this->rules[$role][$resource][$action] === true ? true : false;
- }
-
- /**
- * Builds the actual ruleset to validate against.
- */
- protected function build() {
- $builds = array();
-
- foreach($this->access as $role => $rules) {
- if(isset($rules['_inherit'])) {
- unset($rules['_inherit']);
- }
-
- foreach($rules as $resource => $actions) {
- $builds[$role][$resource] = $actions;
- }
-
- // Import inheritance
- $inheritance = isset($this->access[$role]['_inherit']) && isset($this->access[$this->access[$role]['_inherit']]) ? $this->access[$role]['_inherit'] : false;
- while($inheritance) {
- foreach($this->access[$inheritance] as $resource => $actions) {
- $builds[$role][$resource] = array_merge($actions,$builds[$role][$resource]);
- }
- $inheritance = isset($this->access[$inheritance]['_inherit']) && isset($this->access[$this->access[$inheritance]['_inherit']]) ? $this->access[$inheritance]['_inherit'] : false;
- }
- }
-
- $this->rules = $builds;
- }
- }
Herunder findes en lille demo af hvordan den kan benyttes:
- // Demo, initialize the ACL with access rules. This is the basic access for a blog.
- $acl = new ACL(array(
- 'user' => array( // visitor or user rules
- 'blog' => array(
- 'create' => false, 'read' => true, 'update' => false, 'delete' => false // users can only read.
- )
- ),
- 'admin' => array( // admin rules.
- '_inherit' => 'user', // inherit rules from "user".
- 'blog' => array(
- 'create' => true, 'update' => true, 'delete' => true // admin can do a full CRUD.
- )
- )
- ));
-
- // Just a few checks to demonstrate usage...
- echo $acl->isAllowed('user', 'blog', 'read') ? 'yes' : 'no'; // yes
- echo $acl->isAllowed('user', 'blog', 'update') ? 'yes' : 'no'; // no
- echo $acl->isAllowed('admin', 'blog', 'read') ? 'yes' : 'no'; // yes
- echo $acl->isAllowed('admin', 'blog', 'update') ? 'yes' : 'no'; // yes
Håber det kan bruges...
Indlæg senest redigeret d. 11.11.2011 14:55 af Bruger #10216