I am trying to implement zend ACL in my project, i am facing three problems.
To explain the problems this is the code:
My library plugin class
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');
}
}
}
This is my ACL class in the models folder
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'));
}
}
This is the _initAppAutoload in the bootstarp
$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) the first problem is how can i specify in the Application_Model_DbTable_LibraryAcl that i have a modular implementation with admin and default folders or how can i create a tree of resources for each module?
2)i don’t have a default role in my database but i want to make this default user to have some previligaes without creating an account(That’s why i check the identity of the role and if it is none i set it to default). is that the best practice to do so or even logical ?
3) how can i check in my Mylib_Controller_Plugin_AccessCheck class in the _isAllowed method for the action too not just the module and the controller.?
Also this way of redirecting is also giving me an error of isn’t redirecting properly
Here goes,
You are pretty close already, for a basic implementation:
This is probably not perfect, but should give you the idea of what to do. you can test the results as you need.
Works for me, it’s really hard to check a users role ’til you know who they are. Check away.
Adding the action name to the
isAllowed()seems to work for my app, but the testing has not benn very extensive. So use with caution. I’m like you still trying to get my head all the way around these concepts.