Created
February 29, 2012 12:10
-
-
Save prolic/1940408 to your computer and use it in GitHub Desktop.
ACL Listener
This file contains hidden or 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 | |
namespace Application\Event; | |
use Zend\EventManager\StaticEventManager, | |
Zend\EventManager\EventDescription, | |
Application\Event\Exception\ForbiddenException, | |
Application\Event\Exception\UnexpectedValueException, | |
Humus\Di\Locator; | |
class AclListener implements LocatorAware | |
{ | |
/** | |
* @var \Humus\Di\Locator | |
*/ | |
protected $locator; | |
/** | |
* @var array | |
*/ | |
protected $options; | |
/** | |
* The constructor | |
* | |
* @param array $options | |
*/ | |
public function __construct(array $options = array()) | |
{ | |
$this->options = $options; | |
} | |
/** | |
* Set the locator | |
* | |
* @param \Humus\Di\Locator $locator | |
* @return void | |
*/ | |
public function setLocator(Locator $locator) | |
{ | |
$this->locator = $locator; | |
} | |
/** | |
* Get the locator | |
* | |
* @return \Humus\Di\Locator | |
*/ | |
public function getLocator() | |
{ | |
return $this->locator; | |
} | |
/** | |
* Attach one or more listeners | |
* | |
* @return void | |
*/ | |
public function attach() | |
{ | |
$sevm = StaticEventManager::getInstance(); | |
foreach ($this->options as $context => $events) { | |
foreach ($events as $event) { | |
$sevm->attach($context, $event, array($this, 'validate'), 200); | |
} | |
} | |
} | |
/** | |
* @param \Zend\EventManager\EventDescription $e | |
* @throws Exception\ForbiddenException | |
*/ | |
public function validate(EventDescription $e) | |
{ | |
if (!$this->acl()->isAllowed($this->role(), $e->getTarget(), $e->getName())) { | |
throw new ForbiddenException(); | |
} | |
} | |
/** | |
* Get the acl | |
* | |
* @throws Exception\UnexpectedValueException | |
* @return \Zend_Acl | |
*/ | |
protected function acl() | |
{ | |
$acl = $this->getLocator()->get('Acl'); | |
if (!$acl instanceof \Zend_Acl) { | |
throw new UnexpectedValueException('No acl found in locator.'); | |
} | |
return $acl; | |
} | |
/** | |
* Get the role | |
* | |
* @throws Exception\UnexpectedValueException | |
* @return \Zend_Acl_Role_Interface | |
*/ | |
protected function role() | |
{ | |
$role = $this->getLocator()->get('CurrentUser'); | |
if (!$role instanceof \Zend_Acl_Role_Interface) { | |
throw new UnexpectedValueException('No role found in locator.'); | |
} | |
return $role; | |
} | |
} | |
// usage: | |
$locator; // instance of Humus\Di\Locator | |
$listener = new \Application\Event\AclListener(array( | |
'Application\Service\User' => array( | |
'findAll', | |
'findById', | |
'delete', | |
'create', | |
'edit' | |
))); | |
$listener->setLocator($locator); | |
$listener->attach(); | |
namespace Application\Service; | |
class User | |
{ | |
// some code | |
public function edit(array $data) | |
{ | |
$params = compact('data'); | |
$results = $this->events()->trigger(__FUNCTION__, $this, $params); | |
// some code | |
} | |
// some code | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment