From a2287cbabf2a896e3031d76af4d91b23f67f1855 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Wed, 19 Oct 2016 09:55:28 +0200 Subject: [PATCH] Account : All request with Doctrine --- library/WsScore/Account/v0.1/Service.php | 751 ++++++++++++----------- 1 file changed, 409 insertions(+), 342 deletions(-) diff --git a/library/WsScore/Account/v0.1/Service.php b/library/WsScore/Account/v0.1/Service.php index 2e911346..7224f175 100644 --- a/library/WsScore/Account/v0.1/Service.php +++ b/library/WsScore/Account/v0.1/Service.php @@ -34,36 +34,36 @@ class Account extends Scores_Ws_Server // --- Enregistrement authentification OK try { - $authLogM = new Application_Model_Sdv1UtilisateursAuthLog(); - $authLogM->insert(array( - 'login' => $login, - 'authenticate' => 'OK', - 'ip' => $ip, - 'dateInsert' => date('YmdHis'), + $this->conn->insert('sdv1.utilisateurs_auth_log', array( + 'login' => $login, + 'authenticate' => 'OK', + 'ip' => $ip, + 'dateInsert' => date('YmdHis'), )); - } catch (Zend_Db_Exception $e) {} + } catch (\Doctrine\DBAL\DBALException $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); + $sql = "SELECT * FROM sdv1.clients_services + WHERE IdClient = :clientId AND Code = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $this->User->idClient); + $stmt->bindValue('serviceCode', $this->User->serviceCode); + $stmt->execute(); + $serviceParams = $stmt->fetch(\PDO::FETCH_OBJ); // --- 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, + $this->conn->insert('sdv1.utilisateurs_browser_log', 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) { + } catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { throw new SoapFault('ERR', "Application error"); @@ -136,17 +136,19 @@ class Account extends Scores_Ws_Server * 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) { + $sql = "SELECT u.id, u.login, u.idClient, u.actif, c.actif AS clientActif, + s.Service AS serviceCode + FROM sdv1.utilisateurs u, sdv1.clients c + LEFT JOIN sdv1.utilisateurs_service ON u.login=s.login + WHERE u.login = :login AND c.id = :clientId"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $login); + $stmt->bindValue('clientId', $client); + $stmt->execute(); + $result = $stmt->fetch(\PDO::FETCH_OBJ); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { throw new SoapFault('ERR', "Application error"); @@ -209,10 +211,13 @@ class Account extends Scores_Ws_Server //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"); + $this->conn->update('sdv1.utilisateurs', + array('password' => $hash), array('id' => $result->id)); } + catch (\Doctrine\DBAL\DBALException $e) { + throw new SoapFault('SSO', "Activation de l'utilisateur impossible"); + } + return $hash; } @@ -232,26 +237,20 @@ class Account extends Scores_Ws_Server 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); + $sql = "SELECT 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 + FROM sdv1.clients WHERE actif='Oui'"; + $stmt = $this->conn->executeQuery($sql); + if ($stmt->rowCount() > 0) { + while ($item = $stmt->fetch(\PDO::FETCH_OBJ)) { + $client = new Client(); + $client->Nom = $item->Nom; + $client->Siren = $item->Siren; + $client->Nic = $item->Nic; + $client->Actif = $item->Actif; + $client->Test = $item->Test; - 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; + $clients[] = $client; } } @@ -273,26 +272,24 @@ class Account extends Scores_Ws_Server $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); + $sql = "SELECT 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 + FROM sdv1.clients WHERE id= :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('id', $id); + $stmt->execute(); - if ( $result === null ) { + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', 'Information client introuvable.'); } + $result = $stmt->fetch(\PDO::FETCH_OBJ); - $client = new Client(); - $client->Nom = $result->Nom; - $client->Siren = $result->Siren; - $client->Nic = $result->Nic; - $client->Actif = $result->Actif; - $client->Test = $result->Test; + $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; } @@ -312,44 +309,40 @@ class Account extends Scores_Ws_Server $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); + $sql = "SELECT 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 + FROM sdv1.clients WHERE id= :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('id', $id); + $stmt->execute(); - if ( $result === null ) { + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', 'Information client introuvable.'); } + $result = $stmt->fetch(\PDO::FETCH_OBJ); $client = new ClientServices(); - $client->Nom = $result->Nom; - $client->Siren = $result->Siren; - $client->Nic = $result->Nic; - $client->Actif = $result->Actif; - $client->Test = $result->Test; + $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); + $sql = "SELECT id, Code, Label, Editable, Active FROM sdv1.clients_services + WHERE Deleted = 0 AND Active = 1 AND IdClient = :clientId"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $id); + $stmt->execute(); $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; + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { + $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; } } @@ -400,11 +393,12 @@ class Account extends Scores_Ws_Server 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) { + $sql = "SELECT * FROM sdv1.clients_services WHERE Deleted = 0 AND IdClient = :clientId"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->execute(); + } catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -412,21 +406,21 @@ class Account extends Scores_Ws_Server } } $services = array(); - if ( count($serviceResult) > 0 ) { - foreach ($serviceResult as $item) { + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $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; + $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; } @@ -448,11 +442,15 @@ class Account extends Scores_Ws_Server $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) { + $sql = "SELECT * FROM sdv1.clients_services + WHERE Deleted = 0 AND IdClient = :clientId AND Code = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->bindValue('serviceCode', $code); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -460,21 +458,22 @@ class Account extends Scores_Ws_Server } } - if ( $result === null ) { + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', 'Service introuvable.'); } + $result = $stmt->fetch(\PDO::FETCH_OBJ); $output = new Service(); - $output->id = $result->id; - $output->IdClient = $client; - $output->Code = $result->Code; - $output->Label = $result->Label; + $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->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; @@ -482,41 +481,49 @@ class Account extends Scores_Ws_Server // 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) { + $sql = "SELECT * FROM sdv1.clients_services_droits + WHERE IdClient = :clientId AND Service = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->bindValue('serviceCode', $serviceCode); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $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) { + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $acces = new Acces(); - $acces->Code = strtoupper($item->Acces); - $acces->Label = $this->listeDroits[strtoupper($item->Acces)]; + $acces->Code = strtoupper($item->Acces); + $acces->Label = $this->listeDroits[strtoupper($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) { + $sql = "SELECT * FROM sdv1.clients_services_ip + WHERE IdClient = :clientId AND Service = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->bindValue('serviceCode', $serviceCode); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $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) { + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $output->IP[] = $item->IP; } } @@ -548,16 +555,26 @@ class Account extends Scores_Ws_Server $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); + try { + $sql = "SELECT * FROM sdv1.clients_services + WHERE Deleted = 0 AND IdClient = :clientId AND id = :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->bindValue('id', $id); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } - if ( $result === null ) { + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', "Service introuvable."); } + $result = $stmt->fetch(\PDO::FETCH_OBJ); if ( $result->Editable == 0) { throw new SoapFault('MSG', "Impossible d'éditer le service"); @@ -579,12 +596,13 @@ class Account extends Scores_Ws_Server $dataToUpdate = array('Label' => $value); } - if ( count($dataToUpdate) > 0 ) { + if (($dataToUpdate) > 0) { $dataToUpdate['DateUpdate'] = date('YmdHis'); try { - $serviceM->update($dataToUpdate, 'id='.$id); + $this->conn->update('sdv1.clients_services', $dataToUpdate, array('id' => $id)); return true; - } catch (Zend_Db_Exception $e) { + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -622,13 +640,14 @@ class Account extends Scores_Ws_Server $client = $this->User->idClient; try { - $serviceM = new Application_Model_Sdv1ClientsServices(); - $sql = $serviceM->select() - ->where('IdClient=?', $client) - ->where('Deleted=?', 0) - ->where('id=?', $id); - $result = $serviceM->fetchRow($sql); - } catch (Zend_Db_Exception $e) { + $sql = "SELECT * FROM sdv1.clients_services + WHERE Deleted = 0 AND IdClient = :clientId AND id = :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $client); + $stmt->bindValue('id', $id); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -636,9 +655,10 @@ class Account extends Scores_Ws_Server } } - if ( $result === null ) { + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', "Service introuvable."); } + $result = $stmt->fetch(\PDO::FETCH_OBJ); if ( $result->Editable == 0) { throw new SoapFault('MSG', "Impossible d'éditer le service"); @@ -647,25 +667,28 @@ class Account extends Scores_Ws_Server // --- Acces if ($type == 'acces') { if (array_key_exists($value, $this->listeDroits)) { - $accesM = new Application_Model_Sdv1ClientsServicesDroits(); if ($delete === true) { try { - $row = $accesM->delete(array( - 'IdClient="'.$result->IdClient.'"', - 'Service="'.$result->Code.'"', - 'Acces="'.$value.'"', + $row = $this->conn->delete('sdv1.clients_services_droits', array( + 'IdClient' => $result->IdClient, + 'Service' => $result->Code, + 'Acces' => $value, )); if ($row > 0) { return true; } - } catch (Zend_Db_Exception $e) { + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); } } - } else { + } + else { try { - $row = $accesM->insert(array( + $row = $this->conn->insert('sdv1.clients_services_droits', array( 'IdClient' => $result->IdClient, 'Service' => $result->Code, 'Acces' => $value, @@ -674,9 +697,12 @@ class Account extends Scores_Ws_Server if ($row > 0 ) { return true; } - } catch (Zend_Db_Exception $e) { + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); } } } @@ -685,14 +711,13 @@ class Account extends Scores_Ws_Server // --- IP if ($type == 'ip') { - $ipM = new Application_Model_Sdv1ClientsServicesIP(); - if ( $delete ) { - $row = $ipM->delete('id='.$id); + if ($delete) { + $row = $this->conn->delete('sdv1.clients_services_ip', array('id' => $id)); } else { //Control de la plage IP ou de l'IP $validate = new Zend_Validate_Ip(); if ( $validate->isValid($value) ) { - $row = $accessM->insert(array( + $this->conn->insert('sdv1.clients_services_ip', array( 'IdClient' => $result->IdClient, 'Service' => $result->Code, 'IP' => $value, @@ -736,26 +761,27 @@ class Account extends Scores_Ws_Server } 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); - + $qb = $this->conn->createQueryBuilder(); + $qb->select(array('u.id', 'u.idClient', 'u.login', 'u.email', 'u.civilite', 'u.nom', + 'u.prenom', 'u.actif', 'u.deleted', 's.Service', 'sd.Label')) + ->leftJoin('u', 'sdv1.utilisateurs_service', 's', 'u.login=s.login') + ->leftJoin('u', 'sdv1.clients_services', 'sd', 'sd.Code=s.Service') + ->from('sdv1.utilisateurs', 'u') + ->where('u.idClient = :clientId'); if ( $actif !== null && in_array($actif, array(0,1)) ) { - $sql->where('u.actif=?', $actif); + $qb->andWhere('u.actif = :actif')->setParameter('actif', $actif); } - $sql->where('u.deleted=?', 0); + $qb->andWhere('u.deleted = 0'); if ($service == 'DEFAULT') { - $sql->where('(s.Service IS NULL) OR (s.Service="DEFAULT" AND sd.idClient='.$client.')'); + $qb->andWhere("(s.Service IS NULL) OR (s.Service='DEFAULT' AND sd.idClient=:clientId)"); } else { - $sql->where('sd.Code=?', $service); + $qb->andWhere('sd.Code = :serviceCode')->setParameter('serviceCode', $service); } - $result = $userM->fetchAll($sql); - } catch (Zend_Db_Exception $e) { + $qb->setParameter('clientId', $client); + $stmt = $qb->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -764,8 +790,8 @@ class Account extends Scores_Ws_Server } $users = array(); - if (count($result) > 0) { - foreach ($result as $item) { + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $user = new UserList(); $user->id = $item->id; $user->IdClient = $item->idClient; @@ -812,15 +838,16 @@ class Account extends Scores_Ws_Server // 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) { + $qb = $this->conn->createQueryBuilder(); + $qb->select('*') + ->leftJoin('u', 'sdv1.utilisateurs_service', 's', 'u.login=s.login') + ->leftJoin('u', 'sdv1.clients_services', 'sd', 'sd.Code=s.Service') + ->from('sdv1.utilisateurs', 'u') + ->where('u.id = :id'); + $qb->setParameter('id', $id); + $stmt = $qb->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -828,9 +855,11 @@ class Account extends Scores_Ws_Server } } - if ( $user === null ) { + + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', 'Utilisateur inexistant !'); } + $user = $stmt->fetch(\PDO::FETCH_OBJ); // Service $serviceCode = 'DEFAULT'; @@ -869,19 +898,24 @@ class Account extends Scores_Ws_Server // Service - Droits if (count($output->Acces) == 0) { - 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) { + try { + $sql = "SELECT * FROM sdv1.clients_services_droits + WHERE IdClient = :clientId AND Service = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $idClient); + $stmt->bindValue('serviceCode', $serviceCode); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $acces = new Acces(); $acces->Code = strtoupper($item->Acces); $acces->Label = $this->listeDroits[strtoupper($item->Acces)]; @@ -892,18 +926,22 @@ class Account extends Scores_Ws_Server // 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) { + $sql = "SELECT * FROM sdv1.clients_services_ip + WHERE IdClient = :clientId AND Service = :serviceCode"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $idClient); + $stmt->bindValue('serviceCode', $serviceCode); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $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) { + if ($stmt->rowCount() > 0) { + while ($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $output->IP[] = $item; } } @@ -929,13 +967,15 @@ class Account extends Scores_Ws_Server $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) { + $sql = "SELECT * FROM sdv1.utilisateurs_emails + WHERE IdClient = :clientId AND id = :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('clientId', $idClient); + $stmt->bindValue('id', $id); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { throw new SoapFault('ERR', "Application error"); @@ -943,8 +983,8 @@ class Account extends Scores_Ws_Server } $emails = array(); - if ( count($result)>0 ) { - foreach ( $result as $item ) { + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { $email = new Email(); $email->id = $item->id; $email->value = $item->email; @@ -977,20 +1017,19 @@ class Account extends Scores_Ws_Server $item = 'identite'; - // --- Generate SQL - $logM = new Application_Model_Sdv1Logs(); - $logSql = $logM->select(true) - ->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 try { - $cols = new Zend_Db_Expr("COUNT(*) AS NB"); - $logSql->columns($cols); - $totalResult = $logM->fetchRow($logSql); - } catch (Zend_Db_Exception $e) { + $sql = "SELECT COUNT(*) AS NB FROM sdv1.logs + WHERE login = :login AND page = :page AND dateHeure BETWEEN :begin AND :end"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $this->User->login); + $stmt->bindValue('page', $item); + $stmt->bindValue('begin', $selectedYear.'-'.$selectedMonth.'-00 00:00:00'); + $stmt->bindValue('end', $selectedYear.'-'.$selectedMonth.'-31 23:59:59'); + $stmt->execute(); + $totalResult = $stmt->fetch(\PDO::FETCH_OBJ); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { @@ -1005,27 +1044,38 @@ class Account extends Scores_Ws_Server if ($totalResult->NB > 0) { // --- Get Row try { - $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); - } catch (Zend_Db_Exception $e) { + $sql = "SELECT LPAD(siren,9,0) AS siren, LPAD(nic,5,0) AS nic, raisonSociale + FROM sdv1.logs + WHERE login = :login AND page = :page AND dateHeure BETWEEN :begin AND :end + ORDER BY dateHeure DESC LIMIT $p, $limit"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $this->User->login); + $stmt->bindValue('page', $item); + $stmt->bindValue('begin', $selectedYear.'-'.$selectedMonth.'-00 00:00:00'); + $stmt->bindValue('end', $selectedYear.'-'.$selectedMonth.'-31 23:59:59'); + $stmt->execute(); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { throw new SoapFault('ERR', "Application error"); } } - foreach ($logResult as $l) { - $struct = new UserLogItem(); - $companyId = $l->siren; - if (intval($l->nic) > 0) { - $companyId.= $l->nic; + + if ($stmt->rowCount() > 0) { + while($l = $stmt->fetch(\PDO::FETCH_OBJ)) { + $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; } - $struct->CompanyId = $companyId; - $struct->CompanyType = 'SIREN'; - $struct->CompanyName = $l->raisonSociale; - $struct->Date = $l->dateHeure; - $output->List[] = $struct; } } @@ -1040,7 +1090,7 @@ class Account extends Scores_Ws_Server * @param int $limit * @return UserLogItem[] */ - public function getUserHistory($id=null, $limit=10) + public function getUserHistory($id = null, $limit = 10) { $this->authenticate(); @@ -1055,10 +1105,8 @@ class Account extends Scores_Ws_Server $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) { + $stmt = $this->conn->executeQuery($logSql); + while ($l = $stmt->fetch(\PDO::FETCH_OBJ)) { $struct = new UserLogItem(); $companyId = $l->siren; if (intval($l->nic) > 0) { @@ -1070,11 +1118,12 @@ class Account extends Scores_Ws_Server $struct->Date = $l->dateHeure; $output[] = $struct; } - } catch (Zend_Db_Exception $e) { + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { throw new SoapFault('ERR', $e->getMessage()); } else { - throw new SoapFault('ERR', 'Application Error.'); + throw new SoapFault('ERR', "Application error"); } } @@ -1102,23 +1151,23 @@ class Account extends Scores_Ws_Server $dateStart = $month.'-01 00:00:00'; $dateEnd = $month.'-31 23:59:59'; - // --- Generate SQL - $logM = new Application_Model_Sdv1UtilisateursAuthLog(); // --- Get total - $logSql = $logM->select()->from($logM, array()) - ->where('login=?', $this->User->login) - ->where('authenticate=?', 'OK') - ->where('dateInsert BETWEEN "'.$dateStart.'" AND "'.$dateEnd.'"'); - $cols = new Zend_Db_Expr("COUNT(*) AS NB"); - $logSql->columns($cols); try { - $totalResult = $logM->fetchRow($logSql); - } catch (Zend_Db_Exception $e) { - if ($this->User->idClient == 1) { - throw new SoapFault('ERR', $e->getMessage()); - } else { - throw new SoapFault('ERR', 'Application Error.'); - } + $sql = "SELECT COUNT(*) AS NB FROM sdv1.utilisateurs_auth_log + WHERE login = :login AND authenticate = 'OK' AND dateInsert BETWEEN :begin AND :end"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $this->User->login); + $stmt->bindValue('begin', $dateStart); + $stmt->bindValue('end', $dateEnd); + $stmt->execute(); + $totalResult = $stmt->fetch(\PDO::FETCH_OBJ); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } $output = new AuthLog(); @@ -1127,26 +1176,31 @@ class Account extends Scores_Ws_Server if ($totalResult->NB > 0) { // --- Get Row - $logSql = $logM->select()->from($logM, array()) - ->where('login=?', $this->User->login) - ->where('authenticate=?', 'OK') - ->where('dateInsert BETWEEN "'.$dateStart.'" AND "'.$dateEnd.'"'); - $logSql->columns(array('IP', 'dateInsert AS Date')); - $logSql->order('dateInsert DESC')->limit($limit, $limit * $p); try { - $logResult = $logM->fetchAll($logSql); - } catch (Zend_Db_Exception $e) { - if ($this->User->idClient == 1) { - throw new SoapFault('ERR', $e->getMessage()); - } else { - throw new SoapFault('ERR', 'Application Error.'); - } + $sql = "SELECT IP, dateInsert AS Date FROM sdv1.utilisateurs_auth_logs + WHERE login = :login AND authenticate = 'OK' AND dateInsert BETWEEN :begin AND :end + ORDER BY dateInsert DESC"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $this->User->login); + $stmt->bindValue('begin', $dateStart); + $stmt->bindValue('end', $dateEnd); + $stmt->execute(); } - foreach ($logResult as $item) { - $struct = new AuthLogItem(); - $struct->IP = $item->IP; - $struct->Date = $item->Date; - $output->List[] = $struct; + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } + } + + if ($stmt->rowCount() > 0) { + while($item = $stmt->fetch(\PDO::FETCH_OBJ)) { + $struct = new AuthLogItem(); + $struct->IP = $item->IP; + $struct->Date = $item->Date; + $output->List[] = $struct; + } } } @@ -1172,8 +1226,6 @@ class Account extends Scores_Ws_Server $values = json_decode($data); - $userM = new Application_Model_Sdv1Utilisateurs(); - // --- Création if ( $id === null ) { @@ -1201,12 +1253,15 @@ class Account extends Scores_Ws_Server 'listeEven' => '', ); - $sql = $userM->select()->where('login=?', $values->login); - $row = $userM->fetchRow($sql); // --- Utilisateur existant - if ( null !== $row ) { + $sql = "SELECT id FROM sdv1.utilisateurs WHERE login = :login"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('login', $values->login); + $stmt->execute(); + if ($stmt->rowCount() > 0) { throw new SoapFault('ERR', "User exist"); } + // --- Prepare data to insert foreach ($values as $key => $value) { if (array_key_exists($key, $userData)) { @@ -1216,29 +1271,30 @@ class Account extends Scores_Ws_Server $userData['dateInsert'] = date('YmdHis'); try { - $userM->insert($userData); - } catch (Zend_Db_Exception $e) { + $this->conn->insert('sdv1.utilisateurs', $userData); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { - throw new SoapFault('ERR', $e->getMessage()); - } else { - throw new SoapFault('ERR', 'Application Error.'); - } + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } //Définition du service - $serviceM = new Application_Model_Sdv1UtilisateursService(); try { - $serviceM->insert(array( + $this->conn->insert('sdv1.utilisateurs_service', array( 'login' => $infos->login, 'idClient'=> $infos->idClient, 'Service'=> $infos->Service )); - } catch (Zend_Db_Exception $e) { + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { - throw new SoapFault('ERR', $e->getMessage()); - } else { - throw new SoapFault('ERR', 'Application Error.'); - } + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } return true; @@ -1249,9 +1305,12 @@ class Account extends Scores_Ws_Server $userData = array(); - $sql = $userM->select()->where('id=?', $id); - $row = $userM->fetchRow($sql); - if ( null === $row ) { + $sql = "SELECT id FROM sdv1.utilisateurs WHERE id = :id"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('id', $id); + $stmt->execute(); + + if ($stmt->rowCount() == 0) { throw new SoapFault('ERR', "User doesn't exist"); } // --- Prepare data to update @@ -1260,13 +1319,14 @@ class Account extends Scores_Ws_Server } try { - $nb = $userM->update($userData, 'id='.$id); - } catch (Zend_Db_Exception $e) { + $nb = $this->conn->update('sdv1.utilisateurs', $userData, array('id' => $id)); + } + catch (\Doctrine\DBAL\DBALException $e) { if ($this->User->idClient == 1) { - throw new SoapFault('ERR', $e->getMessage()); - } else { - throw new SoapFault('ERR', 'Application Error.'); - } + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } if ($nb > 0) { @@ -1342,15 +1402,19 @@ class Account extends Scores_Ws_Server ); //Définition du service - $serviceM = new Application_Model_Sdv1UtilisateursService(); try { - $serviceM->insert(array( + $this->conn->insert('sdv1.utilisateurs_service', array( 'login' => $infos->login, 'idClient'=> $infos->idClient, 'Service'=> $infos->Service )); - } catch ( Zend_Db_Exception $e ) { - return false; + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } //Prepare data to insert @@ -1365,9 +1429,10 @@ class Account extends Scores_Ws_Server //Insertion dans la base de données try { - $userM = new Application_Model_Sdv1Utilisateurs(); - $userM->insert($userData); - } catch ( Zend_Db_Exception $e ) { + $this->conn->insert('sdv1.utilisateurs', $userData); + + } + catch (\Doctrine\DBAL\DBALException $e) { return false; } @@ -1409,15 +1474,16 @@ class Account extends Scores_Ws_Server if ( $id === null ) { $id = $this->User->id; - } elseif ( $id !== null && $this->User->profil != 'Administrateur' ) { + } + 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); + $result = $this->conn->update('sdv1.utilisateurs', + array('password'=>$password), array('id' => $id)); if ( $result == 1 ) { return true; } @@ -1479,13 +1545,14 @@ class Account extends Scores_Ws_Server $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()); + $result = $this->conn->update('sdv1.utilisateurs', $data, array('id' => $id)); + } + catch (\Doctrine\DBAL\DBALException $e) { + if ($this->User->idClient == 1) { + throw new SoapFault('ERR', $e->getMessage()); + } else { + throw new SoapFault('ERR', "Application error"); + } } if ( 1 == $result ) { return true; @@ -1496,7 +1563,7 @@ class Account extends Scores_Ws_Server protected function setUserEnable($id) { //Un administrateur force l'activation d'un utilisateur ? - $userM = new Application_Model_Sdv1Utilisateurs(); + //actif = 1 /** @@ -1520,7 +1587,7 @@ class Account extends Scores_Ws_Server { //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