From 346c66b4d4b189e7425792333ce9ef913db00383 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 14 Apr 2016 14:58:26 +0200 Subject: [PATCH 1/7] Log utilisateur --- library/WsScore/Gestion/v0.4/Service.php | 168 +++++++++++++++++++++-- library/WsScore/Gestion/v0.4/Types.php | 39 ++++++ 2 files changed, 196 insertions(+), 11 deletions(-) diff --git a/library/WsScore/Gestion/v0.4/Service.php b/library/WsScore/Gestion/v0.4/Service.php index daffd065..649cd8c8 100644 --- a/library/WsScore/Gestion/v0.4/Service.php +++ b/library/WsScore/Gestion/v0.4/Service.php @@ -33,7 +33,6 @@ class Gestion extends Scores_Ws_Server $this->authenticate(); // --- Enregistrement authentification OK - // @todo : Ajout userAgent try { $authLogM = new Application_Model_Sdv1UtilisateursAuthLog(); $authLogM->insert(array( @@ -45,26 +44,23 @@ class Gestion extends Scores_Ws_Server } catch (Zend_Db_Exception $e) {} - //Check App authorization - Find Service parameters + // --- Check App authorization - Find Service parameters $serviceM = new Application_Model_Sdv1ClientsServices(); $sql = $serviceM->select() ->where('IdClient=?', $this->User->idClient) ->where('Code=?', $this->User->serviceCode); $serviceParams = $serviceM->fetchRow($sql); - //Save information in database - //id, clientId, userId, login, ip, userAgent, dateLogin - //Enregistrement informations navigateur uniquement si bien identifié + // --- Enregistrement informations navigateur uniquement si bien identifié if ($browser !== null) { try { $browserLogM = new Application_Model_Sdv1UtilisateursBrowserLog(); $browserLogM->insert(array( - 'idClient' => $this->User->idClient, - 'idUser' => $this->User->id, + 'clientId' => $this->User->idClient, + 'userId' => $this->User->id, 'service' => $this->User->serviceCode, 'login' => $this->User->login, - 'authenticate' => 'OK', - 'ip' => $ip, + 'userAgent' => $browser, )); } catch (Zend_Db_Exception $e) {} } @@ -703,7 +699,7 @@ class Gestion extends Scores_Ws_Server return $users; } - protected function getUsersByFile($actif = null, $service = null, $client = null) + protected function getUsersFile($actif = null, $service = null, $client = null) { } @@ -869,7 +865,157 @@ class Gestion extends Scores_Ws_Server return $emails; } - protected function getUserLogByFile($id){} + /** + * Log des consultations + * @param string $month AAAAMM + * @param string $item + * Code de l'élément + * @param integer $p + * @param integer $limit + */ + public function getUserLog($month, $item, $p=0, $limit=50) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + $item = 'identite'; + + // --- Generate SQL + $logM = new Application_Model_Sdv1Logs(); + $logSql = $logM->select() + ->where('login=?', $this->User->login) + ->where('page=?', $item) + ->where('dateHeure BETWEEN "'.$selectedYear.'-'.$selectedMonth.'-00 00:00:00" AND "'. + $selectedYear.'-'.$selectedMonth.'-31 23:59:59"'); + + // --- Get total + $cols = new Zend_Db_Expr("COUNT(*) AS NB"); + $logSql->columns($cols); + $totalResult = $logM->fetchRow($logSql); + + $output = new UserLog(); + $output->Total = $totalResult->NB; + $output->List = array(); + + if ($totalResult->NB > 0) { + // --- Get Row + $logSql->columns(array('LPAD(siren,9,0) AS siren', 'LPAD(nic,5,0) AS nic', 'raisonSociale')); + $logSql->order('dateHeure DESC')->limitPage($p, $limit); + $logResult = $logM->fetchAll($logSql); + foreach ($logResult as $l) { + $struct = new UserLogItem(); + $companyId = $l->siren; + if (intval($l->nic) > 0) { + $companyId.= $l->nic; + } + $struct->CompanyId = $companyId; + $struct->CompanyType = 'SIREN'; + $struct->CompanyName = $l->raisonSociale; + $struct->Date = $l->dateHeure; + $output->List[] = $struct; + } + } + + return $output; + } + + protected function getUserLogFile($id){} + + /** + * Company User History (10 last) + * @param int $id + * @param int $limit + * @return UserLogItem[] + */ + public function getUserHistory($id=null, $limit=10) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + $output = array(); + + // --- Generate SQL + try { + $logSubquery = "SELECT siren, MAX(dateHeure) AS MaxDateHeure FROM logs WHERE login='".$this->User->login."' AND page='identite' GROUP BY siren"; + $logSql = "SELECT LPAD(x.siren,9,0) AS siren, LPAD(x.nic,5,0) AS nic, x.raisonSociale, x.dateHeure FROM logs x ". + "JOIN (".$logSubquery.") y ON y.siren = x.siren AND y.MaxDateHeure = x.dateHeure ". + "ORDER BY x.dateHeure DESC LIMIT 0,".$limit; + $db = Zend_Db_Table_Abstract::getDefaultAdapter(); + $db->setFetchMode(Zend_Db::FETCH_OBJ); + $logResult = $db->fetchAll($logSql); + foreach ($logResult as $l) { + $struct = new UserLogItem(); + $companyId = $l->siren; + if (intval($l->nic) > 0) { + $companyId.= $l->nic; + } + $struct->CompanyId = $companyId; + $struct->CompanyType = 'SIREN'; + $struct->CompanyName = $l->raisonSociale; + $struct->Date = $l->dateHeure; + $output[] = $struct; + } + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', 'Application Error.'); + } + } + + return $output; + } + + /** + * Log d'authentification + * @param string $month Date (AAAAMM) + * @param string $type (OK, KO) + * @param integer $p + * @param integer $limit + */ + public function getAuthLog($month, $type, $p=0, $limit=20) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + // --- Generate SQL + $logM = new Application_Model_Sdv1UtilisateursAuthLog(); + $logSql = $logM->select() + ->where('login=?', $this->User->login) + ->where('authenticate=?', 'OK') + ->where('dateInsert BETWEEN "'.$selectedYear.'-'.$selectedMonth.'-00 00:00:00" AND "'. + $selectedYear.'-'.$selectedMonth.'-31 23:59:59"'); + + // --- Get total + $cols = new Zend_Db_Expr("COUNT(*) AS NB"); + $logSql->columns($cols); + $totalResult = $logM->fetchRow($logSql); + + $output = new AuthLog(); + $output->Total = $totalResult->NB; + $output->List = array(); + + if ($totalResult->NB > 0) { + // --- Get Row + $logSql->columns(array('IP', 'dateInsert AS Date')); + $logsSql->order('dateInsert DESC')->limitPage($p, $limit); + $logResult = $logM->fetchAll($logSql); + foreach ($logResult as $item) { + $struct = new AuthLogItem(); + $struct->IP = $item->IP; + $struct->Date = $item->Date; + $output->List[] = $struct; + } + } + + return $output; + } /** * Définition d'un utilisateur diff --git a/library/WsScore/Gestion/v0.4/Types.php b/library/WsScore/Gestion/v0.4/Types.php index 2ae664ed..a238debe 100644 --- a/library/WsScore/Gestion/v0.4/Types.php +++ b/library/WsScore/Gestion/v0.4/Types.php @@ -251,3 +251,42 @@ class AuthParam /** @var string */ public $value; } + +class AuthLog +{ + /** @var int */ + public $Total; + + /** @var AuthLogItem[] */ + public $List; +} + +class AuthLogItem +{ + /** @var string */ + public $IP; + + /** @var string */ + public $Date; +} + +class UserLog +{ + /** @var int */ + public $Total; + + /** @var UserLogItem[] */ + public $List; +} + +class UserLogItem +{ + /** @var string */ + public $CompanyId; + + /** @var string */ + public $CompanyName; + + /** @var string */ + public $Date; +} From 8042bc869ea1860b12d14a5ca4e38f4f6e4685f0 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 3 May 2016 21:09:21 +0200 Subject: [PATCH 2/7] Comment --- library/WsScore/Gestion/v0.4/Service.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/WsScore/Gestion/v0.4/Service.php b/library/WsScore/Gestion/v0.4/Service.php index 649cd8c8..bca5f33d 100644 --- a/library/WsScore/Gestion/v0.4/Service.php +++ b/library/WsScore/Gestion/v0.4/Service.php @@ -594,7 +594,7 @@ class Gestion extends Scores_Ws_Server throw new SoapFault('MSG', "Impossible d'éditer le service"); } - //Acces + // Acces if ( $type == 'acces' ) { if ( in_array($value, $this->listeDroits) ) { @@ -602,7 +602,7 @@ class Gestion extends Scores_Ws_Server } } - //IP + // IP if ( $type == 'ip' ) { if ( $delete ) { @@ -1028,7 +1028,7 @@ class Gestion extends Scores_Ws_Server { $this->authenticate(); - //Vérification des droits de création d'utilisateur + // Vérification des droits de création d'utilisateur if ( $this->User->id != $id || !in_array($this->User->profil, array('SuperAdministrateur', 'Administrateur')) ) { throw new SoapFault('ERR', 'Accès non authorisé'); } From acc0c0b473112ce221a448ee37be7972eae42f42 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 12 May 2016 16:03:29 +0200 Subject: [PATCH 3/7] Change method name --- library/WsScore/Gestion/v0.4/Service.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/WsScore/Gestion/v0.4/Service.php b/library/WsScore/Gestion/v0.4/Service.php index bca5f33d..d97981f8 100644 --- a/library/WsScore/Gestion/v0.4/Service.php +++ b/library/WsScore/Gestion/v0.4/Service.php @@ -345,7 +345,7 @@ class Gestion extends Scores_Ws_Server return $client; } - protected function getContrats() + protected function getContratList() { //Liste des contrats, par service résumé @@ -373,7 +373,7 @@ class Gestion extends Scores_Ws_Server * Id client * @return ServiceList[] */ - public function getServices($client = null) + public function getServiceList($client = null) { //Liste des services $this->authenticate(); @@ -627,7 +627,7 @@ class Gestion extends Scores_Ws_Server * @param string $client Id du client * @return UserList[] */ - public function getUsers($actif = null, $service = null, $client = null) + public function getUserList($actif = null, $service = null, $client = null) { //Liste des utilisateurs - filtre au service $this->authenticate(); From c29a6f39060ed94a8098771f890a57c802026b1b Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 26 May 2016 09:39:32 +0200 Subject: [PATCH 4/7] Formattage --- application/controllers/ServiceController.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index c800fcfa..aaaaa475 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -192,12 +192,9 @@ class ServiceController extends Zend_Controller_Action // --- Pour débuggage ultime $debug = false; $debugUser = ''; - if ($debug && $_SERVER['PHP_AUTH_USER'] == $debugUser) - { + if ($debug && $_SERVER['PHP_AUTH_USER'] == $debugUser) { file_put_contents(APPLICATION_PATH . '/../debugcall.log', - "FichierWSDL : ".$fichierWsdl."\n". - "Hostname : ".$hostName."\n" - ); + "FichierWSDL : ".$fichierWsdl."\n"."Hostname : ".$hostName."\n"); $request = $server->getLastRequest(); file_put_contents(APPLICATION_PATH . '/../debugcall.log', $request . "\n", FILE_APPEND); $response = $server->getLastResponse(); From 6f0de74424480457ddfc6e7790de873a0bb6e806 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 26 May 2016 09:40:13 +0200 Subject: [PATCH 5/7] Gestion des erreurs SQL --- library/WsScore/Gestion/v0.4/Service.php | 111 ++++++++++++++--------- 1 file changed, 70 insertions(+), 41 deletions(-) diff --git a/library/WsScore/Gestion/v0.4/Service.php b/library/WsScore/Gestion/v0.4/Service.php index d97981f8..d871070d 100644 --- a/library/WsScore/Gestion/v0.4/Service.php +++ b/library/WsScore/Gestion/v0.4/Service.php @@ -62,7 +62,13 @@ class Gestion extends Scores_Ws_Server 'login' => $this->User->login, 'userAgent' => $browser, )); - } catch (Zend_Db_Exception $e) {} + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient==1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } } //Retourne un minimum d'information pour les applications @@ -382,21 +388,25 @@ class Gestion extends Scores_Ws_Server $client = $this->User->idClient; } - //Uniquement si l'utilisateur est administrateur et dans le service DEFAULT - if ( $this->User->profil != 'Administrateur' ) { + // Uniquement si l'utilisateur est administrateur et dans le service DEFAULT + if ( !in_array($this->User->profil, array('Administrateur', 'SuperAdministrateur')) ) { throw new SoapFault('ERR', 'Accès non authorisé'); } $serviceM = new Application_Model_Sdv1ClientsServices(); - $sql = $serviceM->select() - ->where('IdClient=?', $client) - ->where('Deleted=0'); - - $result = $serviceM->fetchAll($sql); - + try { + $serviceSql = $serviceM->select()->where('IdClient=?', $client) ->where('Deleted=?', 0); + $serviceResult = $serviceM->fetchAll($serviceSql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } $services = array(); - if ( count($result) > 0 ) { - foreach ( $result as $item ) { + if ( count($serviceResult) > 0 ) { + foreach ($serviceResult as $item) { $service = new ServiceList(); $service->id = $item->id; $service->IdClient = $item->IdClient; @@ -430,13 +440,18 @@ class Gestion extends Scores_Ws_Server $client = $this->User->idClient; - //Détail d'un service + // Détail d'un service $serviceM = new Application_Model_Sdv1ClientsServices(); - $sql = $serviceM->select() - ->where('IdClient=?', $client) - ->where('Deleted=?', 0) - ->where('id=?', $id); - $result = $serviceM->fetchRow($sql); + try { + $sql = $serviceM->select()->where('IdClient=?', $client)->where('Deleted=?', 0)->where('id=?', $id); + $result = $serviceM->fetchRow($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } if ( $result === null ) { throw new SoapFault('ERR', 'Service introuvable.'); @@ -447,7 +462,7 @@ class Gestion extends Scores_Ws_Server $output->Code = $result->Code; $output->Label = $result->Label; $output->TypeCompte = $result->TypeCompte; - $output->TypeAcess = $result->TypeAcess; + $output->TypeAcces = $result->TypeAcces; $output->TypeScore = $result->TypeScore; $output->Timeout = $result->Timeout; $output->Editable = $result->Editable; @@ -455,14 +470,22 @@ class Gestion extends Scores_Ws_Server $output->DateInsert = $result->DateInsert; $output->DateUpdate = $result->DateUpdate; - //Droits + $serviceCode = $result->Code; + + // Droits $output->Acces = array(); - $serviceDroitsM = new Application_Model_Sdv1ClientsServicesDroits(); - $sql = $serviceDroitsM->select() - ->where('IdClient=?', $client) - ->where('Service=?', $serviceCode); - $result = $serviceDroitsM->fetchAll($sql); - if ( count($result)>0 ) { + $serviceDroitsM = new Application_Model_Sdv1ClientsServicesDroits(); + try { + $sql = $serviceDroitsM->select()->where('IdClient=?', $client)->where('Service=?', $serviceCode); + $result = $serviceDroitsM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if (count($result) > 0) { foreach ( $result as $item ) { $acces = new AccesDetails(); $acces->Code = $item->Acces; @@ -471,15 +494,21 @@ class Gestion extends Scores_Ws_Server } } - //IP + // IP $output->IP = array(); $serviceIPM = new Application_Model_Sdv1ClientsServicesIP(); - $sql = $serviceIPM->select() - ->where('IdClient=?', $client) - ->where('Service=?', $serviceCode); - $result = $serviceDroitsM->fetchAll($sql); - if ( count($result)>0 ) { - foreach ( $result as $item ) { + try { + $sql = $serviceIPM->select()->where('IdClient=?', $client)->where('Service=?', $serviceCode); + $result = $serviceIPM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if (count($result) > 0) { + foreach ($result as $item) { $output->IP[] = $item->IP; } } @@ -629,19 +658,19 @@ class Gestion extends Scores_Ws_Server */ public function getUserList($actif = null, $service = null, $client = null) { - //Liste des utilisateurs - filtre au service + // Liste des utilisateurs - filtre au service $this->authenticate(); if ( $client === null ) { $client = $this->User->idClient; } - //Administrateur + // Administrateur if ( !in_array($this->User->profil, array('Administrateur', 'SuperAdministrateur')) ) { throw new SoapFault('ERR', 'Accès non authorisé'); } - //Administrateur d'un service + // Administrateur d'un service if ( $this->User->Service != '' && $this->User->Service !== null && $this->User->Service !== 'DEFAULT' ) { $service = $this->User->Service; } @@ -653,17 +682,17 @@ class Gestion extends Scores_Ws_Server ->from(array('u'=>'sdv1.utilisateurs'), array('id','idClient','login','email','civilite','nom','prenom','actif','deleted')) ->joinLeft(array('s'=>'sdv1.utilisateurs_service'), 'u.login=s.login', array('Service')) ->joinLeft(array('sd'=>'sdv1.clients_services'), 'sd.Code=s.Service', array('Label')) - ->where('u.idClient=?',$client); + ->where('u.idClient=?', $client); if ( $actif !== null && in_array($actif, array(0,1)) ) { $sql->where('u.actif=?', $actif); } - $sql->where('u.deleted=?',0); + $sql->where('u.deleted=?', 0); - if ($service == 'DEFAULT') { - $sql->where('(s.Service IS NULL AND u.idClient='.$client.') OR (s.Service IS NOT NULL AND u.idClient='.$client.' AND u.idClient='.$client.' AND sd.idClient='.$client.') OR sd.Code="'.$service.'"'); - } else if ( $service !== null ) { - $sql->where('s.Service=?', $service); + if ($service->Code == 'DEFAULT') { + $sql->where('(s.Service IS NULL) OR (s.Service="DEFAULT" AND sd.idClient='.$client.') OR sd.Code='.$service); + } else { + $sql->where('sd.Code=?', $service); } $result = $userM->fetchAll($sql); From fd3e43e8cd6914e63ce1508876c9953537334894 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Thu, 26 May 2016 17:43:57 +0200 Subject: [PATCH 6/7] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20fiche=20service=20?= =?UTF-8?q?par=20le=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/WsScore/Gestion/v0.4/Service.php | 113 +++++++++++------------ 1 file changed, 54 insertions(+), 59 deletions(-) diff --git a/library/WsScore/Gestion/v0.4/Service.php b/library/WsScore/Gestion/v0.4/Service.php index d871070d..8c4e800c 100644 --- a/library/WsScore/Gestion/v0.4/Service.php +++ b/library/WsScore/Gestion/v0.4/Service.php @@ -430,11 +430,11 @@ class Gestion extends Scores_Ws_Server /** * Détail d'un service - * @param int $id + * @param string $code * @throws SoapFault * @return Service */ - public function getService($id) + public function getService($code) { $this->authenticate(); @@ -443,7 +443,7 @@ class Gestion extends Scores_Ws_Server // Détail d'un service $serviceM = new Application_Model_Sdv1ClientsServices(); try { - $sql = $serviceM->select()->where('IdClient=?', $client)->where('Deleted=?', 0)->where('id=?', $id); + $sql = $serviceM->select()->where('IdClient=?', $client)->where('Deleted=?', 0)->where('Code=?', $code); $result = $serviceM->fetchRow($sql); } catch (Zend_Db_Exception $e) { if ($this->User->idClient == 1) { @@ -459,6 +459,7 @@ class Gestion extends Scores_Ws_Server $output = new Service(); $output->id = $result->id; + $output->IdClient = $client; $output->Code = $result->Code; $output->Label = $result->Label; $output->TypeCompte = $result->TypeCompte; @@ -689,12 +690,11 @@ class Gestion extends Scores_Ws_Server } $sql->where('u.deleted=?', 0); - if ($service->Code == 'DEFAULT') { - $sql->where('(s.Service IS NULL) OR (s.Service="DEFAULT" AND sd.idClient='.$client.') OR sd.Code='.$service); + if ($service == 'DEFAULT') { + $sql->where('(s.Service IS NULL) OR (s.Service="DEFAULT" AND sd.idClient='.$client.')'); } else { $sql->where('sd.Code=?', $service); } - $result = $userM->fetchAll($sql); } catch (Zend_Db_Exception $e) { if ($this->User->idClient == 1) { @@ -810,8 +810,8 @@ class Gestion extends Scores_Ws_Server if ( count($droits) > 0 ) { foreach ($droits as $item) { $acces = new Acces(); - $acces->Code = $item->Acces; - $acces->Label = $this->listeDroits[$item->Acces]; + $acces->Code = strtoupper($item->Acces); + $acces->Label = $this->listeDroits[strtoupper($item->Acces)]; $output->Acces[] = $acces; } } @@ -827,7 +827,6 @@ class Gestion extends Scores_Ws_Server } } - // Service - IP try { $ipM = new Application_Model_Sdv1ClientsServicesIP(); @@ -1047,7 +1046,7 @@ class Gestion extends Scores_Ws_Server } /** - * Définition d'un utilisateur + * Paramétrage d'un utilisateur * @param string $data * @param int $id * @throws SoapFault @@ -1057,75 +1056,52 @@ class Gestion extends Scores_Ws_Server { $this->authenticate(); - // Vérification des droits de création d'utilisateur + // --- Vérification des droits de création d'utilisateur if ( $this->User->id != $id || !in_array($this->User->profil, array('SuperAdministrateur', 'Administrateur')) ) { throw new SoapFault('ERR', 'Accès non authorisé'); } - $infos = json_decode($data); + $values = json_decode($data); $userM = new Application_Model_Sdv1Utilisateurs(); $userData = array( - 'idClient', - 'login', - 'email', + 'idClient' => $values['idClient'], + 'login' => trim($values['login']), + 'email' => strtolower(trim($values['email'])), 'password' => '', - 'actif' => 0, + 'actif' => 1, 'deleted' => 0, 'typeCompte' => 'PROD', - 'filtre_ip' => '', - 'civilite' => 'M', - 'nom' => '', - 'prenom' => '', - 'raisonSociale' => '', - 'siret' => '000000000', - 'adrNum' => null, - 'adrIndRep' => null, - 'adrTypeVoie' => null, - 'adrLibVoie' => '', - 'adrCp' => null, - `adrVille` => null, - `adrComp` => '', - 'tel' => '', - 'fax' => null, - 'mobile' => null, - 'pref' => '', + 'civilite' => $values['civilite'], + 'nom' => trim($values['nom']), + 'prenom' => trim($values['prenom']), 'profil' => 'Utilisateur', - 'dateInscription' => '0000-00-00 00:00:00', - 'dateValidation' => '0000-00-00 00:00:00', - 'nombreConnexions' => '0', - 'dateDerniereConnexion' => '0000-00-00 00:00:00', 'droits' => '', 'referenceParDefaut' => '', - 'nbReponses' => '020', - 'lang' => null, - 'formatMail' => 'txt1', + 'nbReponses' => 10, + 'lang' => 'fr', + 'formatMail' => 'txt2', 'lienExtranetMail' => 0, 'lienSurvFic' => 0, 'idSurvFic' => '', 'loginCptSurvFic' => '', 'listeEven' => '', - 'dateDebutCompte' => null, - 'dateFinCompte' => null, - 'maxFicheId' => null, - 'accesWS' => '0', - 'rechRefType' => 'UTI', - 'acceptationCGU' => null, ); + // --- Création if ( $id === null ) { - $sql = $userM->select()->where('login=?', $infos->login); + $sql = $userM->select()->where('login=?', $values['login']); $row = $userM->fetchRow($sql); - //Utilisateur existant + // --- Utilisateur existant if ( null !== $row ) { throw new SoapFault('ERR', "User exist"); } - //Prepare data to insert - foreach ($infos as $key => $value) { + // --- Prepare data to insert + foreach ($values as $key => $value) { if (array_key_exists($key, $userData)) { - $userData[$key] = $value; + $userData[$key] = trim($value); } } $userData['dateInsert'] = date('YmdHis'); @@ -1150,22 +1126,24 @@ class Gestion extends Scores_Ws_Server return true; - } else { + } + // --- Modification + else { $sql = $userM->select()->where('id=?', $id); $row = $userM->fetchRow($sql); if ( null === $row ) { throw new SoapFault('ERR', "User doesn't exist"); } - //Prepare data to update + // --- Prepare data to update foreach ($row as $key => $value) { if (array_key_exists($key, $userData)) { - $userData[$key] = $value; + $userData[$key] = trim($value); } } - foreach ($infos as $key => $value) { + foreach ($values as $key => $value) { if (array_key_exists($key, $userData)) { - $userData[$key] = $value; + $userData[$key] = trim($value); } } @@ -1326,10 +1304,14 @@ class Gestion extends Scores_Ws_Server return false; } + /** + * Catégorie et Accès + * @return AccesCategory[] + */ public function getCategory() { $output = array(); - foreach ( $this->listeCategory as $code => $desc ) { + foreach ($this->listeCategory as $code => $desc) { $c = new AccesCategory(); $c->Code = $code; $c->Label = $desc['label']; @@ -1339,9 +1321,22 @@ class Gestion extends Scores_Ws_Server return $output; } - protected function getAccess() + /** + * Liste des acces + * @return Acces[] + */ + public function getAccess() { - //Liste des accès - Code, Label, Category, Description, + $droits = $this->listeDroits; + $list = array(); + foreach ($droits as $k => $v) { + $acces = new Acces(); + $acces->Code = $k; + $acces->Label = $v; + $list[] = $acces; + } + + return $list; } protected function getPref() From cd599e01493652f58d0710427a77070ad71349d7 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Wed, 1 Jun 2016 16:29:45 +0200 Subject: [PATCH 7/7] =?UTF-8?q?Nouveau=20service=20Account,=20bas=C3=A9=20?= =?UTF-8?q?sur=20Gestion=20v0.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/WsScore/Account/v0.1/Config.php | 17 + library/WsScore/Account/v0.1/Service.php | 1434 ++++++++++++++++++++++ library/WsScore/Account/v0.1/Types.php | 292 +++++ library/WsScore/ServicesConfig.php | 7 + 4 files changed, 1750 insertions(+) create mode 100644 library/WsScore/Account/v0.1/Config.php create mode 100644 library/WsScore/Account/v0.1/Service.php create mode 100644 library/WsScore/Account/v0.1/Types.php diff --git a/library/WsScore/Account/v0.1/Config.php b/library/WsScore/Account/v0.1/Config.php new file mode 100644 index 00000000..07c95de8 --- /dev/null +++ b/library/WsScore/Account/v0.1/Config.php @@ -0,0 +1,17 @@ + 'Client', + 'ClientServices' => 'ClientServices', + 'ClientServicesList' => 'ClientServicesList', + 'ServiceList' => 'ServiceList', + 'Service' => 'Service', + 'UserList' => 'UserList', + 'User' => 'User', + 'Acces' => 'Acces', + 'AccesCategory' => 'AccesCategory', + 'AuthParam' => 'AuthParam', + 'AuthLog' => 'AuthLog', + 'AuthLogItem' => 'AuthLogItem', + 'UserLog' => 'UserLog', + 'UserLogItem' => 'UserLogItem', +); diff --git a/library/WsScore/Account/v0.1/Service.php b/library/WsScore/Account/v0.1/Service.php new file mode 100644 index 00000000..1f0527fb --- /dev/null +++ b/library/WsScore/Account/v0.1/Service.php @@ -0,0 +1,1434 @@ +authApp = 'extranet'; + $this->authIp = $ip; + break; + case 'odea': + $this->authApp = 'odea'; + $this->authIp = $ip; + break; + case 'starlinks': + $this->authApp = 'starlinks'; + $this->authIp = $ip; + break; + } + + // --- Authentification des applications par login + $this->authenticate(); + + // --- Enregistrement authentification OK + try { + $authLogM = new Application_Model_Sdv1UtilisateursAuthLog(); + $authLogM->insert(array( + 'login' => $login, + 'authenticate' => 'OK', + 'ip' => $ip, + 'dateInsert' => date('YmdHis'), + )); + } catch (Zend_Db_Exception $e) {} + + + // --- Check App authorization - Find Service parameters + $serviceM = new Application_Model_Sdv1ClientsServices(); + $sql = $serviceM->select() + ->where('IdClient=?', $this->User->idClient) + ->where('Code=?', $this->User->serviceCode); + $serviceParams = $serviceM->fetchRow($sql); + + // --- Enregistrement informations navigateur uniquement si bien identifié + if ($browser !== null) { + try { + $browserLogM = new Application_Model_Sdv1UtilisateursBrowserLog(); + $browserLogM->insert(array( + 'clientId' => $this->User->idClient, + 'userId' => $this->User->id, + 'service' => $this->User->serviceCode, + 'login' => $this->User->login, + 'userAgent' => $browser, + )); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient==1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + } + + //Retourne un minimum d'information pour les applications + //login + //civilite + //nom + //prenom + //email + //profil + //pref + //droits + //typeScore + //acceptationCGU + + } + + /** + * Authentication by key + * @param string $app + */ + protected function keyAuthenticate($app) + { + //Authentification par clé - @define + $this->authApp = 'extranet'; + + //Reception du login + + //Reception de la clé (key:md5(login + '|' + key)) + + //Vérification nécessaire : application - enable - date debut - date fin + + + + } + + /** + * Authentication by SSO + * @param int $client + * @param string $login + * @param string $token + * @param AuthParam[] $params + * @throws SoapFault + * @return mixed + * Retourne FALSE or HASH to connect + */ + public function ssoAuthenticate($client, $login, $token, $params) + { + /** + * login ? + * nom@mon.tld => extract nom + */ + $part = strstr($login, '@', true); + if ($part !== false) { + $login = $part; + } + + /** + * Extract login from database + */ + try { + $userM = new Application_Model_Sdv1Utilisateurs(); + $sql = $userM->select() + ->setIntegrityCheck(false) + ->from(array('u'=>'utilisateurs'), array('u.id', 'u.login', 'u.idClient', 'u.actif',)) + ->join(array('c'=>'clients'), 'u.idClient = c.id', array('c.actif AS clientActif')) + ->joinLeft(array('s'=>'sdv1.utilisateurs_service'), 'u.login=s.login', array('Service AS serviceCode')) + ->where('u.login=?', $login) + ->where('c.id=?', $client); + $result = $userM->fetchRow($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient==1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + /** + * Client disable + */ + if ( $result !== null && $result->clientActif == 'Non') { + throw new SoapFault('SSO', 'Compte client inactif'); //Client désactivé + } + + /** + * No user + */ + if ( null === $result ) { + return false; //Utilisateur inexistant + } + + /** + * User not activated + */ + if ( $result->actif == 0) { + throw new SoapFault('SSO', 'Utilisateur non activé'); //Utilisateur non activé + } + + /** + * Process token - uniquement compatible In Extenso + */ + $key = 'rh5s4z'; + $maxTime = time() + (15 * 60); + $time = 0; + $user = ''; + if (count($params->item) > 0) { + foreach ($params->item as $k => $param) { + if ($param->label == 'time') { + $time = $param->value; + } + if ($param->label == 'mail') { + $user = $param->value; + } + } + } + + /** + * Limite de temps + */ + if ($time > $maxTime) { + throw new SoapFault('SSO', 'Délai dépassé pour la connexion'); //Limite de temps dépassé pour la connexion + } + + /** + * Check token + */ + $data = $user . '/' . $time . '/' . $key; + $internalToken = hash('sha256', $data); + //file_get_contents('inextenso.log', $internalToken .' = '. $token."\n", FILE_APPEND); + if ($internalToken == $token) { + //Generate random password + $hash = password_hash ( uniqid() , PASSWORD_BCRYPT ); + try { + $userM->update(array('password' => $hash), 'id='.$result->id); + } catch (Zend_Db_Exception $e) { + throw new SoapFault('SSO', "Activation de l'utilisateur impossible"); + } + return $hash; + } + + throw new SoapFault('SSO', 'Token invalide'); //Token invalide + } + + /** + * Liste des clients + * @throws SoapFault + * @return Client[] + */ + public function getClients() + { + $this->authenticate(); + + if ( $this->User->idClient != 1 ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + $clientM = new Application_Model_Sdv1Clients(); + $sql = $clientM->select(true)->columns(array( + 'nom AS Nom', + 'LPAD(siren,9,0) AS Siren', + 'LPAD(nic,5,0) AS Nic', + 'IF(actif="Oui",1,0) AS Actif', + 'IF(test="Oui",1,0) AS Test' + ))->where('actif=?', 'Oui'); + $result = $clientM->fetchAll($sql); + + if ( count($result) > 0 ) { + foreach ( $result as $item ) { + $client = new Client(); + $client->Nom = $item->Nom; + $client->Siren = $item->Siren; + $client->Nic = $item->Nic; + $client->Actif = $item->Actif; + $client->Test = $item->Test; + + $clients[] = $client; + } + } + + return $clients; + } + + /** + * Information client + * @param int $id + * ID du client + * @throws SoapFault + * @return Client + */ + public function getClient($id = null) + { + $this->authenticate(); + + if ( $id === null || $this->User->idClient != 1 ) { + $id = $this->User->idClient; + } + + $clientM = new Application_Model_Sdv1Clients(); + $sql = $clientM->select(true)->columns(array( + 'nom AS Nom', + 'LPAD(siren,9,0) AS Siren', + 'LPAD(nic,5,0) AS Nic', + 'IF(actif="Oui",1,0) AS Actif', + 'IF(test="Oui",1,0) AS Test' + ))->where('id=?', $id); + $result = $clientM->fetchRow($sql); + + if ( $result === null ) { + throw new SoapFault('ERR', 'Information client introuvable.'); + } + + $client = new Client(); + $client->Nom = $result->Nom; + $client->Siren = $result->Siren; + $client->Nic = $result->Nic; + $client->Actif = $result->Actif; + $client->Test = $result->Test; + + return $client; + } + + /** + * Information client avec la liste des services + * @param int $id + * ID du client + * @throws SoapFault + * @return ClientServices + */ + public function getClientServices($id = null) + { + $this->authenticate(); + + if ( $id === null || $this->User->idClient != 1 ) { + $id = $this->User->idClient; + } + + $clientM = new Application_Model_Sdv1Clients(); + $sql = $clientM->select()->from($clientM, array( + 'nom AS Nom', + 'LPAD(siren,9,0) AS Siren', + 'LPAD(nic,5,0) AS Nic', + 'IF(actif="Oui",1,0) AS Actif', + 'IF(test="Oui",1,0) AS Test' + ))->where('id=?', $id); + $result = $clientM->fetchRow($sql); + + if ( $result === null ) { + throw new SoapFault('ERR', 'Information client introuvable.'); + } + + $client = new ClientServices(); + $client->Nom = $result->Nom; + $client->Siren = $result->Siren; + $client->Nic = $result->Nic; + $client->Actif = $result->Actif; + $client->Test = $result->Test; + + //Get Services + $serviceM = new Application_Model_Sdv1ClientsServices(); + $sql = $serviceM->select() + ->from($serviceM, array('id', 'Code', 'Label', 'Editable', 'Active')) + ->where('Deleted=?', 0) + ->where('Active=?', 1) + ->where('IdClient=?', $id); + $result = $serviceM->fetchAll($sql); + $services = array(); + if (count($result) > 0) { + foreach ($result as $item) { + $clientService = new ClientServicesList(); + $clientService->id = $item->id; + $clientService->Code = $item->Code; + $clientService->Label = $item->Label; + $clientService->Editable = $item->Editable; + $clientService->Active = $item->Active; + $services[] = $clientService; + } + } + $client->Services = $services; + + return $client; + } + + protected function getContratList() + { + //Liste des contrats, par service résumé + + //Vérification des droits d'utilisation + if ( $this->User->profil != 'Administrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + } + + protected function getContrat($id) + { + //Détail d'un contrat + + //Vérification des droits d'utilisation + if ( $this->User->profil != 'Administrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + } + + /** + * Liste des services + * @param string $client + * Id client + * @return ServiceList[] + */ + public function getServiceList($client = null) + { + //Liste des services + $this->authenticate(); + + if ( $client === null ) { + $client = $this->User->idClient; + } + + // Uniquement si l'utilisateur est administrateur et dans le service DEFAULT + if ( !in_array($this->User->profil, array('Administrateur', 'SuperAdministrateur')) ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + $serviceM = new Application_Model_Sdv1ClientsServices(); + try { + $serviceSql = $serviceM->select()->where('IdClient=?', $client) ->where('Deleted=?', 0); + $serviceResult = $serviceM->fetchAll($serviceSql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + $services = array(); + if ( count($serviceResult) > 0 ) { + foreach ($serviceResult as $item) { + $service = new ServiceList(); + $service->id = $item->id; + $service->IdClient = $item->IdClient; + $service->Code = $item->Code; + $service->Label = $item->Label; + $service->TypeCompte = $item->TypeCompte; + $service->TypeAcces = $item->TypeAcces; + $service->TypeScore = $item->TypeScore; + $service->Timeout = $item->Timeout; + $service->Editable = $item->Editable; + $service->Active = $item->Active; + $service->DateInsert = $item->DateInsert; + $service->DateUpdate = $item->DateUpdate; + + $services[] = $service; + } + } + + return $services; + } + + /** + * Détail d'un service + * @param string $code + * @throws SoapFault + * @return Service + */ + public function getService($code) + { + $this->authenticate(); + + $client = $this->User->idClient; + + // Détail d'un service + $serviceM = new Application_Model_Sdv1ClientsServices(); + try { + $sql = $serviceM->select()->where('IdClient=?', $client)->where('Deleted=?', 0)->where('Code=?', $code); + $result = $serviceM->fetchRow($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + if ( $result === null ) { + throw new SoapFault('ERR', 'Service introuvable.'); + } + + $output = new Service(); + $output->id = $result->id; + $output->IdClient = $client; + $output->Code = $result->Code; + $output->Label = $result->Label; + $output->TypeCompte = $result->TypeCompte; + $output->TypeAcces = $result->TypeAcces; + $output->TypeScore = $result->TypeScore; + $output->Timeout = $result->Timeout; + $output->Editable = $result->Editable; + $output->Active = $result->Active; + $output->DateInsert = $result->DateInsert; + $output->DateUpdate = $result->DateUpdate; + + $serviceCode = $result->Code; + + // Droits + $output->Acces = array(); + $serviceDroitsM = new Application_Model_Sdv1ClientsServicesDroits(); + try { + $sql = $serviceDroitsM->select()->where('IdClient=?', $client)->where('Service=?', $serviceCode); + $result = $serviceDroitsM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if (count($result) > 0) { + foreach ( $result as $item ) { + $acces = new AccesDetails(); + $acces->Code = $item->Acces; + $acces->Label = $this->listeDroits[$item->Acces]; + $output->Acces[] = $acces; + } + } + + // IP + $output->IP = array(); + $serviceIPM = new Application_Model_Sdv1ClientsServicesIP(); + try { + $sql = $serviceIPM->select()->where('IdClient=?', $client)->where('Service=?', $serviceCode); + $result = $serviceIPM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if (count($result) > 0) { + foreach ($result as $item) { + $output->IP[] = $item->IP; + } + } + + return $output; + } + + protected function getServiceConso($id){} + + protected function getServiceLogByFile(){} + + /** + * Modification des éléments d'un service par un administrateur + * @param string $code Element à modifier (label | active | delete) + * @param mixed $value Valeur + * @param string $id Id du service + * @throws SoapFault + * @return boolean + */ + public function setService($code, $value, $id) + { + $this->authenticate(); + + //Vérification des droits d'utilisation + if ( $this->User->profil != 'Administrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + $client = $this->User->idClient; + + //Définir les éléments du service + $serviceM = new Application_Model_Sdv1ClientsServices(); + $sql = $serviceM->select() + ->where('IdClient=?', $client) + ->where('Deleted=?', 0) + ->where('id=?', $id); + $result = $serviceM->fetchRow($sql); + + if ( $result === null ) { + throw new SoapFault('ERR', 'Service introuvable.'); + } + + if ( $result->Editable == 0) { + throw new SoapFault('MSG', "Impossible d'éditer le service"); + } + + $data = json_decode($data); + $dataToUpdate = array(); + + //Suppression d'un service + if ( $code == 'delete' && $value==1 ) { + $dataToUpdate = array('Deleted'=>1); + } + + if ( $code == 'active' && in_array($value,array(0,1)) ) { + $dataToUpdate = array('Active'=>$value); + } + + if ( $code == 'label' && is_string($value) ) { + $dataToUpdate = array('Label'=>$value); + } + + if ( count($dataToUpdate) > 0 ) { + $dataToUpdate['DateUpdate'] = date('YmdHis'); + try { + $serviceM->update($dataToUpdate, 'id='.$id); + return true; + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient==1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + } + + return false; + } + + /** + * Définit les paramètres d'un service + * @param string $type + * @param string $value + * @param string $id + * @throws SoapFault + * @return boolean + */ + public function setServiceParam($type, $value, $id, $delete = false) + { + //Définir un paramètre du service + $this->authenticate(); + + //Vérification des droits d'utilisation + if ( $this->User->profil != 'Administrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + $client = $this->User->idClient; + + $serviceM = new Application_Model_Sdv1ClientsServices(); + $sql = $serviceM->select() + ->where('IdClient=?', $client) + ->where('Deleted=?', 0) + ->where('id=?', $id); + $result = $serviceM->fetchRow($sql); + + if ( $result === null ) { + throw new SoapFault('ERR', 'Service introuvable.'); + } + + if ( $result->Editable == 0) { + throw new SoapFault('MSG', "Impossible d'éditer le service"); + } + + // Acces + if ( $type == 'acces' ) { + + if ( in_array($value, $this->listeDroits) ) { + + } + } + + // IP + if ( $type == 'ip' ) { + + if ( $delete ) { + + + } else { + //Control de la plage IP ou de l'IP + $validate = new Zend_Validate_Ip(); + if ( $validate->isValid($value) ) { + + } + } + } + + return false; + } + + /** + * Liste des utilisateurs + * @param int $actif (0|1) + * @param string $service Code du service + * @param string $client Id du client + * @return UserList[] + */ + public function getUserList($actif = null, $service = null, $client = null) + { + // Liste des utilisateurs - filtre au service + $this->authenticate(); + + if ( $client === null ) { + $client = $this->User->idClient; + } + + // Administrateur + if ( !in_array($this->User->profil, array('Administrateur', 'SuperAdministrateur')) ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + // Administrateur d'un service + if ( $this->User->Service != '' && $this->User->Service !== null && $this->User->Service !== 'DEFAULT' ) { + $service = $this->User->Service; + } + + try { + $userM = new Application_Model_Sdv1Utilisateurs(); + $sql = $userM->select() + ->setIntegrityCheck(false) + ->from(array('u'=>'sdv1.utilisateurs'), array('id','idClient','login','email','civilite','nom','prenom','actif','deleted')) + ->joinLeft(array('s'=>'sdv1.utilisateurs_service'), 'u.login=s.login', array('Service')) + ->joinLeft(array('sd'=>'sdv1.clients_services'), 'sd.Code=s.Service', array('Label')) + ->where('u.idClient=?', $client); + + if ( $actif !== null && in_array($actif, array(0,1)) ) { + $sql->where('u.actif=?', $actif); + } + $sql->where('u.deleted=?', 0); + + if ($service == 'DEFAULT') { + $sql->where('(s.Service IS NULL) OR (s.Service="DEFAULT" AND sd.idClient='.$client.')'); + } else { + $sql->where('sd.Code=?', $service); + } + $result = $userM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + $users = array(); + if (count($result) > 0) { + foreach ($result as $item) { + $user = new UserList(); + $user->id = $item->id; + $user->IdClient = $item->idClient; + $user->ServiceCode = $item->Service; + $user->ServiceLabel = $item->Label; + $user->Login = $item->login; + $user->Email = $item->email; + $user->Civilite = $item->civilite; + $user->Nom = $item->nom; + $user->Prenom = $item->prenom; + $user->Enable = $item->actif; + $user->Delete = $item->deleted; + + $users[] = $user; + + } + } + + return $users; + } + + protected function getUsersFile($actif = null, $service = null, $client = null) + { + + } + + /** + * Information Utilisateur + * @param string $id + * ID de l'utilisateur + * @throws SoapFault + * @return User + */ + public function getUser($id = null) + { + //Détail d'un utilisateur + $this->authenticate(); + + $idClient = $this->User->idClient; + + if ( $id === null ) { + $id = $this->User->id; + } + + // Get Data + try { + $userM = new Application_Model_Sdv1Utilisateurs(); + $sql = $userM->select()->from(array('u'=>'utilisateurs')) + ->setIntegrityCheck(false) + ->joinLeft(array('s'=>'sdv1.utilisateurs_service'), 'u.login=s.login', array('Service')) + ->joinLeft(array('sd'=>'sdv1.clients_services'), 'sd.Code=s.Service', array('Label AS ServiceLabel')) + ->where('u.id=?', $id); + + $user = $userM->fetchRow($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + if ( $user === null ) { + throw new SoapFault('ERR', 'Utilisateur inexistant !'); + } + + // Service + $serviceCode = 'DEFAULT'; + $serviceLabel = 'Standard'; + if ( $user->Service !== null ) { + $serviceCode = $user->Service; + $serviceLabel = $user->ServiceLabel; + } + + $output = new User(); + $output->id = $user->id; + $output->IdClient = $user->idClient; + $output->ServiceCode = $serviceCode; + $output->ServiceLabel = $serviceLabel; + $output->Login = $user->login; + $output->Email = $user->email; + $output->Civilite = $user->civilite; + $output->Nom = $user->nom; + $output->Prenom = $user->prenom; + $output->Enable = $user->actif; + $output->Delete = $user->deleted; + + // Service - Droits + $acces = array(); + try { + $droitsM = new Application_Model_Sdv1ClientsServicesDroits(); + $sql = $droitsM->select()->where('IdClient=?', $idClient)->where('Service=?', $serviceCode); + $droits = $droitsM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if ( count($droits) > 0 ) { + foreach ($droits as $item) { + $acces = new Acces(); + $acces->Code = strtoupper($item->Acces); + $acces->Label = $this->listeDroits[strtoupper($item->Acces)]; + $output->Acces[] = $acces; + } + } + + // Si l'utilisateur a pour Service = DEFAULT et pas de service DEFAULT alors droits de l'utilisateur + if ( count($droits) == 0 ) { + $droits = explode(' ', $user->droits); + foreach ($droits as $item) { + $acces = new Acces(); + $acces->Code = $item; + $acces->Label = $this->listeDroits[strtoupper($item)]; + $output->Acces[] = $acces; + } + } + + // Service - IP + try { + $ipM = new Application_Model_Sdv1ClientsServicesIP(); + $sql = $ipM->select()->where('IdClient=?', $idClient)->where('Service=?', $serviceCode); + $ips = $ipM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + if ( count($ips) > 0 ) { + foreach ($ips as $item) { + $output->IP[] = $item; + } + } + + return $output; + } + + protected function getUserActivity($id = null) + { + //Retourner la liste des dernières connexions + } + + /** + * Emails secondaires + * @param int $id + * @throws SoapFault + * @return string[] + */ + public function getUserEmail($id) + { + $this->authenticate(); + + $idClient = $this->User->idClient; + + try { + $emailsM = new Application_Model_Sdv1UtilisateursEmails(); + $sql = $emailsM->select() + ->where('id=?', $id) + ->where('idClient=?', $idClient); + $result = $emailsM->fetchAll($sql); + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient==1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + $emails = array(); + if ( count($result)>0 ) { + foreach ( $result as $item ) { + $email = new Email(); + $email->id = $item->id; + $email->value = $item->email; + + $emails[] = $email; + } + } + + return $emails; + } + + /** + * Log des consultations + * @param string $month AAAAMM + * @param string $item + * Code de l'élément + * @param integer $p + * @param integer $limit + */ + public function getUserLog($month, $item, $p=0, $limit=50) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + $item = 'identite'; + + // --- Generate SQL + $logM = new Application_Model_Sdv1Logs(); + $logSql = $logM->select() + ->where('login=?', $this->User->login) + ->where('page=?', $item) + ->where('dateHeure BETWEEN "'.$selectedYear.'-'.$selectedMonth.'-00 00:00:00" AND "'. + $selectedYear.'-'.$selectedMonth.'-31 23:59:59"'); + + // --- Get total + $cols = new Zend_Db_Expr("COUNT(*) AS NB"); + $logSql->columns($cols); + $totalResult = $logM->fetchRow($logSql); + + $output = new UserLog(); + $output->Total = $totalResult->NB; + $output->List = array(); + + if ($totalResult->NB > 0) { + // --- Get Row + $logSql->columns(array('LPAD(siren,9,0) AS siren', 'LPAD(nic,5,0) AS nic', 'raisonSociale')); + $logSql->order('dateHeure DESC')->limitPage($p, $limit); + $logResult = $logM->fetchAll($logSql); + foreach ($logResult as $l) { + $struct = new UserLogItem(); + $companyId = $l->siren; + if (intval($l->nic) > 0) { + $companyId.= $l->nic; + } + $struct->CompanyId = $companyId; + $struct->CompanyType = 'SIREN'; + $struct->CompanyName = $l->raisonSociale; + $struct->Date = $l->dateHeure; + $output->List[] = $struct; + } + } + + return $output; + } + + protected function getUserLogFile($id){} + + /** + * Company User History (10 last) + * @param int $id + * @param int $limit + * @return UserLogItem[] + */ + public function getUserHistory($id=null, $limit=10) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + $output = array(); + + // --- Generate SQL + try { + $logSubquery = "SELECT siren, MAX(dateHeure) AS MaxDateHeure FROM logs WHERE login='".$this->User->login."' AND page='identite' GROUP BY siren"; + $logSql = "SELECT LPAD(x.siren,9,0) AS siren, LPAD(x.nic,5,0) AS nic, x.raisonSociale, x.dateHeure FROM logs x ". + "JOIN (".$logSubquery.") y ON y.siren = x.siren AND y.MaxDateHeure = x.dateHeure ". + "ORDER BY x.dateHeure DESC LIMIT 0,".$limit; + $db = Zend_Db_Table_Abstract::getDefaultAdapter(); + $db->setFetchMode(Zend_Db::FETCH_OBJ); + $logResult = $db->fetchAll($logSql); + foreach ($logResult as $l) { + $struct = new UserLogItem(); + $companyId = $l->siren; + if (intval($l->nic) > 0) { + $companyId.= $l->nic; + } + $struct->CompanyId = $companyId; + $struct->CompanyType = 'SIREN'; + $struct->CompanyName = $l->raisonSociale; + $struct->Date = $l->dateHeure; + $output[] = $struct; + } + } catch (Zend_Db_Exception $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', 'Application Error.'); + } + } + + return $output; + } + + /** + * Log d'authentification + * @param string $month Date (AAAAMM) + * @param string $type (OK, KO) + * @param integer $p + * @param integer $limit + */ + public function getAuthLog($month, $type, $p=0, $limit=20) + { + $this->authenticate(); + + $selectedYear = date('Y'); + $selectedMonth = date('m'); + + // --- Generate SQL + $logM = new Application_Model_Sdv1UtilisateursAuthLog(); + $logSql = $logM->select() + ->where('login=?', $this->User->login) + ->where('authenticate=?', 'OK') + ->where('dateInsert BETWEEN "'.$selectedYear.'-'.$selectedMonth.'-00 00:00:00" AND "'. + $selectedYear.'-'.$selectedMonth.'-31 23:59:59"'); + + // --- Get total + $cols = new Zend_Db_Expr("COUNT(*) AS NB"); + $logSql->columns($cols); + $totalResult = $logM->fetchRow($logSql); + + $output = new AuthLog(); + $output->Total = $totalResult->NB; + $output->List = array(); + + if ($totalResult->NB > 0) { + // --- Get Row + $logSql->columns(array('IP', 'dateInsert AS Date')); + $logsSql->order('dateInsert DESC')->limitPage($p, $limit); + $logResult = $logM->fetchAll($logSql); + foreach ($logResult as $item) { + $struct = new AuthLogItem(); + $struct->IP = $item->IP; + $struct->Date = $item->Date; + $output->List[] = $struct; + } + } + + return $output; + } + + /** + * Paramétrage d'un utilisateur + * @param string $data + * @param int $id + * @throws SoapFault + * @return boolean + */ + public function setUser($data, $id = null) + { + $this->authenticate(); + + // --- Vérification des droits de création d'utilisateur + if ( $this->User->id != $id || !in_array($this->User->profil, array('SuperAdministrateur', 'Administrateur')) ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + $values = json_decode($data); + + $userM = new Application_Model_Sdv1Utilisateurs(); + + $userData = array( + 'idClient' => $values['idClient'], + 'login' => trim($values['login']), + 'email' => strtolower(trim($values['email'])), + 'password' => '', + 'actif' => 1, + 'deleted' => 0, + 'typeCompte' => 'PROD', + 'civilite' => $values['civilite'], + 'nom' => trim($values['nom']), + 'prenom' => trim($values['prenom']), + 'profil' => 'Utilisateur', + 'droits' => '', + 'referenceParDefaut' => '', + 'nbReponses' => 10, + 'lang' => 'fr', + 'formatMail' => 'txt2', + 'lienExtranetMail' => 0, + 'lienSurvFic' => 0, + 'idSurvFic' => '', + 'loginCptSurvFic' => '', + 'listeEven' => '', + ); + + // --- Création + if ( $id === null ) { + + $sql = $userM->select()->where('login=?', $values['login']); + $row = $userM->fetchRow($sql); + // --- Utilisateur existant + if ( null !== $row ) { + throw new SoapFault('ERR', "User exist"); + } + // --- Prepare data to insert + foreach ($values as $key => $value) { + if (array_key_exists($key, $userData)) { + $userData[$key] = trim($value); + } + } + $userData['dateInsert'] = date('YmdHis'); + + try { + $userM->insert($userData); + } catch (Zend_Db_Exception $e) { + return false; + } + + //Définition du service + $serviceM = new Application_Model_Sdv1UtilisateursService(); + try { + $serviceM->insert(array( + 'login' => $infos->login, + 'idClient'=> $infos->idClient, + 'Service'=> $infos->Service + )); + } catch (Zend_Db_Exception $e) { + return false; + } + + return true; + + } + // --- Modification + else { + + $sql = $userM->select()->where('id=?', $id); + $row = $userM->fetchRow($sql); + if ( null === $row ) { + throw new SoapFault('ERR', "User doesn't exist"); + } + // --- Prepare data to update + foreach ($row as $key => $value) { + if (array_key_exists($key, $userData)) { + $userData[$key] = trim($value); + } + } + foreach ($values as $key => $value) { + if (array_key_exists($key, $userData)) { + $userData[$key] = trim($value); + } + } + + try { + $userM->update($userData, 'id='.$id); + } catch (Zend_Db_Exception $e) { + return false; + } + + return true; + } + } + + /** + * Création de compte client partenaire + * @param string $data + * @return boolean + */ + public function setUserSSO ( $data ) + { + //Check IP application + /*$ip = $_SERVER['REMOTE_ADDR']; + if ( !in_array($ip, $this->listApplicationIp) ) { + $this->sendError('0901'); + }*/ + + //Decodage + $infos = json_decode($data); + + $userData = array( + 'idClient' => null, + 'login' => null, + 'email' => '', + 'password' => '', + 'actif' => 0, + 'deleted' => 0, + 'typeCompte' => 'PROD', + 'filtre_ip' => '', + 'civilite' => 'M', + 'nom' => '', + 'prenom' => '', + 'raisonSociale' => '', + 'siret' => '000000000', + 'adrNum' => null, + 'adrIndRep' => null, + 'adrTypeVoie' => null, + 'adrLibVoie' => '', + 'adrCp' => null, + 'adrVille' => null, + 'adrComp' => '', + 'tel' => '', + 'fax' => null, + 'mobile' => null, + 'pref' => '', + 'profil' => 'Utilisateur', + 'dateInscription' => '0000-00-00 00:00:00', + 'dateValidation' => '0000-00-00 00:00:00', + 'nombreConnexions' => '0', + 'dateDerniereConnexion' => '0000-00-00 00:00:00', + 'droits' => '', + 'referenceParDefaut' => '', + 'nbReponses' => '020', + 'lang' => null, + 'formatMail' => 'txt1', + 'lienExtranetMail' => 0, + 'lienSurvFic' => 0, + 'idSurvFic' => '', + 'loginCptSurvFic' => '', + 'listeEven' => '', + 'dateDebutCompte' => null, + 'dateFinCompte' => null, + 'maxFicheId' => null, + 'accesWS' => '0', + 'rechRefType' => 'UTI', + 'acceptationCGU' => null, + ); + + //Définition du service + $serviceM = new Application_Model_Sdv1UtilisateursService(); + try { + $serviceM->insert(array( + 'login' => $infos->login, + 'idClient'=> $infos->idClient, + 'Service'=> $infos->Service + )); + } catch ( Zend_Db_Exception $e ) { + return false; + } + + //Prepare data to insert + foreach ($infos as $key => $value) { + if (array_key_exists($key, $userData)) { + $userData[$key] = $value; + } + } + $userData['password'] = password_hash ( uniqid() , PASSWORD_BCRYPT ); + $userData['dateInsert'] = date('YmdHis'); + $userData['dateDebutCompte'] = date('YmdHis'); + + //Insertion dans la base de données + try { + $userM = new Application_Model_Sdv1Utilisateurs(); + $userM->insert($userData); + } catch ( Zend_Db_Exception $e ) { + return false; + } + + return true; + } + + protected function setUserService($service, $id) + { + //Déplacer un utilisateur de service - ne pas activer tout de suite + + } + + public function setUserEmail($id, $email, $op = null) + { + //Which operation + switch ( $op ) { + //Ajouter un email secondaire + case null: + case 'add': + break; + //Supprimer un email secondaire + case 'del': + break; + } + + + } + + /** + * Change password + * @param string $password + * @param int $id + * @throws SoapFault + * @return boolean + */ + public function setUserPassword($password, $id = null) + { + $this->authenticate(); + + if ( $id === null ) { + $id = $this->User->id; + } elseif ( $id !== null && $this->User->profil != 'Administrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + //@todo : Prise en compte de la version + + //Changer le mot de passe + $userM = new Application_Model_Sdv1Utilisateurs(); + $result = $userM->update(array('password'=>$password), 'id='.$id); + if ( $result == 1 ) { + return true; + } + return false; + } + + /** + * Catégorie et Accès + * @return AccesCategory[] + */ + public function getCategory() + { + $output = array(); + foreach ($this->listeCategory as $code => $desc) { + $c = new AccesCategory(); + $c->Code = $code; + $c->Label = $desc['label']; + $c->Acces = $desc['droits']; + $output[] = $c; + } + return $output; + } + + /** + * Liste des acces + * @return Acces[] + */ + public function getAccess() + { + $droits = $this->listeDroits; + $list = array(); + foreach ($droits as $k => $v) { + $acces = new Acces(); + $acces->Code = $k; + $acces->Label = $v; + $list[] = $acces; + } + + return $list; + } + + protected function getPref() + { + //Liste des préférences - Code, Label, Description, Values + } + + protected function setPref(){} + + /** + * Acceptation des CGUs + * @param string $app + * @throws SoapFault + * @return boolean + */ + public function setCGU($app = null) + { + $this->authenticate(); + + $id = $this->User->id; + + try { + $userM = new Application_Model_Sdv1Utilisateurs(); + $data = array('acceptationCGU' => date('YmdHis')); + $result = $userM->update($data, 'id='.$idUser); + } catch (Zend_Db_Exception $e) { + throw new SoapFault('Erreur', $e->getMessage()); + } catch (Zend_Exception $e) { + throw new SoapFault('Erreur', $e->getMessage()); + } + if ( 1 == $result ) { + return true; + } + return false; + } + + protected function setUserEnable($id) + { + //Un administrateur force l'activation d'un utilisateur ? + $userM = new Application_Model_Sdv1Utilisateurs(); + //actif = 1 + + /** + * L'utilisateur n'a pas reçu ou a perdu l'email avec son mot de passe, vous pouvez activez sont compte + * (attention son email ne sera pas validé, et certaines prestations nécessitant un email valide ne peuvent fonctionner) + */ + + } + + protected function setLinkValidation() + { + //Génération d'un hash pour la validation email + //Database : utilisateurs_validationh [id, ] + + //utilisateurs_email => dateValidation + surveillance + + //Envoi email avec lien de validation + } + + protected function setUserValidation($id) + { + //Un utilisateur valide son compte pour la première connexion et déclenche l'envoi de validation de l'email + //Doit-on demander un nouveau mot de passe ? Est ce que le mot de passe a été générer et envoyé par email . + $userM = new Application_Model_Sdv1Utilisateurs(); + //actif = 0 + //dateValidation pour email + + //Envoi email de validation + //lien + email + login + date + hash ('sha256', string ) => string {idClient}{login}{email}{AAAAMMJJ} + } + + protected function setUserEmailValidation($id) + { + //Validation d'un email + //Email secondaire ajouté marqueur dateValidation + } + + /** + * Un SuperAdministrateur devenir un utilisateur (pour les tests) + * @param int $id + * @throws SoapFault + */ + protected function setAdminAs($id) + { + $this->authenticate(); + + //Vérification des droits d'utilisation + if ( $this->User->profil != 'SuperAdministrateur' ) { + throw new SoapFault('ERR', 'Accès non authorisé'); + } + + // Pour les SuperAdministrateur, voir l'application comme un login (id) + + } +} \ No newline at end of file diff --git a/library/WsScore/Account/v0.1/Types.php b/library/WsScore/Account/v0.1/Types.php new file mode 100644 index 00000000..a238debe --- /dev/null +++ b/library/WsScore/Account/v0.1/Types.php @@ -0,0 +1,292 @@ + array( + 'actif' => true, + 'versions' => array( + '0.1' => array( 'actif' => true, 'defaut' => true ), + ), + 'idClient' => array(1), + ), 'catalog' => array( 'actif' => true, 'versions' => array(