odea/library/Application/Controller/Plugin/Auth.php

181 lines
6.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 {
$layout = Zend_Layout::getMVCInstance();
//Authentifié => on met à jour la session
if ( $auth->hasIdentity() )
{
$identity = $auth->getIdentity();
$identity->time = time() + $identity->timeout;
if ( !isset($identity->dateContrat) && $layout->isEnabled() )
{
//Get customer's params
$paramsM = new Application_Model_CiblageCustomerParams();
$sql = $paramsM->select()
->where('idClient = ?', $identity->idClient)
//@todo : add service from WebService
->order('dateContrat DESC')->limit(1);
Zend_Registry::get('firebug')->info($sql->__toString());
$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_CiblagePrefs();
$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=='0000-00-00 00:00:00' ) {
//If no params are detected, display a message to contact support
$request->setModuleName('default')
->setControllerName('aide')
->setActionName('message')
->setParam('typeMsg', 'support');
}
//Check dateContrat and calculate end of contrat
else {
$hour = intval(substr($identity->dateContrat,11,2));
$min = intval(substr($identity->dateContrat,14,2));
$sec = intval(substr($identity->dateContrat,17,2));
$month = intval(substr($identity->dateContrat,5,2)) + $identity->periodContrat;
$day = intval(substr($identity->dateContrat,8,2));
$year = intval(substr($identity->dateContrat,0,4));
$dateFinContrat = mktime($hour,$min,$sec,$month,$day,$year);
if ( time() > $dateFinContrat ) {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout')
->setParam('message', "Votre Contrat est arrivé à expiration.");
}
}
$auth->getStorage()->write($identity);
if ( $identity->profil=="SuperAdministrateur" ) {
$view = $layout->getView();
$view->admin = true;
}
Zend_Registry::get('firebug')->info($identity);
if (Zend_Session::namespaceIsset('login')){
Zend_Session::namespaceUnset('login');
}
} elseif (time() > $auth->getIdentity()->time ) {
if ( $request->getControllerName()=='index' && $request->getActionName()=='index' ) {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('login');
} else {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout')
->setParam('ajax', $request->isXmlHttpRequest());
}
} else {
if ( !$request->isXmlHttpRequest() ) {
$session = new Zend_Session_Namespace('login');
$session->url = $_SERVER['REQUEST_URI'];
$request->setModuleName('default')
->setControllerName('user')
->setActionName('login');
} else {
$request->setModuleName('default')
->setControllerName('user')
->setActionName('logout')
->setParam('ajax', 1);
}
}
}
}
}
}