Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour,
J'essaie de développer un plugin d'authentification couplé à la gestion des exceptions. Mon plugin d'authentification marche très bien mais je n'arrive pas à gérer les exceptions par son intermédiaire. Je me suis inspiré de certains bout de code qui traînent sur le forum et voici mon Plugin Auth :
<?php class Zftemplate_Controller_Plugins_Auth extends Zend_Controller_Plugin_Abstract { private $_auth; private $_acl; private $_noauth = array('module' => 'frontoffice', 'controller' => 'auth', 'action' => 'noauth'); private $_noacl = array('module' => 'frontoffice', 'controller' => 'auth', 'action' => 'unauthorized'); private $_noroute = array('module' => 'frontoffice', 'controller' => 'error', 'action' => 'error'); public function __construct() { $this->_auth = Zend_Auth::getInstance(); $this->_acl = new Zftemplate_Acl_MyAcl(); } public function preDispatch(Zend_Controller_Request_Abstract $request) { //RECUPERATION DU ROLE DE L'UTILISATEUR - SINON "INVITE" PAR DEFAUT if ($this->_auth->hasIdentity()) { $roleUser = $this->_auth->getIdentity()->role; } else { $roleUser = 'Invite'; } //RECUPERATION DU NOM DU CONTROLLER, ACTION ET DU MODULE DANS LEQUEL L'UTILISATEUR VEUT S'ENGAGER A CHAQUE REQUETE $controller = $request->controller; $action = $request->action; $module = $request->module; //FORMATAGE DU NOM DE RESSOURCE SUIVANT ROUTER //A DECLARER UNIQUEMENT DANS UNE APPLI MODULAIRE if($module == null) { $module = 'frontoffice'; } $resource = $module.'_'.$controller; //$resource = $controller; //TEST SI LES RESSOURCES DES ACLs SONT DECLAREES //SI ELLES N'EXISTENT PAS, ON LES DECLARE NULL if (!$this->_acl->has($resource)) { $resource = null; } //TEST DE L'EXISTENCE DU CONTROLLER ET DE SON ACTION //SI CE N'EST PAS LE CAS, ON REDIRIGE VERS LE ERROR_HANDLER (ErrorController.php) if(!$this->_routeExists($request)) { $module = $this->_noroute['module']; $controller = $this->_noroute['controller']; $action = $this->_noroute['action']; } else { //TEST DES DROITS if(!$this->_acl->isAllowed($roleUser, $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); } private function _routeExists($request) { $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher(); //TEST SI LA REQUETE HTTP EST DISPATCHABLE - EXISTENCE DE CONTROLLER //SI CE N'EST PAS DISPATCHABLE, ON RETOURNE QUE LA REQUETE A ECHOUE if(!$dispatcher->isDispatchable($request)) { return false; } //VERIFICATION DE L'EXISTANCE DE L'ACTION //RECUPERATION DU NOM DU CONTROLLER DANS LA REQUETE HTTP - FORMATAGE DU NOM + EXTENSION $controllerClassName = $dispatcher->formatControllerName($request->getControllerName()); $controllerClassFile = $controllerClassName.'.php'; //RECUPERATION DU MODULE UTILISE if($request->getModuleName() != $dispatcher->getDefaultModule()) { $controllerClassName = ucfirst($request->getModuleName().'_'.$controllerClassName); } //RECUPERATION DES ACTIONS DU CONTROLLER SPECIFIE try { //ON INCLUT LE FICHIER SI LE DOSSIER DE CONTROLLERS NE SONT PAS DANS UN INCLUDE //Zend_Loader::loadFile($controllerClassFile,$dispatcher->getControllerDirectory($request->getModuleName())); $actionMethodName = $dispatcher->formatActionName($request->getActionName()); //VERIFICATION SI L'ACTION EST BIEN PRESENTE DANS LE CONTROLLER SPECIFIE DANS LA REQUETE if(@in_array($actionMethodName, get_class_methods($controllerClassName))) { return true; } return false; } catch (Exception $exception) { return false; } } } ?>
Mon ErrorController.php :
<?php class ErrorController extends Zend_Controller_Action { public function errorAction() { //RECUPERATION DU PARAMETRE D'ERREUR $errors = $this->_getParam('error_handler'); //ANALYSE DE LA PROVENANCE DE L'ERREUR if($errors->exception instanceof Zend_Controller_Exception) { $log = "notice"; $this->getResponse()->setHttpResponseCode(404); $this->view->setTitrePage("Zftemplate - Page Introuvable !"); $this->view->message = $this->view->translate("La page que vous avez demande n'a pas pu etre trouvee !"); } elseif($errors->exception instanceof Zend_Db_Exception) { $log = "emerg"; $this->getResponse()->setHttpResponseCode(503); $this->view->setTitrePage("Zftemplate - Probleme de base de donnees"); $this->view->message = $this->view->translate("Un probleme de base de donnees nous empeche de servir votre requete !"); } elseif($errors->exception instanceof Zftemplate_User_Exception) { $log = "user"; $this->view->setTitrePage("Zftemplate - Vous avez commis une erreur !"); $this->view->message = $this->view->translate($errors->exception->getMessage()); } elseif($errors->exception instanceof Zftemplate_Cache_Exception) { $log = "Cache"; $this->view->setTitrePage("Zftemplate - Erreur de Cache !"); $this->view->message = $this->view->translate($errors->exception->getMessage()); } else { $this->getResponse()->setHttpResponseCode(503); $log = "alert"; $this->view->setTitrePage("Zftemplate - Erreur de l'application !"); $this->view->message = $this->view->translate("Notre site est momentanement indisponible !"); } //VIDE LE CONTENU DE LA REPONSE $this->_response->clearBody(); //SI ON EST EN MODE DEBUG if($this->getInvokeArg('debug') == 1) { //ON ECRASE LE FICHIER ET ON AFFICHE L'EXCEPTION COMPLETE $this->view->message = $errors->exception; } //ENREGISTREMENT DE L'ERREUR DANS LE FICHIER DE LOG Zend_Registry::get('log_error')->$log($errors->exception); } } ?>
Ce code marche presque....sauf que lorsque je me route sur un controller ou une action bidon, il me redirige bien sur mon ErrorController avec l'action error mais il ne détecte pas l'exception :
A l'éxecution de l'action errorAction() :
Erreur apparue Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 10 Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 17 Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 24 Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 30 Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 51 Notice: Trying to get property of non-object in /var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/ErrorController.php on line 55
Il ne me retourne aucune exception ???
Si quelqu'un a une explication je suis preneur !!
Merci
Hors ligne
Du coup, je me réponds.
J'ai corrigé mon code, voici mon nouveau plugin Auth :
<?php class Zftemplate_Controller_Plugins_Auth extends Zend_Controller_Plugin_Abstract { private $_auth; private $_acl; private $_dispatcher; private $_noacl = array('module' => 'frontoffice', 'controller' => 'auth', 'action' => 'unauthorized'); private $_noroute = array('module' => 'frontoffice', 'controller' => 'error', 'action' => 'error'); public function __construct() { $this->_auth = Zend_Auth::getInstance(); $this->_acl = new Zftemplate_Acl_MyAcl(); $this->_dispatcher = Zend_Controller_Front::getInstance()->getDispatcher(); } public function preDispatch(Zend_Controller_Request_Abstract $request) { //RECUPERATION DU ROLE DU CLIENT //SI PAS AUTHENTIFIE, ROLE INVITE PAR DEFAUT if($this->_auth->hasIdentity()) { $role = $this->_auth->getIdentity()->role; } else { $role = 'Invite'; } //RECUPERATION DU MODULE, CONTROLLER ET DE L'ACTION DANS LA REQUETE $module = $request->getModuleName(); $controller = $request->getControllerName(); $action = $request->getActionName(); //SI LE MODULE N'EST PAS DANS LA REQUETE (EX: REDIRECTION STATIQUE) //ON CONTRUIT UNE PARTIE DE LA RESSOURCE if(!$module) { $module = 'frontoffice'; } $resource = $module.'_'.$controller; //ON TESTE LA ROUTE DEMANDEE DANS LA REQUETE //SI ELLE EXISTE, ON TESTE L'AUTHORISATION DANS LES ACLs if(!$this->_routeExists($request)) { throw new Zend_Controller_Exception(); $module = $this->_noroute['module']; $controller = $this->_noroute['controller']; $action = $this->_noroute['action']; } else { if(!$this->_acl->isAllowed($role, $resource, $action)) { $module = $this->_noacl['module']; $controller = $this->_noacl['controller']; $action = $this->_noacl['action']; } } //ON PARAMETRE LA REQUETE AVEC LES PARAMETRES ADEQUATS $request->setModuleName($module); $request->setControllerName($controller); $request->setActionName($action); } private function _routeExists($request) { //TEST SI LA REQUETE HTTP EST DISPATCHABLE - EXISTENCE DE CONTROLLER //SI CE N'EST PAS DISPATCHABLE, ON RETOURNE QUE LA REQUETE A ECHOUE ET ON RENVOIE FALSE if(!$this->_dispatcher->isDispatchable($request)) { return false; } else { $class = $this->_dispatcher->getControllerClass($request); $r = new ReflectionClass($class); $action = $this->_dispatcher->getActionMethod($request); if($r->hasMethod($action)) { return true; } else { return false; } } } } ?>
ça fonctionne bien sauf quand je me place dans un controller du backoffice car la fonction _routeExists() bug qd je lui demande une nouvelle instance de ReflectionClass().
Je m'explique : mes controllers dans mon backoffice sont préfixé comme cela :
<?php class Backoffice_UsersController extends Zend_Controller_Action { ....etc
or dans mon plugin Auth je lui demande de m'instancier une ReflectionClass() grâce au nom du controller, or si je me trouve dans le backoffice, il va me retourner le nom "UsersController" et comme la class s'appelle "Backoffice_UsersController", ça merde...
Du coup, je reconstruis le nom de la classe :
private function _routeExists($request) { //TEST SI LA REQUETE HTTP EST DISPATCHABLE - EXISTENCE DE CONTROLLER //SI CE N'EST PAS DISPATCHABLE, ON RETOURNE QUE LA REQUETE A ECHOUE ET ON RENVOIE FALSE if(!$this->_dispatcher->isDispatchable($request)) { return false; } else { $class = $this->_dispatcher->getControllerClass($request); $module = $request->getModuleName(); if($module == 'backoffice') { $module = ucfirst($module); $r = new ReflectionClass($module.'_'.$class); } else { $r = new ReflectionClass($class); } $action = $this->_dispatcher->getActionMethod($request); if($r->hasMethod($action)) { return true; } else { return false; } } }
Je retourne dans mon $r = new ReflectionClass($class) avec $class = "Backoffice_UsersController" mais j'obtiens une erreur lié à mon Zend_Loader::registerAutoload() de mon bootstrap :
Warning: Zend_Loader::include_once(backoffice/UsersController.php) [zend-loader.include-once]: failed to open stream: No such file or directory in /var/www/zendframework/zftemplate/library/Zend/Loader.php on line 83 Warning: Zend_Loader::include_once() [function.include]: Failed opening 'backoffice/UsersController.php' for inclusion (include_path='/var/www/zendframework/zftemplate/:/var/www/zendframework/zftemplate/library/:/var/www/zendframework/zftemplate/application/modules/frontoffice/controllers/:/var/www/zendframework/zftemplate/application/modules/backoffice/controllers/:/var/www/zendframework/zftemplate/application/modules/frontoffice/models/:/var/www/zendframework/zftemplate/application/modules/backoffice/models/:.:/usr/share/php:/var/www/zendframework/zftemplate/library') in /var/www/zendframework/zftemplate/library/Zend/Loader.php on line 83
Si quelqu'un sait comment shunter le pré-fixage des underscores (_) en slashs (/) .....
Merci
Dernière modification par allfab (27-02-2009 10:39:59)
Hors ligne
Pages: 1