All pastes #771249 Raw Edit

Authorize Plugin

public text v1 · immutable
·published 2007-11-12 21:47 UTC
rendered paste body
<?php

require_once 'Zend/Controller/Plugin/Abstract.php';

class My_Controller_Plugin_Authorize extends Zend_Controller_Plugin_Abstract
{
     private $_auth;
     private $_acl;
     private $_view;

     private $_noauth = array('module'     => 'default',
                              'controller' => 'Authorize',
                              'action'     => 'NotAuthorized');

     private $_noacl  = array('module'     => 'default',
                              'controller' => 'Authorize',
                              'action'     => 'Signin');

     public function __construct($auth, $acl, $view)
     {
         $this->_auth = $auth;
         $this->_acl  = $acl;
         $this->_view = $view;
     }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
     {
         if ($this->_auth->hasIdentity()) {
             $identity = $this->_auth->getInstance()->getIdentity();
             $this->_view->role = strtolower($identity->role);
         } else {
             $this->_view->role = 'guest';
         }
		 $this->_view->menu->setAllow($this->_view->role, $this->_view, $this->_acl);
		 if ($this->_view->role !== "guest") { $this->_view->menu->deny("signin"); }
         if (!$this->_acl->has($resource = strtolower($request->controller))) {
             $resource = null;
         }

         if (!$this->_acl->isAllowed($this->_view->role, $resource, strtolower($request->action))) {
             if ($this->_auth->hasIdentity()) {
                 $request->setModuleName($this->_noauth['module']);
                 $request->setControllerName($this->_noauth['controller']);
                 $request->setActionName($this->_noauth['action']);
             } else {
                 $request->setModuleName($this->_noacl['module']);
                 $request->setControllerName($this->_noacl['controller']);
                 $request->setActionName($this->_noacl['action']);
             }
         }
     }
}