Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Felix-Johannes Jendrusch

By: a guest on Oct 4th, 2009  |  syntax: PHP  |  size: 1.86 KB  |  hits: 380  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     /**
  2.      * Excerpt from my ACL implementation based on registries for roles, resources
  3.      * and rules. Allows easy registry swapping (database, ...) and supports multiple
  4.      * role and resource inheritance
  5.      */
  6.     public function isAllowed($role = null, $resource = null, $privilege = null)
  7.     {
  8.         $roles = array();
  9.  
  10.         array_push($roles, null);
  11.  
  12.         if ($role !== null)
  13.             array_push($roles, $this->getRole($role));
  14.  
  15.         $resources = array();
  16.  
  17.         if ($resource !== null)
  18.             $resource = $this->getResource($resource);
  19.  
  20.         do {
  21.             $currentRole = array_pop($roles);
  22.  
  23.             array_push($resources, null);
  24.  
  25.             if ($resource !== null)
  26.                 array_push($resources, $resource);
  27.  
  28.             do {
  29.                 $currentResource = array_pop($resources);
  30.  
  31.                 foreach (array($privilege, null) as $currentPrivilege) {
  32.                     if (!$this->hasRule(null, $currentRole, $currentResource, $currentPrivilege))
  33.                         continue;
  34.  
  35.                     $rule = $this->getRule(null, $currentRole, $currentResource, $currentPrivilege);
  36.  
  37.                     if ($rule->assert($this, $role, $resource, $privilege))
  38.                         return ($rule->getType() === Example_Acl_Rule::TYPE_ALLOW);
  39.                 }
  40.  
  41.                 if ($currentResource === null)
  42.                     break;
  43.  
  44.                 foreach ($this->getResourceParents($currentResource) as $currentResourceParent)
  45.                     array_push($resources, $currentResourceParent);
  46.             } while (true);
  47.  
  48.             if ($currentRole === null)
  49.                 break;
  50.  
  51.             foreach ($this->getRoleParents($currentRole) as $currentRoleParent)
  52.                 array_push($roles, $currentRoleParent);
  53.         } while (true);
  54.  
  55.         return false;
  56.     }