/ ** Creating Roles * /
require_once 'Zend / Acl / Role.php';
$ myAcl-> addRole (new Zend_Acl_Role ('guest'))
-> addRole (new Zend_Acl_Role ('writer'), 'guest')
-> addRole (new Zend_Acl_Role ('admin'), 'writer');
/ ** Creating resources * /
require_once 'Zend / Acl / Resource.php';
/ ** Default module * /
$ myAcl-> add (new Zend_Acl_Resource ('user'))
-> add (new Zend_Acl_Resource ('article'));
/ ** Admin module * /
$ myAcl-> add (new Zend_Acl_Resource ('admin'))
-> add (new Zend_Acl_Resource ('admin: article', 'admin'))
-> add (new Zend_Acl_Resource ('admin: quick-link', 'admin'))
-> add (new Zend_Acl_Resource ('admin: category', 'admin'));
/ ** Creating permissions * /
$ myAcl-> allow ('guest', 'user')
-> deny ('guest', 'article')
-> allow ('guest', 'article', 'view')
-> allow (array ('writer', 'admin'), 'article', array ('add', 'edit'))
-> allow ('admin', 'admin');
/ ** Setting up the front controller * /
require_once 'Zend / Controller / Front.php';
$ front = Zend_Controller_Front :: getInstance ();
$ front-> setControllerDirectory (array ('default' => 'path / to / default / controllers',
'admin' => 'path / to / admin / controllers'));
/ ** Registering the Plugin object * /
require_once 'Zend / Controller / Plugin / Acl.php';
$ front-> registerPlugin (new Zend_Controller_Plugin_Acl ($ myAcl, 'guest'));
/ ** Dispatching the front controller * /
$ front-> dispatch (); class UserController extends Zend_Controller_Action
{
protected $ _application;
public function init ()
{
require_once 'Zend / Session / Namespace.php';
$ this -> _ application = new Zend_Session_Namespace ('myApplication');
}
public function loginAction ()
{
... Validation code
if ($ valid) {
/ ** Setting role into session * /
$ this -> _ application-> currentRole = $ user-> role;
$ this -> _ application-> loggedUser = $ user-> username;
}
}
public function logoutAction ()
{
$ this -> _ application-> currentRole = 'guest';
$ this -> _ application-> loggedUser = null;
}
} / ** Loading application from session * /
require_once 'Zend / Session / Namespace.php';
$ application = new Zend_Session_Namespace ('myApplication');
if (! isset ($ application-> currentRole)) {
$ application-> currentRole = 'guest';
}
/ ** Setting up the front controller * /
require_once 'Zend / Controller / Front.php';
$ front = Zend_Controller_Front :: getInstance ();
$ front-> setControllerDirectory ('path / to / controllers');
/ ** Registering the Plugin object * /
require_once 'Zend / Controller / Plugin / Acl.php';
$ front-> registerPlugin (new Zend_Controller_Plugin_Acl ($ myAcl, $ application-> currentRole));
/ ** Dispatching the front controller * /
$ front-> dispatch (); / ** Setting up the front controller * /
require_once 'Zend / Controller / Front.php';
$ front = Zend_Controller_Front :: getInstance ();
$ front-> setControllerDirectory ('path / to / controllers');
/ ** Setting default access denied action * /
require_once 'Zend / Controller / Plugin / Acl.php';
$ aclPlugin = new Zend_Controller_Plugin_Acl ($ myAcl, 'guest');
$ aclPlugin-> setErrorPage ('goaway', 'my-error-controller', 'my-module');
/ ** Registering the Plugin object * /
$ front-> registerPlugin ($ aclPlugin);
/ ** Dispatching the front controller * /
$ front-> dispatch (); / ** Loading application from session * /
require_once 'Zend / Session / Namespace.php';
$ application = new Zend_Session_Namespace ('myApplication');
if (! isset ($ application-> loggedUser)) {
$ application-> loggedUser = null;
}
/ ** Setting up the front controller * /
require_once 'Zend / Controller / Front.php';
$ front = Zend_Controller_Front :: getInstance ();
$ front-> setControllerDirectory ('path / to / controllers');
/ ** Registering the Plugin object * /
require_once 'Zend / Controller / Plugin / Acl.php';
$ front-> registerPlugin (new Zend_Controller_Plugin_Acl ($ myAcl, $ application-> currentRole));
/ ** Registering the Action Helper object * /
require_once 'Zend / Controller / Action / Helper / Acl.php';
require_once 'Zend / Controller / Action / HelperBroker.php';
Zend_Controller_Action_HelperBroker :: addHelper (new Zend_Controller_Action_Helper_Acl ());
/ ** Dispatching the front controller * /
$ front-> dispatch (); class ArticleController extends Zend_Controller_Action
{
protected $ _acl;
protected $ _application;
public function init ()
{
/ ** Get our Action Helper * /
$ this -> _ acl = $ this -> _ helper-> getHelper ('acl');
require_once 'Zend / Session / Namespace.php';
$ this -> _ application = new Zend_Session_Namespace ('myApplication');
}
...
public function editAction ()
{
/ ** Load article by id * /
$ article = new Article ($ this -> _ request-> id);
/ ** Validate if Admin
if (($ article-> author! = $ this -> _ application-> loggedUser)
&& ($ this -> _ application-> currentRole! = 'admin')) {
$ this -> _ acl-> denyAccess ();
}
...
}
} Source: https://habr.com/ru/post/31644/
All Articles