Zend framework zend acl模块化实现?
我试图在我的项目中实现zend ACL,我面临三个问题。 要解释这些问题,代码如下: 我的库插件类Zend framework zend acl模块化实现?,zend-framework,default,acl,roles,modular,Zend Framework,Default,Acl,Roles,Modular,我试图在我的项目中实现zend ACL,我面临三个问题。 要解释这些问题,代码如下: 我的库插件类 class Mylib_Controller_Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract { private $_acl = null; private $_auth = null; public function __construct(Zend_Acl $acl, Zend_Auth $aut
class Mylib_Controller_Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {
private $_acl = null;
private $_auth = null;
public function __construct(Zend_Acl $acl, Zend_Auth $auth) {
$this->_acl = $acl;
$this->auth = $auth;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$module = $request->getModuleName();
$recourse = $request->getControllerName();
$action = $request->getActionName();
$identity = $this->auth->getStorage()->read();
if (!isset($identity->Role)) {
$role = 'default';
} else {
$role = $identity->Role;
}
if (!$this->_acl->isAllowed($role, $module, $recourse)) {
$request->setModuleName('Admin')
->setControllerName('User')
->setActionName('index');
}
}
}
这是我在models文件夹中的ACL类
class Application_Model_DbTable_LibraryAcl extends Zend_Acl {
public function __construct() {
$this->addRole(new Zend_acl_Role('default'));
$this->addRole(new Zend_acl_Role('User'));
$this->addRole(new Zend_acl_Role('Admin'), 'User');
$this->add(new Zend_Acl_Resource('Admin'))
->add(new Zend_Acl_Resource('default'))
;
$this->allow('Admin')
->deny(array('User', 'default'));
}
}
这是bootstarp中的_initAppAutoload
$acl = new Application_Model_DbTable_LibraryAcl();
$auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->setControllerDirectory(array('default' => '/../application/modules/default/controllers',
'Admin' => '/../application/modules/Admin/controllers'));
$fc->registerPlugin(new Hyderlib_Controller_Plugin_AccessCheck($acl, $auth));
1) 第一个问题是,我如何在应用程序\模型\数据库\库ACL中指定我有一个带有管理和默认文件夹的模块化实现,或者如何为每个模块创建一个资源树
2) 我的数据库中没有默认角色,但我想让这个默认用户在不创建帐户的情况下拥有一些权限(这就是为什么我检查角色的标识,如果没有,我将其设置为默认)。这是这样做的最佳实践还是合乎逻辑
3) 我如何在允许的方法中签入Mylib_Controller_Plugin_AccessCheck类来执行操作,而不仅仅是模块和控制器
另外,这种重定向方式也给了我一个错误,就是没有正确重定向
1) 第一个问题是如何在
应用程序\模型\数据库\库ACL,我有一个
使用管理员和默认文件夹的模块化实现,或者如何为每个文件夹创建资源树
模块
对于基本实现,您已经非常接近了:
class Application_Model_DbTable_LibraryAcl extends Zend_Acl {
public function __construct() {
//add Roles
//default role has very limited access
$this->addRole(new Zend_acl_Role('default'));
//User inherits all default access
$this->addRole(new Zend_acl_Role('User'), 'default');
//Admin inherits all User and Default acces
$this->addRole(new Zend_acl_Role('Admin'), 'User');
//add resources, caps don't seem to be a problem.
//add Admin module resource
$this->add(new Zend_Acl_Resource('admin'));
//add Admin module Index controller resource, specify admin as parent
$this->add(new Zend_Acl_Resource('index'), 'admin');
//add default module access
$this->add(new Zend_Acl_Resource('default'));
//add access rules
//default in Zend_Acl is to deny all
//everyone has access to the front page and the error page
$this->allow(NULL, 'default', array('index', 'error'));
$this->allow(NULL, 'default', array('index', 'index'));
//add default user rules
//allow default access to login logout
$this->allow('default', 'default', array('login', 'logout'));
//add crud access for User
$this->allow('User', 'default', array('add', 'update'));
//admin can do all
$this->allow('Admin', NULL);
}
}
这可能并不完美,但应该让你知道该怎么做。您可以根据需要测试结果
2) 我在数据库中没有默认角色,但我想这样做
默认用户在不创建
帐户(这就是为什么我要检查角色的身份,如果没有
我将其设置为默认值)。这是这样做的最佳实践吗?甚至
合乎逻辑
对我来说,在你知道用户是谁之前,很难检查用户角色。结账
3) 如何签入Mylib\u控制器\u插件\u访问签入类
不只是模块和
控制器
将动作名称添加到isAllowed()
似乎对我的应用程序有效,但测试的范围并不广泛。所以要小心使用。我就像你一直在想这些概念
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$module = $request->getModuleName();
$recourse = $request->getControllerName();
$action = $request->getActionName();
$identity = $this->auth->getStorage()->read();
if (!isset($identity->Role)) {
$role = 'default';
} else {
$role = $identity->Role;
}
//default role is default, if role is not allowed and not set to default send to error controller.
if (!$this->_acl->isAllowed($role, $module, $recourse, $action)) {
if ($role == 'default'){
$request->setModuleName('default')
->setControllerName('index')
->setActionName('login');
} else {
$request->setModuleName('default')
->setControllerName('error')
->setActionName('noauth');
}