89 lines
2.5 KiB
PHP
Raw Normal View History

2011-10-21 10:14:30 +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)
{
$checkAuth = true;
//Pas d'authentification sur la demande d'authentification
2011-10-21 10:14:30 +00:00
if ($request->getControllerName()=='user' && $request->getActionName()=='login'){
2012-05-20 13:11:13 +00:00
$checkAuth = false;
2011-10-21 10:14:30 +00:00
}
// Pas d'authentification sur ces services
2012-05-20 13:11:13 +00:00
if (in_array($request->getControllerName(), array('service', 'import')) ) {
2011-10-21 10:14:30 +00:00
$checkAuth = false;
}
2012-05-20 13:11:13 +00:00
$checkWs = true;
if ( $request->getControllerName()=='fichier' ) {
$checkWs = false;
2012-05-20 13:11:13 +00:00
}
if ($checkAuth) {
2011-10-21 10:14:30 +00:00
$login = $request->getParam('login');
$pass = $request->getParam('pass', '');
$hach = $request->getParam('hach');
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
$auth = Zend_Auth::getInstance();
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
//On vérifie le tout lors d'une connexion par url
2012-05-20 13:11:13 +00:00
if ( !empty($login) && !empty($hach) ) {
$authAdapter = new Scores_AuthAdapter($login, $hach, $checkWs);
2011-10-21 10:14:30 +00:00
$result = $auth->authenticate($authAdapter);
if (!$result->isValid()) {
$layout = Zend_Layout::getMVCInstance();
if ( !$layout->isEnabled() ){
echo "Identification incorrect ou périmé.";
} else {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout');
}
2012-05-20 13:11:13 +00:00
} else {
2011-10-21 10:14:30 +00:00
$storage = new Zend_Auth_Storage_Session();
2012-05-22 07:34:13 +00:00
$session = new Zend_Session_Namespace($storage->getNamespace());
//$session->setExpirationSeconds(86400);
2012-05-20 13:11:13 +00:00
$auth->setStorage($storage);
2011-10-21 10:14:30 +00:00
}
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
//Sinon on reste sur le standard
} else {
//Pas authentifié
2012-05-22 07:34:13 +00:00
if ( !$auth->hasIdentity() || time() > $auth->getIdentity()->time ) {
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
$auth->clearIdentity();
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
$layout = Zend_Layout::getMVCInstance();
if (!$layout->isEnabled()){
echo "Identification incorrect ou périmé.";
2012-05-20 13:11:13 +00:00
} else {
$this->_response->setRedirect('/user/login')->sendResponse();
}
2011-10-21 10:14:30 +00:00
//Authentifié => on met à jour la session
} else {
2012-05-20 13:11:13 +00:00
2012-05-22 07:34:13 +00:00
$identity = $auth->getIdentity();
$identity->time = time() + $identity->timeout;
$auth->getStorage()->write($identity);
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
2012-05-20 13:11:13 +00:00
2011-10-21 10:14:30 +00:00
}
}
2012-05-20 13:11:13 +00:00
}
2011-10-21 10:14:30 +00:00
}
}