116 lines
3.6 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)
{
$checkAuth = true;
if ($request->getControllerName()=='user' && $request->getActionName()=='login') {
$checkAuth = false;
}
if ($request->getControllerName()=='fichier'
&& $request->getClientIp(false)=='78.31.45.206') {
$checkAuth = false;
}
if ($checkAuth)
{
$login = $request->getParam('login');
$pass = $request->getParam('pass', '');
$hach = $request->getParam('hach');
$checkIp = $request->getParam('checkIp');
$auth = Zend_Auth::getInstance();
//Est ce que l'on a checkIp=only lors de la requête
$iponly = false;
if ($checkIp=='only') {
$hach = 'iponly:'.$_SERVER['REMOTE_ADDR'];
$iponly = true;
}
//On vérifie le tout lors d'une connexion par url
if ( !empty($login) && !empty($hach) ) {
$authAdapter = new Scores_AuthAdapter($login, $hach, $iponly);
$result = $auth->authenticate($authAdapter);
if (!$result->isValid()) {
$messageF = '';
foreach ($result->getMessages() as $message) {
$messageF.= $message."<br/>";
}
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout')
->setParam('message', $messageF);
} else {
$storage = new Zend_Auth_Storage_Session();
$session = new Zend_Session_Namespace($storage->getNamespace());
//$session->setExpirationSeconds(86400);
$auth->setStorage($storage);
}
//Sinon on reste sur le standard
} else {
//Pas authentifié
if ( !$auth->hasIdentity() ) {
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
$request->setModuleName('default')
->setControllerName('user')
->setActionName('login');
} elseif (time() > $auth->getIdentity()->time ) {
$auth->clearIdentity();
$storage = $auth->getStorage();
Zend_Session::namespaceUnset($storage->getNamespace());
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
if ( $request->getControllerName()=='index' && $request->getActionName()=='index' ) {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('login');
} else {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout');
}
//Authentifié => on met à jour la session
} else {
$identity = $auth->getIdentity();
$identity->time = time() + $identity->timeout;
$auth->getStorage()->write($identity);
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
//Check CGU
if ( $request->getControllerName()!='aide' && $request->getActionName()!='cgu'
&& $request->getControllerName()!='user' && $request->getActionName()!='logout') {
if ( empty($identity->acceptationCGU) || $identity->acceptationCGU=='0000-00-00 00:00:00' ) {
$request->setModuleName('default')
->setControllerName('aide')
->setActionName('cgu');
}
}
}
}
}
}
}