97 lines
3.0 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
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);
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);
2012-04-04 10:12:54 +00:00
} else {
2011-09-07 12:54:43 +00:00
$storage = new Zend_Auth_Storage_Session();
Zend_Session::rememberMe(86400);
$session = new Zend_Session_Namespace($storage->getNamespace());
$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-04-02 16:01:28 +00:00
Zend_Registry::get('firebug')->info('TIME : '.date('Y-m-d H:i:s',time()));
2012-04-04 10:12:54 +00:00
2011-09-07 12:54:43 +00:00
//Pas authentifié
2012-04-04 10:12:54 +00:00
if ( !$auth->hasIdentity() || time() > $auth->getIdentity()->time ) {
2011-09-07 12:54:43 +00:00
$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();
if ( !$layout->isEnabled() ) {
echo "Identification incorrect ou périmé.";
2012-03-27 07:48:40 +00:00
} else {
$this->_response->setRedirect('/user/login')->sendResponse();
}
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-04 10:12:54 +00:00
Zend_Registry::get('firebug')->info('TIMEOUT : '.$identity->timeout);
Zend_Registry::get('firebug')->info('ID TIME : '.date('Y-m-d H:i:s',$identity->time));
2012-04-04 10:12:54 +00:00
2012-04-02 16:01:28 +00:00
$identity->time = time() + $identity->timeout;
2012-04-02 16:01:28 +00:00
$auth->getStorage()->write($identity);
Zend_Registry::get('firebug')->info('UP TIME : '.date('Y-m-d H:i:s',$auth->getIdentity()->time));
2011-09-07 12:54:43 +00:00
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
}
}