ZF2 Generate navigation using zfcrbac zfcUser and hierarchical role strategy

烈酒焚心 提交于 2019-12-06 04:30:12

I'll show you how ZfcRbac can work with (ZF2) Zend/Navigation. You have definition of permissions in database, that's why I'll omit this section.

Define your navigation adding pages and permissions:

config/global.phpPHP:

return array(
        'navigation' => array(
            'default' => array(
                array(
                    'label' => 'Contracts',
                    'route' => 'contract',
                    'action' => 'list',
                    'permission' => 'contract.list',
                    'pages' => array(
                         array(
                            'label'  => 'New contract',
                            'route'  => 'contract',
                            'action' => 'add',
                            'permission' => 'contract.add',
                         )
                     )
                )
            )
        ),
        'service_manager' => array(
            'factories' => array(
                'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            )
        )
    );

Create a Listener (/module/Application/src/Application/Authorization/RbacListener.php):

<?php

namespace Application\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 (is_null($permission)) {
            $event->stopPropagation();
            return false;
        }

        $event->stopPropagation();

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

Create a Factory for the RbacListener (/module/Application/src/Application/Factory/RbacListenerFactory.php):

<?php

namespace Application\Factory;

use Application\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);
    }
}

Add your RbacListenerFactory to your ServiceManager (/module/Application/config/module.config.php):

<?php

return array(
    'service_manager' => array(
        'factories' => array(
            'Application\Authorization\RbacListener' => 'Application\Factory\RbacListenerFactory',
        ),
    ),
);

Attach an event to the isAllowed method of the Zend Navigation View Helper (And finally attach an event to the isAllowed method of the Zend Navigation View Helper):

<?php

public function onBootstrap(MvcEvent $event)
{
    $application        = $event->getApplication();
    $eventManager       = $application->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager;
    $serviceManager     = $application->getServiceManager();
    $rbacListener       = $serviceManager->get('Application\Authorization\RbacListener');

    $sharedEventManager->attach(
        'Zend\View\Helper\Navigation\AbstractHelper',
        'isAllowed',
        array($rbacListener, 'accept')
    );
}

To render menu in view or layout:

<?php echo $this->navigation('navigation')->menu(); ?>

I'm using this code and it works quite well. It's based on:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!