ZF2使用zfcrbac zfcUser和分层角色策略生成导航 [英] ZF2 Generate navigation using zfcrbac zfcUser and hierarchical role strategy
问题描述
我从zf2开始,我必须处理多对多"权限/角色.使用 ZfcRbac .
I begin in zf2 and i have to handle Many to Many permissions/roles. Using ZfcRbac.
所以我使用用户/层次角色(表名称=角色)/权限进行映射
So i make a mapping with User / Hierarchical Role (table name= role) / permissions
我让一些警卫拒绝政策.
And i make some guards with deny policy.
我的守卫,地图,数据库都还可以.
My guard, mapping, database, are ok.
我的HierarchicalRole实体看起来像:
My HierarchicalRole Entity looks like :
class HierarchicalRole implements HierarchicalRoleInterface
现在,它与Bakura给出的原始照片相同.
And it's, for now the same as the original given by Bakura.
我的用户实体看起来像这样:
My user Entity looks like this :
class User extends ZfcUserEntity implements IdentityInterface
与
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToMany(targetEntity="HierarchicalRole")
* @ORM\JoinTable(
* name="user_role_linker",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="idUser")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
和角色通过以下方式在构造函数中构建:
and roles are built in constructor by :
function __construct() {
$this->roles = new ArrayCollection();
}
使用zend开发人员工具,我可以看到ZfcRbac\Collector\RbacCollector
显示我想要的实际登录用户的所有信息(权限,子级,主要角色等).
With zend developper tools i can see that ZfcRbac\Collector\RbacCollector
show all i want for the actual logged user (permission, children, main role etc...).
我的问题是:如何生成动态导航,因为只有用户才能看到被授予访问权限的链接?.并在用户未登录时检查连接,并在用户登录时将其隐藏...
My question is : How to generate a dynamic navigation, for a user wich can only see links that is granted to access ?. And also check connexion when the user not logged and hide it when he's logged...
我还是一个新手,但是如果可能的话,最好有一个很好的示例说明,以便使用此模块进行良好的动态导航.
I'm a newbie yet, but if possible it would be great to have an example well explained for doing a good dynamic navigation using this module.
编辑(5月28日) 到目前为止,我一直在寻求解决方案,但是尝试还没有帮助我. 您可以在这里找到一个: 精美的导航 仍然不能完美运行.
EDIT (may 28) So far i always seek for a solution, my tries didn't helped me yet.. You can find one here : Spiffy navigation Still not working perfectly.
推荐答案
我将向您展示ZfcRbac如何与(ZF2)Zend/Navigation一起使用. 您已经在数据库中定义了权限,这就是为什么我将省略本节的原因.
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:
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',
)
)
);
创建一个侦听器(/module/Application/src/Application/Authorization/RbacListener.php):
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);
}
}
为RbacListener创建工厂(/module/Application/src/Application/Factory/RbacListenerFactory.php):
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);
}
}
将RbacListenerFactory添加到ServiceManager(/module/Application/config/module.config.php):
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',
),
),
);
将事件附加到Zend Navigation View Helper的isAllowed方法(最后将事件附加到Zend Navigation View Helper的isAllowed方法):
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:
http://puzzlingcodes.blogspot .com/2015/05/check-permissions-in-zf2-zendnavigation.html
这篇关于ZF2使用zfcrbac zfcUser和分层角色策略生成导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!