Zend_Acl & Zend_Navigation

Setting up a simple working example of Acl & Navigation in Zend Framework 1.9.x as demonstrated by jscherer26.
Enjoy

models/Acl.php

< ?php class Model_Acl extends Zend_Acl { public function __construct() { // define Roles $this->addRole(new Zend_Acl_Role('guest')); // not authenicated
$this->addRole(new Zend_Acl_Role('member'), 'guest'); // authenticated as member inherit guest privilages
$this->addRole(new Zend_Acl_Role('admin'), 'member'); // authenticated as admin inherit member 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 privileges
$this->allow('guest', array('index','error'));
$this->allow('guest', 'authentication', array('index','signin'));

$this->allow('member', 'authentication', array('index','signout'));
$this->deny( 'member', 'authentication', 'signin');
$this->allow('member', 'activity', array('index','list')); // member has list privilages for resource activity

$this->allow('admin', 'activity'); // admin has all privileges for resource activity
}
}

plugins/Authenticated.php

< ?php class Plugin_Authenticated 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)
{
$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 Bootstrap extends Zend_Application_Bootstrap_Bootstrap { private $_acl = null; private $_auth = null; protected function _initAutoload() { $modelLoader = new Zend_Application_Module_Autoloader(array( 'namespace' => '',
'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 $modelLoader;
}

function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();

$config = 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);

}

function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();

$navConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
$navigation = 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"?>


5 Replies to “Zend_Acl & Zend_Navigation”

  1. ================FacultyappAcl.php=================
    add(new Zend_Acl_Resource(‘list’));
    $this->add(new Zend_Acl_Resource(‘insert’));
    $this->add(new Zend_Acl_Resource(‘search’));

    $this->addRole(new Zend_Acl_Role(‘user’));
    $this->addRole(new Zend_Acl_Role(‘admin’,’user’));

    //$this->deny(‘user’,’search’);
    $this->allow(‘user’,array(‘list’,’insert’));
    $this->allow(‘admin’,’search’);

    }

    ========================navigation.xml=================

    List
    faculty
    list
    list

    Insert
    faculty
    insert
    insert

    Seach
    faculty
    search
    search

    =================================Bootstrap.php============
    ”,
    ‘basePath’ => APPLICATION_PATH));

    //acl related code
    $this->_acl = new Model_FacultyappAcl;
    $this->_auth = Zend_Auth::getInstance();

    //if(!$this->_auth->hasIdentity()) {$this->_auth->getStorage()->read()->role = ‘user’;}

    $fc = Zend_Controller_Front::getInstance();
    $fc->registerPlugin(new Plugin_AccessCheck($this->_acl,$this->_auth));
    //end acl related code
    return $moduleLododer;
    }

    function _initViewHelpers()
    {
    $this->bootstrap(‘layout’);
    $layout = $this->getResource(‘layout’);
    $view = $layout->getview();
    ZendX_JQuery::enableView($view);

    $view->doctype(‘HTML4_STRICT’);

    $navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH.’/configs/navigation.xml’,’nav’);
    $navContainer = new Zend_Navigation($navContainerConfig);

    //acl related code
    $view->navigation($navContainer)->setAcl($this->_acl)->setRole($this->_auth->getStorage()->read()->role);
    //end acl related code

    }

    }

    ===========================AccessCheck.php=================
    _acl = $acl;
    $this->_auth = $auth;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
    $resource = $request->getControllerName();
    $action = $request->getActionName();

    $identity = $this->_auth->getStorage()->read();
    $role = $identity->role;

    if((!is_object($identity))||!$this->_acl->isAllowed($role, $resource, $action)) {
    $request->setControllerName(‘faculty’)->setActionName(‘list’);
    }
    }
    }
    ?>
    ===================================================================
    I am using Zend 1.8.3

    And got this error “Fatal error: Zend_Acl_Role_Registry_Exception: Role ‘user’ not found in /usr/local/lib/Zend/View/Helper/Navigation/HelperAbstract.php on line 488”

    Any clue where I did wrong?

  2. For me as a Newbie it seems just another 2 hours spend trying out with a tutorial which doesn’t works.

    Buts thanks anyway.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.