98 lines
2.8 KiB
PHP
Raw Normal View History

2013-11-05 11:18: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)
{
$controller = $request->getControllerName();
$action = $request->getActionName();
$checkAuth = true;
2015-04-10 09:47:56 +00:00
// --- Pas d'authentification sur la demande d'authentification
2013-11-05 11:18:30 +00:00
if ( $controller == 'user' && $action == 'login' ) {
$checkAuth = false;
}
2015-04-10 09:47:56 +00:00
// --- Pas d'authentification sur ces services
2013-11-05 11:18:30 +00:00
if ( in_array($controller, array('service', 'import'))
2014-02-25 16:33:07 +00:00
|| ( $controller == 'fichier' && $action == 'logs' )
2013-11-05 11:18:30 +00:00
|| ( $controller == 'fichier' && $action == 'kbis' )
|| ( $controller == 'fichier' && $action == 'csv' )
2014-02-25 16:33:07 +00:00
|| ( $controller == 'fichier' && $action == 'associations' )
2015-08-07 13:45:36 +00:00
|| ( $controller == 'fichier' && $action == 'greffes' )
|| ( $controller == 'fichier' && $action == 'crm' )) {
2013-11-05 11:18:30 +00:00
$checkAuth = false;
}
$checkWs = true;
if ( $controller == 'fichier' ) {
$checkWs = false;
}
if ($checkAuth) {
$login = $request->getParam('login');
$pass = $request->getParam('pass', '');
$hach = $request->getParam('hach');
if (!empty($hach)) {
$pass = $hach;
}
2013-11-21 16:13:20 +00:00
2013-11-05 11:18:30 +00:00
$auth = Zend_Auth::getInstance();
2015-04-10 09:47:56 +00:00
// --- On vérifie le tout lors d'une connexion par url
2013-11-05 11:18:30 +00:00
if ( !empty($login) && !empty($pass) ) {
2013-11-21 16:13:20 +00:00
$authAdapter = new Scores_Auth_Adapter_Db($login, $pass, $checkWs);
2013-11-05 11:18:30 +00:00
$result = $auth->authenticate($authAdapter);
2015-06-05 15:29:18 +00:00
if ($result->isValid()) {
$storage = new Zend_Auth_Storage_Session();
$session = new Zend_Session_Namespace($storage->getNamespace());
//$session->setExpirationSeconds(86400);
$auth->setStorage($storage);
2013-11-05 11:18:30 +00:00
} else {
2015-06-05 15:29:18 +00:00
$layout = Zend_Layout::getMVCInstance();
if ( !$layout->isEnabled() ){
echo "Identification incorrect ou périmé.";
} else {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout');
}
2013-11-05 11:18:30 +00:00
}
//Sinon on reste sur le standard
} else {
2015-06-05 15:29:18 +00:00
2013-11-05 11:18:30 +00:00
//Pas authentifié
if ( !$auth->hasIdentity() || time() > $auth->getIdentity()->time ) {
2015-06-05 15:29:18 +00:00
$layout = Zend_Layout::getMVCInstance();
2013-11-05 11:18:30 +00:00
if (!$layout->isEnabled()){
echo "Identification incorrect ou périmé.";
} else {
$this->_response->setRedirect('/user/login')->sendResponse();
}
2015-06-05 15:29:18 +00:00
2013-11-05 11:18:30 +00:00
//Authentifié => on met à jour la session
} else {
2015-06-05 15:29:18 +00:00
$identity = $auth->getIdentity();
2013-11-05 11:18:30 +00:00
$identity->time = time() + $identity->timeout;
$auth->getStorage()->write($identity);
}
}
}
}
}