102 lines
3.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)
{
2011-09-07 12:54:43 +00:00
$checkAuth = true;
if ($request->getControllerName()=='user' && $request->getActionName()=='login'){
$checkAuth = false;
}
if ($checkAuth)
2011-01-11 08:43:13 +00:00
{
2011-09-07 12:54:43 +00:00
$login = $request->getParam('login');
$pass = $request->getParam('pass', '');
$hach = $request->getParam('hach');
$checkIp = $request->getParam('checkIp');
2011-01-11 08:43:13 +00:00
$auth = Zend_Auth::getInstance();
2011-09-07 12:54:43 +00:00
//Est ce que l'on a checkIp=only lors de la requête
$iponly = false;
if ($checkIp=='only') {
2011-09-07 12:54:43 +00:00
$hach = 'iponly:'.$_SERVER['REMOTE_ADDR'];
$iponly = true;
}
//On vérifie le tout lors d'une connexion par url
if ( !empty($login) && !empty($hach) ) {
require_once 'Web/WebAuthAdapter.php';
2011-09-07 12:54:43 +00:00
$authAdapter = new WebAuthAdapter($login, $hach, $iponly);
$result = $auth->authenticate($authAdapter);
$firebug = Zend_Registry::get('firebug');
$firebug->info($result);
2011-09-07 12:54:43 +00:00
if (!$result->isValid()) {
$messageF = '';
foreach ($result->getMessages() as $message) {
$messageF.= $message."<br/>";
}
2011-09-07 12:54:43 +00:00
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout')
->setParam('message', $messageF);
2011-09-07 12:54:43 +00:00
} else {
2012-03-14 17:05:10 +00:00
$timeout = $auth->getIdentity()->timeout;
/**
* On définit le temps de vie du cookie de session,
* une fois le naviateur client fermé au timeout du client
* Il semble qu'il faille le faire seulement avant le démarrage de la session
* sinon il n'est pas pris en compte donc inutil
*/
//Zend_Session::rememberMe($timeout);
2011-09-07 12:54:43 +00:00
$storage = new Zend_Auth_Storage_Session();
$sessionNamespace = new Zend_Session_Namespace($storage->getNamespace());
2012-03-14 17:05:10 +00:00
2011-09-07 12:54:43 +00:00
$sessionNamespace->setExpirationSeconds($timeout);
$auth->setStorage($storage);
}
2011-09-07 12:54:43 +00:00
//Sinon on reste sur le standard
} else {
//Pas authentifié
if (!$auth->hasIdentity()) {
$auth->clearIdentity();
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
2012-03-27 07:48:40 +00:00
$layout = Zend_Layout::getMVCInstance();
2012-03-27 07:48:40 +00:00
if ( !$layout->isEnabled() && $session->url!='/localauth') {
echo "Identification incorrect ou périmé.";
2012-03-27 07:48:40 +00:00
} else {
$this->_response->setRedirect('/user/login')->sendResponse();
}
2011-09-07 12:54:43 +00:00
//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);
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
}
2011-01-11 08:43:13 +00:00
}
2011-09-07 12:54:43 +00:00
}
2011-01-11 08:43:13 +00:00
}
}