Skip to content

Instantly share code, notes, and snippets.

@gkralik
Last active August 29, 2015 14:05
Show Gist options
  • Save gkralik/b78d1d6d7f95641c68f8 to your computer and use it in GitHub Desktop.
Save gkralik/b78d1d6d7f95641c68f8 to your computer and use it in GitHub Desktop.
<?php
/**
* Attach in Module::onBootstrap()
* <code>
* $navigationRbacListener = $target->getServiceManager()->get('NavigationRbacListener');
* $target->getEventManager()->getSharedManager()->attach(
* 'Zend\View\Helper\Navigation\AbstractHelper',
* 'isAllowed',
* [$navigationRbacListener, 'hasPermission']
* );
* </code>
*/
namespace Application\Listener;
use Zend\EventManager\Event;
use Zend\Navigation\Page\AbstractPage;
use ZfcRbac\Service\AuthorizationService;
/**
* Inject AuthorizationService via Factory
*/
class NavigationRbacListener
{
/** @var AuthorizationService */
private $_authorizationService;
public function __construct(AuthorizationService $authorizationService)
{
$this->_authorizationService = $authorizationService;
}
public function hasPermission(Event $e)
{
$page = $e->getParam('page');
if (!$page instanceof AbstractPage) {
return true;
}
$permission = $page->getPermission();
if (null === $permission) {
return true;
}
$e->stopPropagation();
return $this->_authorizationService->isGranted($permission);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment