Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2007, 05:20 PM
Junior Member
 
Join Date: Jul 2007
Posts: 2
Default preDispatch() for Zend_Controller_Plugin_ErrorHandler

Hi there need a quick help about Zend_Controller_Plugin_ErrorHandler

I use it with my As_Controller_Plugin_Auth which works in preDispatch() method.
The last one uses ACL object to consider redirection to auth controller.
And now if:
Code:
!$this->_acl->isAllowed($role, $resource, $action)
it redirects but this also happend if dispatcher is not dispatchable because:
Code:
if (!$this->_acl->has($resource)) {
    $resource = null;
}
before check.


Earlier I used my own NoRoute plugin which used preDispatch() instead of postDispatch()
and it was checking controller's dispatchable before As_Controller_Plugin_Auth was run.

Therefore I suppose that I need a preDispatch check if there is no route. Can I do this with
Zend_Controller_Plugin_ErrorHandler?

Maybe someone just wrote class override Zend_Controller_Plugin_ErrorHandler with preDispatch() method?


here is As_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract class:
Code:
<?php

class As_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
    private $_auth;
    private $_acl;

    private $_noauth = array('module' => 'default', 'controller' => 'auth', 'action' => 'login');
    
    private $_noacl = array('module' => 'default', 'controller' => 'error', 'action' => 'privileges');
    
    public function __construct($auth, $acl)
    {
        $this->_auth = $auth;
        $this->_acl = $acl;
    }
    
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {   
        if ($this->_auth->hasIdentity()) {
            $role = $this->_auth->getIdentity()->role;
        } else {
            $role = 'guest';
        }
        
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        $module = $request->getModuleName();
        
        $resource = $module . '_' . $controller;
        
        if (!$this->_acl->has($resource)) {
            $resource = null;
        }
        
        if (!$this->_acl->isAllowed($role, $resource, $action)) {
            if (!$this->_auth->hasIdentity()) {
                $module = $this->_noauth['module'];
                $controller = $this->_noauth['controller'];
                $action = $this->_noauth['action'];
            } else {
                $module = $this->_noacl['module'];
                $controller = $this->_noacl['controller'];
                $action = $this->_noacl['action'];
            }
        }
        
        $request->setModuleName($module);
        $request->setControllerName($controller);
        $request->setActionName($action);
    }
}

Last edited by sk0rp : 07-25-2007 at 05:23 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-25-2007, 06:10 PM
Junior Member
 
Join Date: Jul 2007
Posts: 2
Default

Finaly I wrote my As_Controller_Plugin_ErrorHandler class. I welcome any sugestions to the code below

EDIT: I add some changes and now plugin works ok with module controllers

PHP Code:
<?php

class As_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_ErrorHandler
{
    
    public function 
preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$frontController Zend_Controller_Front::getInstance();
        
$dispatcher $frontController->getDispatcher();
        
        if (
$frontController->getParam('noErrorHandler') || $this->_isInsideErrorHandlerLoop) {
            return;
        }
        
        
$error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
        
                
        if (!
$dispatcher->isDispatchable($request)) {
            
$error->type self::EXCEPTION_NO_CONTROLLER;
        } elseif (!
$this->isProperAction($dispatcher$request)) {
            
$error->type self::EXCEPTION_NO_ACTION;
        }
        
        if (isset(
$error->type)) {
            
$this->_isInsideErrorHandlerLoop true;
            
            
$error->request = clone $request;
            
$request->setParam('error_handler'$error)
                    ->
setModuleName($this->getErrorHandlerModule())
                    ->
setControllerName($this->getErrorHandlerController())
                    ->
setActionName($this->getErrorHandlerAction());
        }

    }
    
    public function 
isProperAction($dispatcher$request)
    {
        
$className $dispatcher->loadClass($dispatcher->getControllerClass($request));
        
$actionName $request->getActionName();        
        if (empty(
$actionName)) {
            
$actionName $dispatcher->getDefaultAction();
        }
        
$methodName $dispatcher->formatActionName($actionName);
        
        
$class = new ReflectionClass($className);
        if (
$class->hasMethod($methodName)) {
            return 
true;
        }
        return 
false;
    }
}

Last edited by sk0rp : 07-28-2007 at 07:53 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:50 AM.