2012-07-30 08:29:06 +00:00

145 lines
4.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' || $request->getActionName()=='logout') ) {
$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());
$auth->setStorage($storage);
}
//Sinon on reste sur le standard
} else {
//Pas authentifié
if ( !$auth->hasIdentity() || time() > $auth->getIdentity()->time ) {
$auth->clearIdentity();
$storage = $auth->getStorage();
Zend_Session::namespaceUnset($storage->getNamespace());
$layout = Zend_Layout::getMVCInstance();
if ( !$layout->isEnabled() ) {
echo "Identification incorrect ou périmé.";
} else {
$this->_response->setRedirect('/user/login')->sendResponse();
}
//Authentifié => on met à jour la session
} else {
$identity = $auth->getIdentity();
$identity->time = time() + $identity->timeout;
$layout = Zend_Layout::getMVCInstance();
if ( !empty($identity->dateContrat) && $layout->isEnabled() )
{
//Get customer's params
$paramsM = new Application_Model_CustomerParams();
$sql = $paramsM->select()
->where('idClient = ?', $identity->idClient)
//@todo : add service from WebService
->order('dateContrat DESC')->limit(1);
$params = $paramsM->fetchAll($sql)->toArray();
if (count($params)>0) {
$identity->filterRNCS = $params[0]['filterRNCS'];
$identity->licenceINSEE = $params[0]['licenceINSEE'];
$identity->limitLines = $params[0]['limitLines'];
$identity->limitFiles = $params[0]['limitFiles'];
$identity->immediatExtract = $params[0]['immediatExtract'];
$identity->dateContrat = $params[0]['dateContrat'];
$identity->periodContrat = $params[0]['periodContrat'];
$identity->priceLine = $params[0]['priceLine'];
$identity->forfait = $params[0]['forfait'];
}
//Get user's preference
$prefsM = new Application_Model_Prefs();
$pref = $prefsM->find($identity->username)->current();
if ($pref) {
$identity->preferences = json_decode($pref->json, true);
}
//Add a global value to join filterRNCS and preferences RNCS
if ($identity->filterRNCS) {
$identity->globalRNCS = 1;
}
if (isset($identity->preferences['filter']['rncs']) && $identity->preferences['filter']['rncs']==1) {
$identity->globalRNCS = 1;
}
}
//No contrat define
if ( $identity->dateContrat!=0 ) {
//If no params are detected, display a message to contact support
$request->setModuleName('default')
->setControllerName('aide')
->setActionName('message')
->setParam('typeMsg', 'support');
}
//@todo : Check dateContrat and calculate end of contrat
$auth->getStorage()->write($identity);
if ( $identity->profil=="SuperAdministrateur" ) {
$layout = Zend_Layout::getMVCInstance();
$view = $layout->getView();
$view->admin = true;
}
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
}
}
}
}
}