Zend_Acl & Zend_Navigation

Set­ting up a sim­ple work­ing exam­ple of Acl & Nav­i­ga­tion in Zend Frame­work 1.9.x as demon­strated by jscherer26.
Enjoy

models/Acl.php

< ?php

class Model_Acl extends Zend_Acl {
pub­lic func­tion __construct() {

// define Roles
$this->addRole(new Zend_Acl_Role(‘guest’)); // not autheni­cated
$this->addRole(new Zend_Acl_Role(‘member’), ‘guest’); // authen­ti­cated as mem­ber inherit guest priv­i­lages
$this->addRole(new Zend_Acl_Role(‘admin’), ‘mem­ber’); // authen­ti­cated as admin inherit mem­ber privilages

// define Resources
$this->add(new Zend_Acl_Resource(‘error’));
$this->add(new Zend_Acl_Resource(‘index’));
$this->add(new Zend_Acl_Resource(‘authentication’));
$this->add(new Zend_Acl_Resource(‘activity’));

// assign priv­i­leges
$this->allow(‘guest’, array(‘index’,‘error’));
$this->allow(‘guest’, ‘authen­ti­ca­tion’, array(‘index’,‘signin’));

$this->allow(‘member’, ‘authen­ti­ca­tion’, array(‘index’,‘signout’));
$this->deny( ‘mem­ber’, ‘authen­ti­ca­tion’, ‘signin’);
$this->allow(‘member’, ‘activ­ity’, array(‘index’,‘list’)); // mem­ber has list priv­i­lages for resource activity

$this->allow(‘admin’, ‘activ­ity’); // admin has all priv­i­leges for resource activ­ity
}
}

plugins/Authenticated.php

< ?php
class Plugin_Authenticated extends Zend_Controller_Plugin_Abstract
{
private $_acl = null;
private $_auth = null;

pub­lic func­tion __construct(Zend_Acl $acl, Zend_Auth $auth)
{
$this->_acl = $acl;
$this->_auth = $auth;
}

pub­lic func­tion preDispatch(Zend_Controller_Request_Abstract $request)
{
$resource = $request->getControllerName();
$action = $request->getActionName();

$role .= $this->_auth->getStorage()->read()->role;
if(!$this->_acl->isAllowed($role, $resource, $action)) {
$request->setControllerName(‘authentication’)
->setActionName(‘notauthorized’);
}
}
}

Bootstrap.php

< ?php

class Boot­strap extends Zend_Application_Bootstrap_Bootstrap
{
pri­vate $_acl = null;
pri­vate $_auth = null;

pro­tected func­tion _initAutoload()
{
$mod­el­Loader = new Zend_Application_Module_Autoloader(array(
‘name­space’ => ”,
‘basePath’ => APPLICATION_PATH));

$this->_acl = new Model_Acl;
$this->_auth = Zend_Auth::getInstance();
if(!$this->_auth->hasIdentity()) {$this->_auth->getStorage()->read()->role = ‘guest’;}

$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_Authenticated($this->_acl, $this->_auth));

return $mod­el­Loader;
}

func­tion _initViewHelpers()
{
$this->bootstrap(‘layout’);
$lay­out = $this->getResource(‘layout’);
$view = $layout->getView();

$con­fig = new Zend_Config_Ini(APPLICATION_PATH .’/configs/application.ini’, APPLICATION_ENV);

$view->doctype(‘HTML4_STRICT’);
$view->headMeta()->appendHttpEquiv(‘Content-Type’, ‘text/html; charset=UTF-8′)
->appendHttpEquiv(‘Content-Language’, ‘en-US’)
->appendName(‘keywords’, $config->head->meta->keywords)
->appendName(‘description’, $config->head->meta->description);

$view->headLink()->appendStylesheet($config->head->css->site)
->appendStylesheet($config->head->css->menu)
->appendStylesheet($config->head->css->form)
->appendStylesheet($config->head->css->view);

$view->headTitle()->setSeparator(‘ — ’);
$view->headTitle($config->head->title);

}

func­tion _initNavigation()
{
$this->bootstrap(‘layout’);
$lay­out = $this->getResource(‘layout’);
$view = $layout->getView();

$nav­Con­fig = new Zend_Config_Xml(APPLICATION_PATH . ‘/configs/navigation.xml’, ‘nav’);
$nav­i­ga­tion = new Zend_Navigation($navConfig);

$view->navigation($navigation)->setAcl($this->_acl)
->setRole($this->_auth->getStorage()->read()->role);

}

}

configs/Navigation.xml

< ?xml version="1.0" encoding="UTF-8"?>



Tags: ,

5 Responses to "Zend_Acl & Zend_Navigation"

  • bachus says:
  • first_time_poster says:
  • thero says:
Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes