【问题标题】:Zend_ACL with modular structure?Zend_ACL 具有模块化结构?
【发布时间】:2010-12-22 23:36:45
【问题描述】:
【问题讨论】:
标签:
zend-framework
acl
modular
【解决方案1】:
绝对是。这就是我们在项目中所做的。我们验证 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"
【解决方案2】:
Ivan 的其他选项是设置资源而不是“控制器”。像“模块控制器”。
【解决方案3】:
有可能,我每次都用它。
首先要记住 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
}
}