Advertisement
RalfEggert

ACL-Service

Dec 13th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2. namespace User\Acl;
  3.  
  4. use Zend\Cache\Storage\Adapter\Filesystem;
  5. use Zend\Permissions\Acl\Acl;
  6.  
  7. class Service
  8. {
  9.     protected $role = 'guest';
  10.  
  11.     protected $config = array();
  12.  
  13.     protected $cache = null;
  14.    
  15.     protected $acl = null;
  16.  
  17.     protected $cacheKey = 'application_acl';
  18.  
  19.     public function __construct($role = 'guest', array $config, Filesystem $cache)
  20.     {
  21.         $this->setRole($role);
  22.         $this->setConfig($config);
  23.         $this->setCache($cache);
  24.        
  25.         $this->acl = $this->factory();
  26.     }
  27.  
  28.     public function getRole()
  29.     {
  30.         return $this->role;
  31.     }
  32.    
  33.     public function setRole($role)
  34.     {
  35.         $this->role = $role;
  36.         return $this;
  37.     }
  38.    
  39.     public function getConfig()
  40.     {
  41.         return $this->config;
  42.     }
  43.    
  44.     public function setConfig(array $config)
  45.     {
  46.         $this->config = $config;
  47.         return $this;
  48.     }
  49.    
  50.     public function getCache()
  51.     {
  52.         return $this->cache;
  53.     }
  54.    
  55.     public function setCache(Filesystem $cache)
  56.     {
  57.         $this->cache = $cache;
  58.         return $this;
  59.     }
  60.    
  61.     public function getAcl()
  62.     {
  63.         return $this->acl;
  64.     }
  65.    
  66.     public function setAcl(Acl $acl)
  67.     {
  68.         $this->acl = $acl;
  69.         return $this;
  70.     }
  71.    
  72.     public function factory()
  73.     {
  74.         // try to read acl from cache
  75.         $acl = $this->getCache()->getItem($this->cacheKey);
  76.        
  77.         // check cache
  78.         if (!$acl) {
  79.             // create acl
  80.             $acl = new Acl();
  81.            
  82.             // loop through role data
  83.             foreach ($this->config as $role => $resources) {
  84.                 // check for role
  85.                 if (!$acl->hasRole($role)) {
  86.                     $acl->addRole($role);
  87.                 }
  88.            
  89.                 // loop through resource data
  90.                 foreach ($resources as $resource => $rules) {
  91.                     // check for resource
  92.                     if (!$acl->hasResource($resource)) {
  93.                         $acl->addResource($resource);
  94.                     }
  95.            
  96.                     // loop trough rules
  97.                     foreach ($rules as $rule => $privileges)
  98.                     {
  99.                         // add rule with privileges
  100.                         $acl->$rule($role, $resource, $privileges);
  101.                     }
  102.                 }
  103.             }
  104.            
  105.             // store in cache
  106.             $this->getCache()->setItem($this->cacheKey, $acl);
  107.         }
  108.        
  109.         // pass acl
  110.         return $acl;
  111.     }
  112.    
  113.     public function isAllowed($resource, $privilege)
  114.     {
  115.         // check resource
  116.         if (empty($resource) || !$this->getAcl()->hasResource($resource)) {
  117.             return false;
  118.         }
  119.        
  120.         // check privilege
  121.         if (empty($privilege)) {
  122.             return false;
  123.         }
  124.        
  125.         // check acl
  126.         return $this->getAcl()->isAllowed($this->getRole(), $resource, $privilege);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement