2011-05-03 14:33:09 +00:00

63 lines
2.2 KiB
PHP

<?php
class Application_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
/**
* Vérifie les autorisations
* Utilise _request et _response hérités et injectés par le FC
*
* @param Zend_Controller_Request_Abstract $request : non utilisé, mais demandé par l'héritage
*/
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if ($request->getControllerName()!='user' && $request->getActionName()!='login')
{
$auth = Zend_Auth::getInstance();
//Pas authentifié
if (!$auth->hasIdentity()){
if(count($request->getParams())>0){
$action = $request->getParam('action');
if ($action=='logon'){
$login = $request->getParam('login');
$pass = $request->getParam('pass');
$checkIp = ($request->getParam('checkIp', '')=='only') ? true : false;
//Vérification
$authAdapter = new WebAuthAdapter($login, $pass, $checkIp);
$result = $auth->authenticate($authAdapter);
if (!$result->isValid()){
$this->view->message = '';
foreach ($result->getMessages() as $message) {
$this->view->message.= $message."<br/>";
}
} else {
$timeout = $auth->getIdentity()->timeout;
$storage = new Zend_Auth_Storage_Session();
$sessionNamespace = new Zend_Session_Namespace($storage->getNamespace());
$sessionNamespace->setExpirationSeconds($timeout);
$auth->setStorage($storage);
$url = '';
if (Zend_Registry::isRegistered('URL')){
$url = Zend_Registry::get('URL');
}
if (!empty($url)){
$this->_response->setRedirect($url);
}
$this->_response->setRedirect('/');
}
}
}
$auth->clearIdentity();
Zend_Registry::set('URL',$_SERVER['REQUEST_URI']);
$this->_response->setRedirect('/user/login');
//Authentifié => on met à jour la session
} else {
$timeout = $auth->getIdentity()->timeout;
$storage = new Zend_Auth_Storage_Session();
$sessionNamespace = new Zend_Session_Namespace($storage->getNamespace());
$sessionNamespace->setExpirationSeconds($timeout);
$auth->setStorage($storage);
}
}
}
}