Advertisement
jeremykendall

Front Controller Auth Plugin

Aug 29th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2.  
  3. class Fresh_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
  4. {
  5.  
  6.     private $_auth;
  7.  
  8.     private $_acl;
  9.  
  10.     private $_noauth = array('controller' => 'auth', 'action' => 'index');
  11.  
  12.     private $_noacl = array('controller' => 'error', 'action' => 'privileges');
  13.  
  14.     public function __construct($auth, $acl)
  15.     {
  16.         $this->_auth = $auth;
  17.         $this->_acl = $acl;
  18.     }
  19.  
  20.     public function preDispatch(Zend_Controller_Request_Abstract $request)
  21.     {
  22.         $frontController = Zend_Controller_Front::getInstance();
  23.        
  24.         $controller = $request->getControllerName();
  25.         $action = $request->getActionName();
  26.         $resource = $controller;
  27.        
  28.         if ($this->_auth->hasIdentity()) {
  29.             $identity = $this->_auth->getIdentity();
  30.             if (is_array($identity)) {
  31.                 $role = $identity['role'];
  32.             } else if (is_object($identity)) {
  33.                 $role = $identity->role;
  34.             } else {
  35.                 // If $identity isn't an array and isn't an object, something isn't right.
  36.                 // Set role to guest and move on.
  37.                 $role = 'guest';
  38.             }
  39.         } else {
  40.             $role = 'guest';
  41.         }
  42.        
  43.         if (! $this->_acl->has($resource)) {
  44.             $resource = null;
  45.         }
  46.        
  47.         $isDispatchable = $frontController->getDispatcher()->isDispatchable($request);
  48.        
  49.         if ($isDispatchable && ! $this->_acl->isAllowed($role, $resource, $action)) {
  50.             if (! $this->_auth->hasIdentity()) {
  51.                 // Not logged in, send to login page
  52.                 $controller = $this->_noauth['controller'];
  53.                 $action = $this->_noauth['action'];
  54.                 $redirectNS = new Zend_Session_Namespace(
  55.                 'redirect');
  56.                 $redirectNS->fromUrl = $_SERVER['REQUEST_URI'];
  57.             } else {
  58.                 // Permission to access resource denied.  Send to error page.
  59.                 $controller = $this->_noacl['controller'];
  60.                 $action = $this->_noacl['action'];
  61.             }
  62.         }
  63.        
  64.         $request->setControllerName($controller);
  65.         $request->setActionName($action);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement