-
-
Save danizord/be431edeb9fa2581c63b to your computer and use it in GitHub Desktop.
This file contains 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 | |
public function onBootstrap(MvcEvent $event) | |
{ | |
$application = $event->getApplication(); | |
$eventManager = $application->getEventManager(); | |
$sharedEventManager = $eventManager->getSharedManager; | |
$serviceManager = $application->getServiceManager(); | |
$rbacListener = $serviceManager->get('MyApp\Authorization\RbacListener'); | |
$sharedEventManager->attach( | |
'Zend\View\Helper\Navigation\AbstractHelper', | |
'isAllowed', | |
array($rbacListener, 'accept') | |
); | |
} |
This file contains 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 | |
return array( | |
'service_manager' => array( | |
'factories' => array( | |
'MyApp\Authorization\RbacListener' => 'MyApp\Factory\RbacListenerFactory', | |
), | |
), | |
); |
This file contains 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 MyApp\Authorization; | |
use Zend\EventManager\EventInterface; | |
use Zend\Navigation\Page\AbstractPage; | |
use ZfcRbac\Service\AuthorizationServiceInterface; | |
class RbacListener | |
{ | |
/** | |
* @var AuthorizationServiceInterface | |
*/ | |
protected $authorizationService; | |
/** | |
* @param AuthorizationServiceInterface $authorizationService | |
*/ | |
public function __construct(AuthorizationServiceInterface $authorizationService) | |
{ | |
$this->authorizationService = $authorizationService; | |
} | |
/** | |
* @param EventInterface $event | |
* @return bool|void | |
*/ | |
public function accept(EventInterface $event) | |
{ | |
$page = $event->getParam('page'); | |
if (!$page instanceof AbstractPage) { | |
return; | |
} | |
$permission = $page->getPermission(); | |
if (null === $permission) { | |
return; | |
} | |
$event->stopPropagation(); | |
return $this->authorizationService->isGranted($permission); | |
} | |
} |
This file contains 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 MyApp\Factory; | |
use MyApp\Authorization\RbacListener; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class RbacListenerFactory implements FactoryInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
$authorizationService = $serviceLocator->get('ZfcRbac\Service\AuthorizationService'); | |
return new RbacListener($authorizationService); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment