0

我有模块,建立我的ACL树工作正常。ZF2重定向,如果ACL不允许访问页面

我还在config/autoload目录中有一个导航配置文件,它详细说明了我的应用程序结构以及与条目相关的资源。我的应用程序模块配置中也有一个导航工厂。

所有这一切都工作正常,我正在渲染我的菜单基于登录用户的角色和导航配置页面上的资源的权限。

我无法解决的是如何防止访问用户无法访问的页面(隐藏在呈现的导航菜单中的页面)。我希望在模块中进行管理。我假设在我的Module.php文件中的onBootstrap函数,我需要运行isAllowed对ACL和重定向(如在这个问题 - Forward to another controller/action from module.php)。 isAllowed似乎需要资源来查询,但是。这需要从导航配置中获得。

如果我硬编码isAllowed函数所需的资源,我可以得到这个工作。实际上,我只需要从导航配置中获取当前页面请求的资源。

我确定这必须是标准功能,但我找不到任何具体的例子。

任何帮助表示赞赏。

克里斯

回答

0

这是你正在寻找的东西,或者是你在寻找如何从onBootstrap方法中访问你的配置?

public function onBootstrap($event) { 
    $matched_route = $event->getRouteMatch()->getMatchedRouteName(); 
    $someOtherClass = new MyClassThatAuthenticatesRoutes(); 
    if(!($someOtherClass->isAllowed($matched_route)){ 
     $response = $event->getResponse(); 
     $response->setStatusCode(401); 
     $response->setReasonPhrase('Not allowed!'); 
     return $response; 
    } 

如果你正在寻找的只是配置,你可以去:

$sm = $e->getApplication()->getServiceManager(); 
$config = $sm->get('config'); 
+0

嗨。感谢您的回复。对不起,如果我的措辞不是最好的,但那是我有效的地方。 因此,在$ someOtherClass-> isAllowed需要获取该匹配路径(或父母)中当前页面的资源,以评估用户是否被允许访问该页面。 功能已经可以在面包屑视图助手的组合中找到,它可以从导航cfg中找到我的当前页面,菜单视图助手可以根据用户的角色和该页面的资源隐藏菜单中的某些页面。 我假设会有一种方法来实现这一点。 –

0

如果你需要为ACL路由匹配考虑做这样的事情:

/** 
* Retrieve the route match 
* 
* @return string 
*/ 
protected function getMatchRoute() 
{ 
    $router = $this->getServiceLocator()->get('router'); 
    $request = $this->getServiceLocator()->get('request');  

    $this->routeMatch = $router->match($request)->getMatchedRouteName(); 

    return $this->routeMatch; 
} 

然后在你的控制器中:

// note, $acl is just a class I wrote to extend class Zend\Permissions\Acl\Acl 
// because I needed additional functionality  
$acl = new \PATH_TO\Acl\Acl(); 

// checkAcl(), just however you plan on handling permissions 
// $role is obviously just that, the role of the user, where ever 
// you are setting that. 
// the second param is from the method in the above code block which is the 
// resource (page) you are wanting to check against 
$access = $acl->checkAcl($role, $this->getMatchRoute()); 

// they don't have access so redirect them 
if (!$access) 
{ 
    return $this->redirect()->toRoute('your_route', array()); 
} 

如果你需要看到更多的代码,请让我知道,但希望这可以让你开始。

相关问题