Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2010
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2. class Application_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract {
  3.     // at first i tried routeStartup by that will mean controller and actions will be unknown
  4.     function preDispatch(Zend_Controller_Request_Abstract $req) {
  5.         // get role from Zend_Auth, if not logged in, use 'guest'
  6.         $auth = Zend_Auth::getInstance();
  7.         if ($auth->hasIdentity()) {
  8.             $role = $auth->getIdentity();
  9.         } else {
  10.             $role = 'guest';
  11.         }
  12.        
  13.         // get ACL
  14.         $acl = Zend_Controller_Front::getInstance()
  15.                     ->getParam('bootstrap')
  16.                     ->getResource('acl');
  17.        
  18.         // set resource & privilege based on controller & action
  19.         $resource = $req->getControllerName();
  20.         $privilege = $req->getActionName();
  21.        
  22.         // "plugin": modify resource if is isset
  23.         $id = $req->getParam('id');
  24.         if (!empty($id)) {
  25.             $em = Zend_Registry::get('em');
  26.             switch ($resource) {
  27.                 case 'posts':
  28.                     $post = $em->getRepository('Application\\Models\\Post')
  29.                                ->findOneById($id);
  30.                     if (!empty($post)) {
  31.                         $resource = $post;
  32.                     }  
  33.             }          
  34.         }
  35.         // die($role . ':' . $resource . ':' . $privilege . ' -> ' . ($acl->isAllowed($role, $resource, $privilege) ? 'allowed' : 'denied'));
  36.         if (!$acl->isAllowed($role, $resource, $privilege)) {
  37.             // $res = $this->_response;
  38.             if (!$auth->hasIdentity()) {
  39.                 // login required
  40.                 $req->setControllerName('error')
  41.                     ->setActionName('error')
  42.                     ->setParams(array(
  43.                         'error_handler' => 'login',
  44.                         'returnUrl' => urlencode($req->getRequestUri())
  45.                     ));
  46.             } else {
  47.                 // unauthorized access
  48.                 $res->setRedirect('/error/?error-handler=unauthorized', 403);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement