0

我有构建我的 ACL 树的模块,它工作正常。

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

所有这一切都工作正常,我正在根据登录用户角色的权限和导航配置中页面的资源来呈现我的菜单。

我无法解决的是如何防止访问用户无权访问的页面(隐藏在呈现的导航菜单中的页面)。我希望这可以在一个模块中进行管理。

我假设在我的 Module.php 文件中的 onBootstrap 函数中,我需要针对 ACL 运行 isAllowed 并重定向(如在这个问题中 - Forward to another controller/action from module.php)。然而,isAllowed 似乎需要查询资源。这需要从导航配置中获得。

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

我确定这一定是标准功能,但我找不到任何具体示例。

任何帮助表示赞赏。

克里斯

4

2 回答 2

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');
于 2014-04-11T21:21:02.243 回答
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());
}

如果您需要查看更多代码,请告诉我,但希望这可以帮助您入门。

于 2014-05-16T02:27:22.970 回答