63 lines
2.2 KiB
PHP
Raw Normal View History

2011-01-11 08:43:13 +00:00
<?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 ($this->_request->getControllerName()!='user' &&
$this->_request->getActionName()!='login')
{
$auth = Zend_Auth::getInstance();
//Pas authentifié
if (!$auth->hasIdentity()){
if(count($this->_request->getParams())>0){
$action = $this->_request->getParam('action');
if ($action=='logon'){
$login = $this->_request->getParam('login');
$pass = $this->_request->getParam('pass');
$checkIp = ($this->_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('/');
2011-01-11 08:43:13 +00:00
}
}
}
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);
}
}
}
}