2015-05-02

Check permissions in ZF2 Zend/Navigation with ZfcRbac

ZfcRbac is not tailored to work with Zend/Navigation. There aren’t any builded native solutions that gives ZfcRbac permission to enable or disable entry of menu. The solution for that problem is quite simple.
I found solution in this blog: http://blog.webdevilopers.net/check-zend-navigation-page-permissions-with-zfcrbac/. I based on it. But this solution caused a problem. Positions in menu are displayed only if perrmision is defined. If permission is not defined, menu’s posiotin is displayed.
I’ll show you how to implement checking perrmisions and enabled / disabled menu positions with ZfcRbac. There is only one change in RbacListener:


class RbacListener
{
  protected $authorizationService;


  public function __construct(AuthorizationServiceInterface $authorizationService)
  {
      $this->authorizationService = $authorizationService;
  }


  public function accept(EventInterface $event)
  {
      $page = $event->getParam('page');


      if (! $page instanceof AbstractPage) {
          return;
      }


      $permission = $page->getPermission();


      if (is_null($permission)) {
          $event->stopPropagation();
          return false;
      }


      $event->stopPropagation();


      return $this->authorizationService->isGranted($permission);
  }
}

Brak komentarzy:

Prześlij komentarz