这是我的module.config.php
class Module
{
protected $whitelist = array(
'home',
'user/login',
'job/view',
);
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$serviceManager = $e->getApplication()->getServiceManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
// Authentication service
$auth = $serviceManager->get('AuthService');
// Authorization (ACL)
$this->initAcl($e);
$eventManager->attach(MvcEvent::EVENT_ROUTE, function($e) use ($auth) {
$route = $e->getRouteMatch()->getMatchedRouteName();
//$auth->getIdentity();
// Retrieve logged in user role
$userRole = $auth->getIdentity()->level;
if ($userRole == '1')
{
$userRole = 'admin';
echo "admin";
}
elseif ($userRole == '2'){
$userRole = 'manager';
//echo "manager";
}
elseif ($userRole == '3'){
$userRole = 'hrstaff';
echo "hrstaff";
}
// $userRole = ($auth->getIdentity()->level === '1') ? 'admin' : 'manager';
if ($e->getViewModel()->acl->hasResource($route) && !$e->getViewModel()->acl->isAllowed($userRole, $route)) {
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/404');
$response->setStatusCode(303);
}
});
$list = $this->whitelist;
$eventManager->attach(MvcEvent::EVENT_ROUTE, function($e) use ($list, $auth) {
$match = $e->getRouteMatch();
// // No route match, this is a 404
// if (!match instanceof RouteMatch) {
// return;
// }
// Route is whitelisted
$name = $match->getMatchedRouteName();
if (in_array($name, $list)) {
return;
}
// User is authenticated
if ($auth->hasIdentity()) {
return;
}
$router = $e->getRouter();
$url = $router->assemble(array(), array(
'name' => 'user/login'
));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, -100);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function initAcl(MvcEvent $e)
{
$acl = new \Zend\Permissions\Acl\Acl();
$roles = include __DIR__ . '/config/module.acl.roles.php';
$allResources = array();
foreach ($roles as $role => $resources) {
$role = new \Zend\Permissions\Acl\Role\GenericRole($role);
$acl->addRole($role);
$allResources = array_merge($resources, $allResources);
// Adding resources
foreach ($resources as $resource) {
$acl->addResource(new \Zend\Permissions\Acl\Resource\GenericResource($resource));
}
// Adding restrictions
foreach ($allResources as $resource) {
$acl->allow($role, $resource);
}
}
$e->getViewModel()->acl = $acl;
}
}
这是我的module.acl.roles.php
<?php
return array(
'manager' => array(
'home',
'job',
'user/password',
),
'admin' => array(
),
'hrstaff' => array(
'user',
'user/add',
),
);
?>
之前只有2个角色,Manager和Admin然后我尝试添加hrstaff角色并尝试在阵列上添加模块但不幸的是它没有效果,hrstaff只继承了Manager和I的角色不知道为什么,然后我尝试将Admin的模块剪切并粘贴到hrstaff进行测试,但它也没有效果,结果仍然相同。我错过了什么吗?在此先感谢!