3

我通过阅读http://framework.zend.com/manual/en/zend.controller.modular.html用 Zend 创建了一个 MVC 。

问题是我找不到使用模块化结构的 Zend_ACL 的方法。Zend_Acl 根本没有添加模块的方法。它只允许我添加控制器和动作。

如何将 Zend_Acl 与模块化结构一起使用?Zend 框架的当前版本甚至可能吗?

4

3 回答 3

2

绝对是。这就是我们在项目中所做的。我们验证 URI 路径 ( $request->getPathInfo()),例如:/admin/user/edit。这里“admin”是一个模块,“user”是一个控制器,“edit”是一个动作。我们有一个访问插件:

class Our_Application_Plugin_Access extends Zend_Controller_Plugin_Abstract {
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        foreach (self::current_roles() as $role) {
            if (
                Zend_Registry::get('bootstrap')->siteacl->isAllowed(
                    $role,
                    $request->getPathInfo()
                )
            ) return;
        }

        $this->not_allowed($request);
    }

   ...
}

在 application.ini 中注册:

resources.frontController.plugins.access = "Our_Application_Plugin_Access"
于 2009-12-04T08:38:39.390 回答
1

有可能,我每次都用。首先请记住Zend_Acl 将验证的资源是任意实体(字符串),与特定模块或控制器无关。它可以是字符串“hello”,在您的程序中,您可以检查用户是否可以访问资源“hello”。我经常使用一些任意资源作为“登录按钮”、“注销按钮”来显示 Zend_Navigation 中的链接。

在您的情况下,您应该将资源(在 acl 中)定义为一些可以映射到模块/控制器布局的字符串。例如对于模块 foo 和控制器 bar 定义资源“foo.bar”。在访问检查过程中,您将读取模块和控制器名称并将它们合并到一个字符串中以获取资源。

在一个实际的例子中:

class Application_Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {

...

public function preDispatch(Zend_Controller_Request_Abstract $request){
    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();

...

   $resource = $module . '.' . $controller; //we create the custom resource according to the model we have defined
...

    $role=NULL;
    if($this->_auth->hasIdentity()){
        $identity = $this->_auth->getStorage()->read(); //depending on your implementation
        $role = $identity->role; //depending on your implementation
    }
...

  if(!$this->_acl->isAllowed($role, $resource, $action)){
        //deny access       
    }
    //allow access
}
}
于 2011-09-27T17:57:02.340 回答
1

伊万的其他选择是将资源设置为“控制器”之外的资源。像“模块控制器”。

于 2009-12-04T14:16:58.853 回答