Before I start explaining the code let me just give it to you!

Users SQL

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`userpassword` varchar(40) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`role` varchar(30) NOT NULL DEFAULT 'user',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

/application/configs/application.ini

autoloaderNamespaces[] = "Application_"
resources.frontController.plugins.Acl = "Application_Controller_Plugin_Acl"

acl.roles.guest = null
acl.roles.user = guest
acl.roles.admin = user

acl.resources.allow.index.all = guest
acl.resources.allow.error.all = guest
acl.resources.allow.user.register = guest
acl.resources.allow.user.login = guest
acl.resources.allow.user.profile = user

/library/Application/Acl.php

<?php

class Application_Acl extends Zend_Acl {

    public function __construct(Zend_Config $config){
       $roles = $config->acl->roles;
       $resources = $config->acl->resources;
       $this->_addRoles($roles);
       $this->_addResources($resources);
    }

    private function _addRoles($roles){
        foreach($roles as $name => $parents){
            if(!$this->hasRole($name)) {
                if(empty($parents)){
                    $parents = array();
                } else {
                    $parents = explode(',', $parents);
                }

                $this->addRole(new Zend_Acl_Role($name), $parents);
            }
        }
    }

    private function _addResources($resources){
        foreach($resources as $permissions => $controllers){
            foreach($controllers as $controller => $actions){

                if('all' == $controller){
                    $controller = null;
                } else {
                    if(!$this->has($controller)){
                        $this->add(new Zend_Acl_Resource($controller));
                    }
                }

                foreach($actions as $action => $role){
                    if($action == 'all') {
                        $action = null;
                    }
                    if($permissions = 'allow'){
                        $this->allow($role, $controller, $action);
                    }
                    if($permissions == 'deny'){
                        $this->deny($role, $controller, $action);
                    }
                }

            }
        }
    }

}

/library/Application/Controller/Plugin/Acl.php

<?php

class Application_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request){

        // Load ACL config
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
        $acl = new Application_Acl($config);

        // Begin authorisation
        $auth = Zend_Auth::getInstance();
        $role = 'guest';

        if($auth->hasIdentity()){
            $user = $auth->getIdentity();
            print_r($user);
            if(is_object($user)){
                $role = $user->role;
            }
        }

        $controller = $request->getControllerName();
        $action = $request->getActionName();
        $module = $request->getModuleName();

        $resource = $controller;
        $privellege = $action;

        if(!$acl->has($resource)) {
            throw new Exception('No resource found');
        }

        if(!$acl->isAllowed($role, $resource, $privellege)) {
            $request->setModuleName('default')
                    ->setControllerName('user')
                    ->setActionName('login')
                    ->setDispatched(false);
        }

    }

}

/application/controllers/UserController.php


<?php
class UserController extends Zend_Controller_Action
{

    public function indexAction(){
        // action body
    }

    public function logoutAction(){
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
    }

    public function loginAction() {
        $form = $this->_helper->formLoader('login');
        if ($this->getRequest()->isPost()) {
            if (! $form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $data = $form->getValues();
                $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
                $authAdapter->setTableName('user')->setCredentialColumn('userpassword')->setIdentityColumn('email')->setCredentialTreatment('MD5(?)');
                $authAdapter->setIdentity($data['email']);
                $authAdapter->setCredential($data['password']);
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid()) {
                    $userInfo = $authAdapter->getResultRowObject(null, array('userpassword'));
                    $authStorage = $auth->getStorage();
                    $authStorage->write($userInfo);
                } else {

                }
            }
        }
        $this->view->form = $form;
    }

    public function registerAction(){

        $form = $this->_helper->formLoader('register');

        if($this->getRequest()->isPost()){
            if(!$form->isValid($_POST)){
                $this->view->form = $form;
                return;
            }
        }

        $this->view->form = $form;

    }
}

So that’s the code, cudos has to go to Joe Topjian for giving me the idea and the base code to work from…