当我实现 ACL 时,我创建了自己的 AUTH 模块用于授权和身份验证。
在该模块中,我创建了 ACL 插件.. 如下所示
//模块.php
/**
* This method is called once the MVC bootstrapping is complete
*
* @param \Zend\EventManager\EventInterface $e
*/
public function onBootstrap(Event $e)
{
$services = $e->getApplication()->getServiceManager();
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration'), 101);
}
/**
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function loadConfiguration(MvcEvent $e)
{
$e->getApplication()->getServiceManager()
->get('ControllerPluginManager')->get('AclPlugin')
->checkAcl($e); //Auth/src/Auth/Controller/AclPlugin
}
//Auth/src/Auth/Controller/AclPlugin
namespace Auth\Controller\Plugin;
/**
* load libraries here
*/
class AclPlugin extends AbstractPlugin implements ServiceManagerAwareInterface
{
/*
* @var Doctrine\ORM\EntityManager
*/
protected $em;
protected $sm;
/**
* @param Doctrine\ORM\EntityManager $em
* @return string
*/
public function checkAcl($e)
{
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
$action = $matches->getParam('action', 'index');
if ($acl->isAllowed($role, $resource, $permission)) {
return;
} else {
$matches->setParam('controller', 'Auth\Controller\User'); // redirect
$matches->setParam('action', 'accessdenied');
return;
}
}
/// rest of the code here
}