105 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;
2012-08-23 20:19:14 +00:00
if ($request->getControllerName()=='user' && $request->getActionName()=='login') {
$checkAuth = false;
2011-09-07 12:54:43 +00:00
}
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;
2011-09-07 12:54:43 +00:00
}
//On vérifie le tout lors d'une connexion par url
2012-04-02 16:01:28 +00:00
if ( !empty($login) && !empty($hach) ) {
2012-04-04 10:12:54 +00:00
$authAdapter = new Scores_AuthAdapter($login, $hach, $iponly);
2011-09-07 12:54:43 +00:00
$result = $auth->authenticate($authAdapter);
2012-06-25 08:53:54 +00:00
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);
} else {
2011-09-07 12:54:43 +00:00
$storage = new Zend_Auth_Storage_Session();
$session = new Zend_Session_Namespace($storage->getNamespace());
2012-04-13 15:43:03 +00:00
//$session->setExpirationSeconds(86400);
2012-04-02 16:01:28 +00:00
$auth->setStorage($storage);
}
2011-09-07 12:54:43 +00:00
//Sinon on reste sur le standard
} else {
2012-08-23 08:04:46 +00:00
2011-09-07 12:54:43 +00:00
//Pas authentifié
2012-08-23 20:19:14 +00:00
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 ) {
2011-09-07 12:54:43 +00:00
2012-08-23 20:19:14 +00:00
$auth->clearIdentity();
2012-08-23 08:04:46 +00:00
$storage = $auth->getStorage();
Zend_Session::namespaceUnset($storage->getNamespace());
2012-08-23 20:19:14 +00:00
2011-09-07 12:54:43 +00:00
$session = new Zend_Session_Namespace('login');
2012-08-23 08:04:46 +00:00
$session->url = $_SERVER['REQUEST_URI'];
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout');
2012-04-02 16:01:28 +00:00
2011-09-07 12:54:43 +00:00
//Authentifié => on met à jour la session
} else {
2012-04-02 16:01:28 +00:00
$identity = $auth->getIdentity();
2012-04-02 16:01:28 +00:00
$identity->time = time() + $identity->timeout;
$auth->getStorage()->write($identity);
2011-09-07 12:54:43 +00:00
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
//Check CGU
2012-06-25 08:53:54 +00:00
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');
}
}
2011-09-07 12:54:43 +00:00
}
2011-01-11 08:43:13 +00:00
}
}
2011-01-11 08:43:13 +00:00
}
}