Michael RICOIS 5a72bd68cb 2.4 rev 1821
2013-11-05 11:18:30 +00:00

1974 lines
62 KiB
PHP

<?php
require_once 'WsScore/WsScore.php';
require_once realpath(dirname(__FILE__)).'/Types.php';
class Gestion extends WsScore
{
/**
* Récupère les informations du profil connecté
* @param string $login
* @param string $ipUtilisateur
* @return InfosLoginReturn
*/
public function getInfosLogin($login, $ipUtilisateur = '')
{
$this->authenticate();
//Initialisation
if (empty($ipUtilisateur)) $ipUtilisateur = $_SERVER['REMOTE_ADDR'];
$error = new ErrorType();
$result = new InfosLogin();
//Filtre IP
$filtre = trim($this->tabInfoUser['filtre_ip']);
if ( !empty($filtre) && !checkPlagesIp($filtre, $ipUtilisateur) ){
debugLog('I',"getInfosLogin Adresse IP $ipUtilisateur non declaree pour le compte $login",__LINE__,__FILE__, __FUNCTION__, __CLASS__);
$this->sendError('0904');
}
debugLog('I',"getInfosLogin pour $login (IP=$ipUtilisateur)",__LINE__,__FILE__, __FUNCTION__, __CLASS__);
//L'utilisateur qui demande les informations est différent
if ( $this->tabInfoUser['login']<>$login
&& in_array($this->tabInfoUser['profil'], array('Administrateur', 'SuperAdministrateur') )
) {
/** Un administrateur veut il des infos sur un login ? **/
$iDbCrm = new WDB('sdv1');
$rep = $iDbCrm->select(
'utilisateurs u, clients c',
'u.login, u.id, u.email, u.password, u.idClient, u.typeCompte, u.actif, u.filtre_ip, u.civilite, u.nom, u.prenom, u.tel, u.fax, u.mobile, u.profil, u.raisonSociale, u.siret, u.adrNum, u.adrIndRep, u.adrTypeVoie, u.adrLibVoie, u.adrCp, u.adrVille, u.adrComp, u.pref, u.profil, u.dateInscription, u.dateValidation, u.nombreConnexions, u.dateDerniereConnexion, u.droits, u.referenceParDefaut, u.nbReponses, u.lang, u.formatMail, u.dateDebutCompte, u.dateFinCompte, u.maxFicheId, c.droits AS droitsClients, c.test AS clientTest, c.timeout, c.typeScore AS typeScore',
"u.login='$login' AND u.idClient=c.id AND u.deleted=0",
false, MYSQL_ASSOC
);
$tabRep = $rep[0];
if (count($rep)>0)
{
$timeOutSec = $tabRep['timeout'];
if ($timeOutSec==0) $timeOutSec = 1800;
$result->login = $login;
$result->id = $tabRep['id'];
$result->email = $tabRep['email'];
$result->typeCompte = $tabRep['typeCompte'];
$result->idClient = $tabRep['idClient'];
$result->filtre_ip = $tabRep['filtre_ip'];
$result->civilite = $tabRep['civilite'];
$result->nom = $tabRep['nom'];
$result->prenom = $tabRep['prenom'];
$result->raisonSociale = $tabRep['raisonSociale'];
$result->siret = $tabRep['siret'];
$result->adrNum = $tabRep['adrNum'];
$result->adrIndRep = $tabRep['adrIndRep'];
$result->adrTypeVoie = $tabRep['adrTypeVoie'];
$result->adrLibVoie = $tabRep['adrLibVoie'];
$result->adrCp = $tabRep['adrCp'];
$result->adrVille = $tabRep['adrVille'];
$result->adrComp = $tabRep['adrComp'];
$result->tel = $tabRep['tel'];
$result->fax = $tabRep['fax'];
$result->mobile = $tabRep['mobile'];
$result->pref = $tabRep['pref'];
$result->profil = $tabRep['profil'];
$result->dateInscription = $tabRep['dateInscription'];
$result->dateValidation = $tabRep['dateValidation'];
$result->nombreConnexions = $tabRep['nombreConnexions'];
$result->dateDerniereConnexion = $tabRep['dateDerniereConnexion'];
$result->droits = $tabRep['droits'];
$result->droitsClients = $tabRep['droitsClients'];
$result->timeout = $timeOutSec;
$result->clientTest = $tabRep['clientTest'];
$result->typeScore = $tabRep['typeScore'];
$result->nbReponses = $tabRep['nbReponses'];
$result->lang = $tabRep['lang'];
$result->formatMail = $tabRep['formatMail'];
$result->reference = $tabRep['referenceParDefaut'];
$result->dateDebutCompte = $tabRep['dateDebutCompte'];
$result->dateFinCompte = $tabRep['dateFinCompte'];
$result->maxFicheId = $tabRep['maxFicheId'];
} else {
/** On log l'erreur d'authentification **/
$tabInsert=array(
'login' => $login,
'page' => 'infosLogin',
'params' => 'Erreur Login/Password',
'ipClient' => $ipUtilisateur);
$iDbCrm->insert('logs', $tabInsert);
}
}
//Authentification de l'utilisateur
else
{
$ref='OK';
//Identification utilisateur
$connected = true;
$result->connected = $connected;
$result->login = $this->tabInfoUser['login'];
$result->id = $this->tabInfoUser['id'];
$result->idClient = $this->tabInfoUser['idClient'];
$result->email = $this->tabInfoUser['email'];
$result->typeCompte = $this->tabInfoUser['typeCompte'];
$result->filtre_ip = $this->tabInfoUser['filtre_ip'];
$result->ipPasserelle = $this->tabInfoUser['ipConnexion'];
$result->ipConnexion = $ipUtilisateur;
$result->civilite = $this->tabInfoUser['civilite'];
$result->nom = $this->tabInfoUser['nom'];
$result->prenom = $this->tabInfoUser['prenom'];
$result->raisonSociale = $this->tabInfoUser['raisonSociale'];
$result->siret = $this->tabInfoUser['siret'];
$result->adrNum = $this->tabInfoUser['adrNum'];
$result->adrIndRep = $this->tabInfoUser['adrIndRep'];
$result->adrTypeVoie = $this->tabInfoUser['adrTypeVoie'];
$result->adrLibVoie = $this->tabInfoUser['adrLibVoie'];
$result->adrCp = $this->tabInfoUser['adrCp'];
$result->adrVille = $this->tabInfoUser['adrVille'];
$result->adrComp = $this->tabInfoUser['adrComp'];
$result->tel = $this->tabInfoUser['tel'];
$result->fax = $this->tabInfoUser['fax'];
$result->mobile = $this->tabInfoUser['mobile'];
$result->pref = $this->tabInfoUser['pref'];
$result->profil = $this->tabInfoUser['profil'];
$result->dateInscription = $this->tabInfoUser['dateInscription'];
$result->dateValidation = $this->tabInfoUser['dateValidation'];
$result->nombreConnexions = $this->tabInfoUser['nombreConnexions'];
$result->dateDerniereConnexion = $this->tabInfoUser['dateDerniereConnexion'];
$result->droits = $this->tabInfoUser['droits'];
$result->droitsClients = $this->tabInfoUser['droitsClients'];
$result->timeout = $this->tabInfoUser['timeout'];
$result->clientTest = $this->tabInfoUser['clientTest'];
$result->typeScore = $this->tabInfoUser['typeScore'];
$result->nbReponses = $this->tabInfoUser['nbReponses'];
$result->lang = $this->tabInfoUser['lang'];
$result->formatMail = $this->tabInfoUser['formatMail'];
$result->reference = $this->tabInfoUser['referenceParDefaut'];
$result->dateDebutCompte = $this->tabInfoUser['dateDebutCompte'];
$result->dateFinCompte = $this->tabInfoUser['dateFinCompte'];
$result->maxFicheId = $this->tabInfoUser['maxFicheId'];
$result->typeScore = $this->tabInfoUser['typeScore'];
$result->acceptationCGU = $this->tabInfoUser['acceptationCGU'];
// On log l'accès à l'authentification
if ( $this->tabInfoUser['clientTest']=='Oui' || $this->tabInfoUser['typeCompte']=='TEST' ) {
$test = 1;
} else {
$test = 0;
}
$tabInsert = array(
'login' => $this->tabInfoUser['login'],
'idClient' => $this->tabInfoUser['idClient'],
'page' => 'infosLogin',
'params' => $ref,
'test' => $test,
'ipClient' => $ipUtilisateur
);
$iDbCrm = new WDB('sdv1');
$iDbCrm->insert('logs', $tabInsert);
//Date de dernière connexion
$iDbCrm->update('utilisateurs',
array('dateDerniereConnexion'=> date('Y-m-d H:i:s')),
"idClient=".$this->tabInfoUser['idClient']." AND login='".$this->tabInfoUser['login']."'"
);
}
//Retour
$output = new InfosLoginReturn();
$output->error = $error;
$output->result = $result;
return $output;
}
/**
* Recherche de login
* @param string $idClient
* @param string $query
* @return string
*/
public function searchLogin($idClient, $query)
{
$this->authenticate();
$userM = new Application_Model_Sdv1Utilisateurs();
$sql = $userM->select()
->from($userM, array('id','login','actif','deleted'))
->where('idClient=?', $idClient)
->where('login LIKE "'.$query.'%"')
->limit(20);
$result = $userM->fetchAll($sql);
if ($result->count()>0) {
return json_encode($result->toArray());
}
return false;
}
/**
* Mise à jour des informations du profil connecté
* @param string $login
* @param string $action (delete|enable|disable|changepwd|new|update)
* @param string $infos
* @return boolean
*/
public function setInfosLogin($login, $action, $infos = null)
{
$this->authenticate();
//Initialisation
$result = false;
$infos = json_decode($infos, true);
//Get user infos
$userM = new Application_Model_Sdv1Utilisateurs();
$row = $userM->select()->where('login=?',$login);
//isExist
if ( null === $row ) {
throw new SoapFault('ERR', "User doesn't exist");
}
//isEditable
if ( $row->accesWS == 1
&& !in_array($this->tabInfoUser['profil'], array('Administrateur', 'SuperAdministrateur')) ) {
throw new SoapFault('MSG', "User not editable");
}
//Make action
switch($action) {
case 'delete':
if ($this->tabInfoUser['profil']=='Administrateur'
|| $this->tabInfoUser['profil']=='SuperAdministrateur') {
$tabUpdate = array( 'deleted' => 1, 'dateUpdate' => date('Y-m-d H:i:s') );
$iDbCrm = new WDB('sdv1');
if ($iDbCrm->update('utilisateurs', $tabUpdate, "login='$login'")){
$result = true;
}
}
break;
case 'enable':
case 'disable':
if ($this->tabInfoUser['profil']=='Administrateur'
|| $this->tabInfoUser['profil']=='SuperAdministrateur') {
$actif = 1;
if ($action=='disable') {
$actif = 0;
}
$tabUpdate = array( 'actif' => $actif, 'dateUpdate' => date('YmdHis') );
$iDbCrm = new WDB('sdv1');
if ($iDbCrm->update('utilisateurs', $tabUpdate, "login='$login'")){
$result = true;
}
}
break;
case 'changepwd':
$tabUpdate = array(
'password' => stripslashes($infos['password']),
'dateUpdate' => date('Y-m-d H:i:s')
);
$iDbCrm = new WDB('sdv1');
if ($iDbCrm->update('utilisateurs', $tabUpdate, "login='$login'", true)){
$result = true;
}
break;
case 'new':
if ( $this->tabInfoUser['profil']=='Administrateur'
|| $this->tabInfoUser['profil']=='SuperAdministrateur') {
$iDbCrm = new WDB('sdv1');
$existLogin = $iDbCrm->select('utilisateurs', 'deleted', 'login="'.$login.'"', false, MYSQL_ASSOC);
if (count($existLogin)>0) {
$message = "Utilisateur existant dans le référentiel";
if ($existLogin[0]['deleted']==1) {
$message.=" (supprimé)";
}
throw new SoapFault('ERR', $message);
}
$tabUpdate = array();
$idClient = $this->tabInfoUser['idClient'];
if ( $this->tabInfoUser['profil']=='SuperAdministrateur' && $infos['idClient']!=''){
$idClient = $infos['idClient'];
}
$tabUpdate['idClient'] = $idClient;
$tabUpdate['login'] = $login;
$tabUpdate['email'] = strtolower(stripslashes($infos['email']));
$tabUpdate['password'] = stripslashes($infos['password']);
$tabUpdate['actif'] = 0;
$tabUpdate['deleted'] = 0;
$typeCompte = 'PROD';
if ( $this->tabInfoUser['profil']=='SuperAdministrateur' && $infos['typeCompte']=='TEST'){
$typeCompte = 'TEST';
}
$tabUpdate['typeCompte'] = $typeCompte;
//@todo : See issue #0001209
/*
if ($this->tabInfoUser['profil']=='SuperAdministrateur' && !empty($infos['filtre_ip'])) {
$tabUpdate['filtre_ip'] = $infos['filtre_ip'];
} else {
$rep = $iDbCrm->select('clients', 'filtre_ip', "id='$idClient'",false, MYSQL_ASSOC);
$tabUpdate['filtre_ip'] = $rep[0]['filtre_ip'];
}
*/
//$tabUpdate['civilite'] = 'M';
$tabUpdate['nom'] = stripslashes($infos['nom']);
$tabUpdate['prenom'] = stripslashes($infos['prenom']);
//$tabUpdate['raisonSociale'] = '';
//$tabUpdate['siret'] = 00000000;
//adrNum
//adrIndRep
//adrTypeVoie
//adrLibVoie
//adrCp
//adrVille
//adrComp
$tabUpdate['tel'] = stripslashes($infos['tel_fix']);
$tabUpdate['fax'] = stripslashes($infos['tel_fax']);
$tabUpdate['mobile'] = stripslashes($infos['tel_mob']);
$tabUpdate['pref'] = implode(' ',$infos['pref']);
$tabUpdate['profil'] = (isset($infos['profil'])
|| !in_array($infos->profil, array('SuperAdministrateur','Administrateur', 'Utilisateur')) ) ?
$infos['profil'] : 'Utilisateur';
//dateInscription
//dateValidation
//nombreConnexions 0
//dateDerniereConnexion
$tabUpdate['droits'] = implode(' ', $infos['droits']);
$tabUpdate['referenceParDefaut'] = stripslashes($infos['reference']);
$tabUpdate['nbReponses'] = $infos['nbReponses'];
$tabUpdate['lang'] = $infos['lang'];
if ($this->tabInfoUser['profil']=='SuperAdministrateur') {
$tabUpdate['formatMail'] = $infos['formatMail'];
$tabUpdate['lienExtranetMail'] = $infos['lienExtranetMail'];
$tabUpdate['listeEven'] = $infos['listeEven'];
$tabUpdate['dateDebutCompte'] = $infos['dateDebutCompte'];
$tabUpdate['dateFinCompte'] = $infos['dateFinCompte'];
$tabUpdate['accesWS'] = $infos['accesWS'];
$tabUpdate['rechRefType'] = $infos['rechRefType'];
}
//lienSurvFic
//idSurvFic
//loginCptSurvFic
$tabUpdate['dateInsert'] = date('YmdHis');
//dateUpdate => MySql automatic update
$iDbCrm = new WDB('sdv1');
if ($iDbCrm->insert('utilisateurs', $tabUpdate)){
$result = true;
}
}
break;
case 'update':
//Don't update password if nothing is send
if (!empty($infos['password'])){
$tabUpdate['password'] = stripslashes($infos['password']);
}
//Only this data could be updated by the user himself
$tabUpdate['email'] = stripslashes($infos['email']);
$tabUpdate['tel'] = stripslashes($infos['tel_fix']);
$tabUpdate['fax'] = stripslashes($infos['tel_fax']);
$tabUpdate['mobile'] = stripslashes($infos['tel_mb']);
$tabUpdate['nbReponses'] = $infos['nbReponses'];
if (!empty($infos['lang'])) {
$tabUpdate['lang'] = $infos['lang'];
}
$tabUpdate['pref'] = implode(' ',$infos['pref']);
//User with Administrateur or SuperAdministrateur can update these data
if ($this->tabInfoUser['profil']=='Administrateur'
|| $this->tabInfoUser['profil']=='SuperAdministrateur')
{
$tabUpdate['droits'] = implode(' ', $infos['droits']);
$tabUpdate['nom'] = stripslashes($infos['nom']);
$tabUpdate['prenom'] = stripslashes($infos['prenom']);
$tabUpdate['referenceParDefaut'] = stripslashes($infos['reference']);
$tabUpdate['profil'] = isset($infos['profil']) ? $infos['profil'] : 'Utilisateur';
}
//Only update as SuperAdministrateur
if ($this->tabInfoUser['profil']=='SuperAdministrateur')
{
$typeCompte = 'PROD';
if ( $this->tabInfoUser['profil']=='SuperAdministrateur' && $infos['typeCompte']=='TEST'){
$typeCompte = 'TEST';
}
$tabUpdate['typeCompte'] = $typeCompte;
$formatMail = 'txt1';
if ( !empty($infos['formatMail']) ) {
$formatMail = $infos['formatMail'];
}
$tabUpdate['formatMail'] = $formatMail;
if (array_key_exists('lienExtranetMail', $infos) && !empty($infos['lienExtranetMail'])) {
$tabUpdate['lienExtranetMail'] = $infos['lienExtranetMail'];
}
if (array_key_exists('listeEven', $infos) && !empty($infos['listeEven'])) {
$tabUpdate['listeEven'] = $infos['listeEven'];
}
if (array_key_exists('dateDebutCompte', $infos) && !empty($infos['dateDebutCompte'])) {
$tabUpdate['dateDebutCompte'] = $infos['dateDebutCompte'];
}
if (array_key_exists('dateFinCompte', $infos) && !empty($infos['dateFinCompte'])) {
$tabUpdate['dateFinCompte'] = $infos['dateFinCompte'];
}
if (array_key_exists('accesWS', $infos) && !empty($infos['accesWS'])) {
$tabUpdate['accesWS'] = $infos['accesWS'];
}
$rechRefType = 'UTI';
if (array_key_exists('rechRefType', $infos) && !empty($infos['rechRefType'])) {
$rechRefType = $infos['rechRefType'];
}
$tabUpdate['rechRefType'] = $rechRefType;
}
$tabUpdate['dateUpdate'] = date('YmdHis');
$iDbCrm = new WDB('sdv1');
if ($iDbCrm->update('utilisateurs', $tabUpdate, "login='$login'", true)){
$result = true;
}
break;
default: $result = false; break;
}
return $result;
}
/**
* Obtention automatique d'un nouveau login pour un client
* @param int idClient
* @return NextLoginReturn
*/
public function getNextLogin( $idClient )
{
$this->authenticate();
//Initialisation
$error = new ErrorType();
if ($this->tabInfoUser['profil']=='Administrateur'
|| $this->tabInfoUser['profil']=='SuperAdministrateur')
{
/** Un administrateur veut créer un nouveau login **/
$iDbCrm = new WDB('sdv1');
$rep = $iDbCrm->select('clients c', 'c.racineLogin, c.droits', "c.id='$idClient'");
$racine = $rep[0]['racineLogin'];
$droitsClients = $rep[0]['droits'];
$rep = $iDbCrm->select('utilisateurs', 'login', "idClient='$idClient'");
if (count($rep)==0) {
$racine.='1';
} else {
$last_i = count($rep);
$cpt = 1;
while (true) {
$racine.= $last_i+$cpt;
$rep = $iDbCrm->select('utilisateurs', 'login', "idClient='$idClient' AND login='$racine'");
if (count($rep)==0) break;
$cpt++;
}
}
$result = new NextLoginResult();
$result->racine = $racine;
$result->idClient = $idClient;
$result->droitsClients = $droitsClients;
} else {
$error->errnum = 0;
$error->errmsg = 'Acces non authorisé!';
}
$output = new NextLoginReturn();
$output->error = $error;
$output->result = $result;
return $output;
}
/**
* Récupère la liste des clients ou les informations d'un client
* @param integer|boolean $idClient
* @param ClientFiltre $filtre
* @return ListeClientsReturn
*/
public function getListeClients($idClient=false, $filtre=null)
{
$this->authenticate();
//Initialisation
$error = new ErrorType();
if (empty($idClient)) $idClient = false;
$isAuthorized = false;
if ($this->tabInfoUser['profil']=='Administrateur' &&
($idClient=='false' || $idClient==$this->tabInfoUser['idClient'])){
$idClient = $this->tabInfoUser['idClient'];
$isAuthorized = true;
}
if ($this->tabInfoUser['profil']<>'SuperAdministrateur'){
$error->errnum = 1;
$error->errmsg = 'Profil non administrateur';
} else {
$isAuthorized = true;
}
if ($isAuthorized)
{
$strClient = '';
if (is_numeric($idClient)) $strClient.=" AND id='$idClient' ";
$iDbCrm = new WDB('sdv1');
$rep = $iDbCrm->select('clients',
'id, nom, actif, test, racineLogin, siren, nic, tva, editerFacture, fact_detail, fac_dest, fac_adr1, fac_adr2, fac_adr3, fac_email, fac_tel, fact_rib, liv_dest, liv_adr1, liv_adr2, liv_adr3, liv_email, liv_tel, droits, filtres_ip, dateInsert, dateUpdate, respComSD, typeContrat, dateSignature, typeAcces, typeScore, timeout, accesPieces, accesKbis, accesInvestigations, accesInternationnal, accesEnquetes, miseSousSurveillance, forfaitExtranetPeriode, forfaitExtranetMontant, reconductionAuto, remarque, forfaitPiecesNb, forfaitPiecesMt, forfaitPiecesDep, forfaitInvestigNb, forfaitInvestigMt, forfaitInvestigDep, tarifIndiscore',
"1 $strClient",
false,
MYSQL_ASSOC);
$tabRet = array();
foreach ($rep as $uti)
{
$client = new Client();
$client->idClient = $uti['id'];
$client->nom = $uti['nom'];
$client->actif = $uti['actif'];
$client->test = $uti['test'];
$client->racineLogin = $uti['racineLogin'];
$client->siren = $uti['siren'];
$client->nic = $uti['nic'];
$client->tva = $uti['tva'];
$client->editerFacture = $uti['editerFacture'];
$client->fact_detail = $uti['fact_detail'];
$client->fac_dest = $uti['fac_dest'];
$client->fac_adr1 = $uti['fac_adr1'];
$client->fac_adr2 = $uti['fac_adr2'];
$client->fac_adr3 = $uti['fac_adr3'];
$client->fac_email = $uti['fac_email'];
$client->fac_tel = $uti['fac_tel'];
$client->fact_rib = $uti['fact_rib'];
$client->liv_dest = $uti['liv_dest'];
$client->liv_adr1 = $uti['liv_adr1'];
$client->liv_adr2 = $uti['liv_adr2'];
$client->liv_adr3 = $uti['liv_adr3'];
$client->liv_email = $uti['liv_email'];
$client->liv_tel = $uti['liv_tel'];
$client->droits = strtolower($uti['droits']);
$client->timeout = $uti['timeout'];
$client->filtres_ip = $uti['filtres_ip'];
$client->dateInsert = $uti['dateInsert'];
$client->dateUpdate = $uti['dateUpdate'];
$client->respComSD = $uti['respComSD'];
$client->typeContrat = $uti['typeContrat'];
$client->dateSignature = $uti['dateSignature'];
$client->typeAcces = $uti['typeAcces'];
$client->typeScore = $uti['typeScore'];
$client->accesPieces = $uti['accesPieces'];
$client->accesKbis = $uti['accesKbis'];
$client->accesInvestigations = $uti['accesInvestigations'];
$client->accesInternationnal = $uti['accesInternationnal'];
$client->accesEnquetes = $uti['accesEnquetes'];
$client->miseSousSurveillance = $uti['miseSousSurveillance'];
$client->forfaitExtranetPeriode = $uti['forfaitExtranetPeriode'];
$client->forfaitExtranetMontant = $uti['forfaitExtranetMontant'];
$client->reconductionAuto = $uti['reconductionAuto'];
$client->remarque = $uti['remarque'];
$client->forfaitPiecesNb = $uti['forfaitPiecesNb'];
$client->forfaitPiecesMt = $uti['forfaitPiecesMt'];
$client->forfaitPiecesDep = $uti['forfaitPiecesDep'];
$client->forfaitInvestigNb = $uti['forfaitInvestigNb'];
$client->forfaitInvestigMt = $uti['forfaitInvestigMt'];
$client->forfaitInvestigDep = $uti['forfaitInvestigDep'];
$client->tarifIndiscore = $uti['tarifIndiscore'];
$tabRet[] = $client;
}
}
$output = new ListeClientsReturn();
$output->error = $error;
$output->result = $tabRet;
return $output;
}
/**
* Retourne la liste des droits
* @return ListeDroitsReturn[]
*/
public function getListeDroits()
{
$tabRet = array();
foreach($this->listeDroits as $code => $desc){
$droit = new ListeDroitsReturn();
$droit->code = $code;
$droit->desc = $desc;
$tabRet[] = $droit;
}
return $tabRet;
}
/**
* Catégorie des accès
* @param string $code
* @return ListCategoryReturn[]
*/
public function getCategory($code = null)
{
$tabRet = array();
foreach($this->listeCategory as $code => $desc){
$c = new ListCategoryReturn();
$c->code = $code;
$c->desc = $desc['label'];
$c->droits = $desc['droits'];
$tabRet[] = $c;
}
return $tabRet;
}
/**
* Retourne la liste des préférences
* @return ListePrefsReturn[]
*/
public function getListePrefs()
{
$tabRet = array();
foreach ( $this->listePrefs as $code => $desc ) {
$pref = new ListePrefsReturn();
$pref->code = $code;
$pref->desc = $desc;
$tabRet[] = $pref;
}
return $tabRet;
}
/**
* Retourne la liste des logs
* @return ListeLogsReturn[]
*/
public function getLogs()
{
$tabRet = array();
foreach ( $this->logs as $code => $desc ) {
$log = new ListeLogsReturn();
$log->code = $code;
$log->desc = $desc['label'];
$tabRet[] = $log;
}
return $tabRet;
}
/**
* Récupère les informations du profil connecté
* @param string $login
* @param int $idClient
* @return ListeUtilisateursReturn
*/
public function getListeUtilisateurs($login, $idClient=null)
{
$this->authenticate();
//Initialisation
$error = new ErrorType();
$tabRet = array();
if ($this->tabInfoUser['profil']<>'Administrateur' &&
$this->tabInfoUser['profil']<>'SuperAdministrateur')
{
$error->errnum = 1;
$error->errmsg = 'Profil non administrateur';
} else {
$iDbCrm = new WDB('sdv1');
if (!isset($idClient)){
$rep = $iDbCrm->select('utilisateurs', 'idClient', "login='$login'");
$idClient = $rep[0][0];
}
if ($idClient==-1) {
$error->errnum = 1;
$error->errmsg = 'idClient=0';
} else {
$rep = $iDbCrm->select(
'utilisateurs',
'id, idClient, login, email, actif, nom, prenom, referenceParDefaut',
"idClient='$idClient' AND deleted=0 ORDER BY login ASC",
false, MYSQL_ASSOC);
foreach ($rep as $uti){
$utilisateur = new Utilisateur();
$utilisateur->idUti = $uti['id'];
$utilisateur->idClient= $uti['idClient'];
$utilisateur->login = $uti['login'];
$utilisateur->email = $uti['email'];
$utilisateur->actif= $uti['actif'];
$utilisateur->nom = $uti['nom'];
$utilisateur->prenom = $uti['prenom'];
$utilisateur->reference = $uti['referenceParDefaut'];
$tabRet[]= $utilisateur;
}
}
}
$output = new ListeUtilisateursReturn();
$output->error = $error;
$output->result = $tabRet;
return $output;
}
/**
* getLogsClients
* @param string $mois (Format : AAAA-MM ou AAAA/MM)
* @param int $detail
* @param int $idClient
* @param string $login
* @param int $all
* @return LogsClientsReturn
*/
public function getLogsClients($mois, $detail=0, $idClient=0, $login='', $all=0)
{
$this->authenticate();
//Initialisation
$error = new ErrorType();
if (empty($detail)) $detail = 0;
if (empty($idClient)) $idClient = 0;
if (empty($login)) $login = '';
if (empty($all)) $all = 0;
$strDetail = $strClient = $strLogin='';
// Vue détaillée ou uniquement les infos payantes
if ($detail==0) {
$detail='Non';
//$strDetail=" AND (page LIKE 'greffe_%' OR page LIKE 'inpi_%' OR page='kbis' OR page LIKE 'indiscore%' OR page='privileges' OR page='commandeAsso') AND (params<>'' OR page LIKE 'indiscore%' AND tarifIndiscore<>0) AND (params NOT LIKE '%erreur%' OR params LIKE '%erreur 17%') ";
$strDetail = ' AND ( ';
$pages = array('greffe_actes', 'greffe_bilans', 'kbis', 'indiscore', 'indiscore2', 'indiscore3', 'rapport2', 'rapport3', 'privileges', 'privcumul');
$totalPages = count($pages);
$i=0;
foreach( $pages as $page ) {
switch($page)
{
case 'greffe_actes':
case 'greffe_bilans':
$strDetail.= "(l.page = '".$page."' AND l.params<>'' AND (l.params NOT LIKE '%erreur%' OR l.params LIKE '%erreur 17%'))";
break;
case 'kbis':
$strDetail.= "(l.page = '".$page."' AND l.params<>'' AND l.params<>'1/1/0//' AND l.params<>'0/1/0//')";
break;
default:
$strDetail.= "(l.page = '".$page."')";
break;
}
$i++;
if ( $i<$totalPages) {
$strDetail.= ' OR ';
}
}
$strDetail.= ' ) ';
} elseif($detail==1) {
$detail='Oui';
}
// On veut uniquement le client précis
if ($all && $this->tabInfoUser['profil']=='SuperAdministrateur') {
$all = 'Oui';
} else {
$all = 'Non';
}
// Pas d'idClient mentionné ou tentative sur autre client et non SAD
if ($idClient==0 || ($idClient!=$this->tabInfoUser['idClient'] && $this->tabInfoUser['profil']!='SuperAdministrateur') ){
$idClient = $this->tabInfoUser['idClient'];
}
$strClient = " AND u.idClient=$idClient ";
// On veut uniquement le login précis
if ($login!='') {
$strLogin=" AND l.login='$login' ";
} else {
$login='Non';
}
// Dates de début et de fin
$mois = strtr($mois, array('-'=>'','/'=>''));
$annee = substr($mois,0,4);
$mois = substr($mois,4,2);
$dateDeb=date('Y-m-d', mktime(0,0,0,$mois,1,$annee));
$dateFin=date('Y-m-t', mktime(0,0,0,$mois,15,$annee));
$c = Zend_Registry::get('config');
$fichierCsv=$c->profil->path->files."/logs-$annee-$mois-$detail-$idClient-$login-$all.csv";
if (file_exists($fichierCsv)
&& date('Ymd', filemtime($fichierCsv))==date('Ymd')
&& filesize($fichierCsv)>60 ) {
$size=filesize($fichierCsv);
$erreur=false;
$tabNom=array();
$cache=1;
} else {
@unlink($fichierCsv);
$sql="SELECT LOWER(l.login) as loginUti, page, l.siren, l.nic, l.params, l.dateHeure, u.idClient, c.nom, u.referenceParDefaut AS refUti
FROM `logs` l, utilisateurs u, clients c
WHERE 1 $strDetail $strClient $strLogin AND dateHeure BETWEEN '$dateDeb 00:00:00' AND '$dateFin 23:59:59' AND l.login=u.login AND u.idClient=c.id
ORDER BY l.login ASC, l.dateHeure ASC";
$fp = fopen($c->profil->path->cache."/logs-$annee-$mois-$detail-$idClient-$login-$all.sql", 'w');
fwrite($fp, $sql.EOL);
fclose($fp);
exec("php ".$c->profil->path->batch."/sql2csv.php sdv1 ".$c->profil->path->cache."/logs-$annee-$mois-$detail-$idClient-$login-$all.sql $fichierCsv > /dev/null &");
$size = $cache = 0;
}
$hostname = 'http://'.$_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT']!='80'){
$hostname.= ':'.$_SERVER['SERVER_PORT'];
}
$result = new LogsClients();
$result->Url = $hostname."/fichier/logs/logs-$annee-$mois-$detail-$idClient-$login-$all.csv";
$result->Taille = $size;
$result->Cache = $cache;
$output = new LogsClientsReturn();
$output->error = $error;
$output->result = $result;
return $output;
}
/**
* Création/Modification d'un client
* @param string $infos
* @return ClientReturn
*/
public function setClient($infos)
{
$this->authenticate();
$result = false;
//Pas SuperAdministrateur
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
throw new SoapFault(1, 'Profil insuffisant pour cette méthode');
} else {
$infos = json_decode($infos);
/**
* - Protéger les chaines de caractères
* - Gérer les champs select
*/
$idClient = null;
$tabInfos = array();
foreach($infos as $nom => $value)
{
switch($nom)
{
case 'idClient':
$idClient = $value;
break;
case 'nom':
$tabInfos['nom'] = $value;
break;
case 'actif':
$tabInfos['actif'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'test':
$tabInfos['test'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'racineLogin':
$tabInfos['racineLogin'] = $infos->racineLogin;
break;
case 'siren':
$tabInfos['siren'] = (int) $value;
break;
case 'nic':
$tabInfos['nic'] = (int) $value;
break;
case 'tva':
$tabInfos['tva'] = $value;
break;
case 'idClientFacture':
$tabInfos['idClientFacture'] = $value;
break;
case 'editerFacture':
$tabInfos['editerFacture'] = ($value=='Non') ? 'Non' : 'Oui';
break;
case 'fact_detail':
$tabInfos['fact_detail'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'fac_dest':
$tabInfos['fac_dest'] = $value;
break;
case 'fac_adr1':
$tabInfos['fac_adr1'] = $value;
break;
case 'fac_adr2':
$tabInfos['fac_adr2'] = $value;
break;
case 'fac_adr3':
$tabInfos['fac_adr3'] = $value;
break;
case 'fac_email':
$tabInfos['fac_email'] = $value;
break;
case 'fac_tel':
$tabInfos['fac_tel'] = $value;
break;
case 'fact_rib':
if (in_array($value, array('BPOSTALE','CCOOP', 'CDNORD'))){
$tabInfos['fact_rib'] = $value;
}
break;
case 'liv_dest':
$tabInfos['liv_dest'] = $value;
break;
case 'liv_adr1':
$tabInfos['liv_adr1'] = $value;
break;
case 'liv_adr2':
$tabInfos['liv_adr2'] = $value;
break;
case 'liv_adr3':
$tabInfos['liv_adr3'] = $value;
break;
case 'liv_email':
$tabInfos['liv_email'] = $value;
break;
case 'liv_tel':
$tabInfos['liv_tel'] = $value;
break;
case 'droits':
if (count($value)>0){
$tabInfos['droits'] = strtolower(implode(' ',$value));
} else {
$tabInfos['droits'] = array();
}
break;
case 'filtres_ip':
$tabInfos['filtres_ip'] = $value;
break;
case 'typeContrat':
if (in_array($value, array('Contrat','Marché'))){
$tabInfos['typeContrat'] = $value;
}
break;
case 'typeAcces':
if (in_array($value, array('userPassword','userPasswordIP', 'IP'))){
$tabInfos['typeAcces'] = $value;
}
break;
case 'typeScore':
if (in_array($value, array('20','100'))){
$tabInfos['typeScore'] = $value;
}
break;
case 'timeout':
if (!empty($value)){
$tabInfos['timeout'] = $value;
}
break;
case 'accesPieces':
$tabInfos['accesPieces'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'accesKbis':
$tabInfos['accesKbis'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'accesInvestigations':
$tabInfos['value'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'accesInternationnal':
$tabInfos['accesInternationnal'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'accesEnquetes':
$tabInfos['accesEnquetes'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'miseSousSurveillance':
$tabInfos['miseSousSurveillance'] = ($value=='Oui') ? 'Oui' : 'Non' ;
break;
case 'forfaitExtranetPeriode':
if (in_array($value, array('Mensuel','Trimestriel','Semestriel', 'Annuel'))){
$tabInfos['forfaitExtranetPeriode'] = $value;
}
break;
case 'forfaitExtranetMontant':
$tabInfos['forfaitExtranetMontant'] = $value;
break;
case 'reconductionAuto':
if ($value=='Non') {
$tabInfos['reconductionAuto'] = 'Non';
}
break;
case 'remarque':
$tabInfos['remarque'] = $value;
break;
case 'forfaitPiecesNb':
$tabInfos['forfaitPiecesNb'] = $value;
break;
case 'forfaitPiecesMt':
$tabInfos['forfaitPiecesMt'] = $value;
break;
case 'forfaitPiecesDep':
$tabInfos['forfaitPiecesDep'] = $value;
break;
case 'forfaitInvestigNb':
$tabInfos['forfaitInvestigNb'] = $value;
break;
case 'forfaitInvestigMt':
$tabInfos['forfaitInvestigMt'] = $value;
break;
case 'forfaitInvestigDep':
$tabInfos['forfaitInvestigDep'] = $value;
break;
case 'tarifIndiscore':
$tabInfos['tarifIndiscore'] = $value;
break;
case 'InterSudLogin':
$tabInfos['InterSudLogin'] = $value;
break;
case 'InterSudPass':
$tabInfos['InterSudPass'] = $value;
break;
}
}
$iDbCrm = new WDB('sdv1');
if (empty($idClient)){
//Ajout
$tabInfos['dateInsert'] = date('Y-m-d H:i:s');
$result = $iDbCrm->insert('clients', $tabInfos, true);
} else {
//Droits client actuel avant modification
if ( isset($tabInfos['droits']) ) {
$rep = $iDbCrm->select('clients', 'droits', "id='$idClient'", false, MYSQL_ASSOC);
$droitsPre = explode(' ', $rep[0]['droits']);
$droitsSui = explode(' ', $tabInfos['droits']);
//Détection suppression d'un droit client
if ( count($droitsSui)<count($droitsPre) ) {
$droitsDiff = array_diff($droitsPre, $droitsSui);
//Modification sur les droits utilisateurs
if ( count($droitsDiff)>0 ){
$this->_deleteAccess($droitsDiff, $idClient);
}
}
}
//Modification
$tabInfos['dateUpdate'] = date('Y-m-d H:i:s');
if ($iDbCrm->update('clients', $tabInfos, "id='$idClient'", true)){
$result = true;
}
}
}
$output = new ClientReturn();
$output->error = $error;
$output->result = $result;
return $output;
}
/**
* Get all user params (only for admin)
* @param string $login
* @return string
*/
public function getUser($login)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0901');
}
try {
$userM = new Application_Model_Sdv1Utilisateurs();
$sql = $userM->select()->where('login = ?', $login);
$rowset = $userM->fetchRow($sql);
if ($rowset) {
$user = $rowset->toArray();
return json_encode($user);
} else {
throw new SoapFault('1','Erreur');
}
} catch (Zend_Db_Exception $e) {
throw new SoapFault('Database',$e->getMessage());
}
}
/**
* Enregistre l'acceptation des CGU pour une application
* @param string $application Nom de l'outil utilisé
* @return boolean
*/
public function setCGU($application = null)
{
$this->authenticate();
$idUser = $this->tabInfoUser['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;
}
/**
* Definir la méthode de tarification
* @param int $idClient Nom de l'élément à facturer
* @param ClientTarif $tarif Informations de facturation
* @param int $id Identifiant pour l'update
* @return int
*/
public function setClientTarif($idClient, $tarif, $id=null)
{
/**
* Une tarification est toujours affecté à un élément $log.
* Celle ci est définie sur un compte client (tous les utilisateurs) ou un service,
* Pour un type de facturation
* - Unitaire : Chaque élement est à facturer
* - Forfait limité : Si tarif unitaire>0 alors on applique le prix unitaire pour chaque
* dépassement, sinon on coupe
* - Forfait illimité :
*/
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
if (!in_array($tarif->log, array_keys($this->logs))) {
throw new SoapFault('ClientTarif', 'log inexistant');
}
if (!in_array($tarif->type, array('Unitaire','ForfaitLimit','ForfaitNoLimit'))) {
throw new SoapFault('ClientTarif', 'Erreur type');
}
if (!in_array($tarif->doublon, array('jour','mois', 'period','none'))) {
throw new SoapFault('ClientTarif', 'Erreur doublon');
}
$time = mktime(0,0,0,substr($tarif->date,4,2), substr($tarif->date,6,2), substr($tarif->date,0,4));
$data = array(
'idClient' => $idClient,
'service' => $tarif->service,
'log' => $tarif->log,
'type' => $tarif->type,
'priceUnit' => $tarif->priceUnit,
'limit' => $tarif->limit,
'dateDebut' => date('Y-m-d H:i:s', $time),
'duree' => $tarif->duree,
'doublon' => $tarif->doublon,
);
try {
$tarifM = new Application_Model_Sdv1ClientsTarifs();
if ( null === $id) {
$result = $tarifM->insert($data);
} else {
$result = $tarifM->update($data, 'id='.$id);
}
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
return $result;
}
/**
* Get list of tarif
* @param int $idClient
* @param string $service
* @throws SoapFault
* @return ClientContrat[]
*/
public function getClientTarifs($idClient, $service = null)
{
$this->authenticate();
if ( null === $service ) {
$service = 'DEFAULT';
}
//Liste dateDebut-duree disponible
$tarifM = new Application_Model_Sdv1ClientsTarifs();
try {
$sql = $tarifM->select()->from($tarifM,array(
'dateDebut',
'duree'
))
->where('service=?',$service)
->order('dateDebut DESC')
->group(array('dateDebut', 'duree'));
$contrats = $tarifM->fetchAll($sql);
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
$listeContrat = array();
//Retrouver les éléments à facturer
if ( count($contrats)>0 ) {
foreach ( $contrats as $contrat ) {
try {
$sql = $tarifM->select()
->where('idClient=?', $idClient)
->where('service=?',$service)
->where('dateDebut=?',$contrat->dateDebut)
->where('duree=?',$contrat->duree);
$rows = $tarifM->fetchAll($sql);
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
$output = array();
if ( $rows->count()>0 ) {
foreach ( $rows as $row ) {
$item = new ClientTarif();
$item->id = $row->id;
$item->idClient = $row->idClient;
$item->service = $row->service;
$item->log = $row->log;
$item->type = $row->type;
$item->priceUnit = $row->priceUnit;
$item->limit = $row->limit;
$item->date = substr($row->dateDebut,0,10);
$item->duree = $row->duree;
$item->doublon = $row->doublon;
$output[] = $item;
}
}
$date = new Zend_Date($contrat->dateDebut, false);
$dateBegin = $date->toString('Y-MM-dd');
$dateEnd = $date->add($contrat->duree, Zend_Date::DAY)->toString('YYYY-MM-dd');
$ElementContrat = new ClientContrat();
$ElementContrat->dateBegin = $dateBegin;
$ElementContrat->dateEnd = $dateEnd;
$ElementContrat->tarifs = $output;
$listeContrat[] = $ElementContrat;
}
}
return $listeContrat;
}
/**
* Get a tarif and his details by date
* @param string $log
* @param int $idClient
* @param string $service
* @param string $date
* @throws SoapFault
* @return ClientTarif
*/
public function getClientTarif($log, $idClient, $service='default', $date=null)
{
$this->authenticate();
if ( empty($service) ) {
$service = 'default';
}
//Date not define, looks for the actual price
if ( empty($date) ) {
}
try {
$tarifM = new Application_Model_Sdv1ClientsTarifs();
$sql = $tarifM->select()
->from($tarifM, array(
'id', 'idClient', 'service', 'log', 'type', 'priceUnit', 'limit', 'dateDebut',
'duree', 'doublon', 'DATE_ADD(dateDebut, INTERVAL duree DAY) AS dateFin'
))
->where('log=?',$log)
->where('idClient=?', $idClient)
->where('service=?',$service);
//->where('dateDebut<=?', $date);
//->where('dateFin>?',$date);
//@todo : get date between
$row = $tarifM->fetchRow($sql);
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
return $row;
}
/**
* Liste des prestations pour un client
* @param int $idClient
* @return ClientPrestation[]
* @throws SoapFault
*/
public function getPrestations($idClient)
{
$this->authenticate();
//Read prestations
try {
$prestationsM = new Application_Model_Sdv1Prestations();
$sql = $prestationsM->select()
->from($prestationsM, array('id', 'typeprestation', 'datemiseenplace',
'datefinprestation' ,'identifiantPrestation', 'prestationactive'))
->where('idClient=?', $idClient);
$result = $prestationsM->fetchAll($sql);
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']!=1) {
throw new SoapFault('ERR', "Application error");
} else {
throw new SoapFault('ERR', $e->getMessage());
}
}
$output = array();
if ($result->count()>0) {
foreach ( $result as $item ) {
$clientprestation = new ClientPrestation();
$clientprestation->id = $item->id;
$clientprestation->type = $item->typeprestation;
$clientprestation->identifiant = $item->identifiantPrestation;
$clientprestation->active = $item->prestationactive;
$clientprestation->dateDebut = $item->datemiseenplace;
$clientprestation->dateFin = $item->datefinprestation;
$output[] = $clientprestation;
}
}
return $output;
}
/**
* Liste des paramètres d'une prestation
* @param int $id
* @return string
* @throws SoapFault
*/
public function getPrestation($id)
{
$this->authenticate();
//Read prestations
try {
$prestationsM = new Application_Model_Sdv1Prestations();
$sql = $prestationsM->select()->where('id=?', $id);
$result = $prestationsM->fetchRow($sql);
if ($result!==null) {
return json_encode($result->toArray());
} else {
return '';
}
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']!=1) {
throw new SoapFault('ERR', "Application error");
} else {
throw new SoapFault('ERR', $e->getMessage());
}
}
}
/**
* Enregistre les paramètres des la prestation
* @param int $idClient
* @param string $prestation
* @return int
* @throws SoapFault
*/
public function setPrestation($idClient, $prestation)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
$prestationParameters = json_decode($prestation, true);
//Insert or update a prestation
try {
$prestationsM = new Application_Model_Sdv1Prestations();
//Update
if (!empty($prestationParameters['id'])) {
$id = $prestationParameters['id'];
unset($prestationParameters['id']);
$result = $prestationsM->update($prestationParameters, 'id='.$id);
}
//Insert
else {
//@todo : Some control before save
$result = $prestationsM->insert($prestationParameters);
}
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']!=1) {
throw new SoapFault('ERR', "Application error");
} else {
throw new SoapFault('ERR', $e->getMessage());
}
}
return $result;
}
/**
* Define a Service in Client
* @param int $idClient
* @param string $infos
* @throws SoapFault
* @return int
*/
public function setService($idClient, $infos=null)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
try {
$serviceM = new Application_Model_Sdv1ClientsServices();
$dataIn = json_decode($infos, true);
$code = $dataIn['code'];
$label = $dataIn['label'];
$droits = $dataIn['droits'];
//Check if code exist in idClient
$sql = $serviceM->select()
->where('idClient=?',$idClient)
->where('code=?',$code);
$row = $serviceM->fetchAll($sql);
if ($row->count()>0) {
$data = array(
'label' => $label,
'droits' => $droits
);
$result = $serviceM->update($data, array('idClient='.$idClient, 'code='.$code));
//Delete
if ( isset($row->droits) ) {
$droitsPre = explode(' ', $row->droits);
$droitsSui = explode(' ', $droits);
//Détection suppression d'un droit client
if ( count($droitsSui)<count($droitsPre) ) {
$droitsDiff = array_diff($droitsPre, $droitsSui);
//Modification sur les droits utilisateurs
if ( count($droitsDiff)>0 ){
$this->_deleteAccess($droitsDiff, $idClient, $code);
}
}
}
} else {
$data = array(
'code' => $code,
'label' => $label,
'droits' => $droits,
'idClient' => $idClient
);
$result = $serviceM->insert($data);
}
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
return $result;
}
/**
* Set a User in a Service
* @param string $login
* @param string $code
* @throws SoapFault
* @return int
*/
public function setUserService($login, $code)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
try {
$serviceM = new Application_Model_Sdv1UtilisateursService();
//Check if a login exist
$sql = $serviceM->select()->where('login=?',$login);
$row = $serviceM->fetchAll($sql);
if ($row->count()>0) {
$data = array(
'serviceCode' => $code,
);
$result = $serviceM->update($data, 'login='.$login);
} else {
$data = array(
'login' => $login,
'serviceCode' => $code,
);
$result = $serviceM->insert($data);
}
} catch (Zend_Db_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
//Override login right access - always
$serviceM = new Application_Model_Sdv1ClientsServices();
$sql = $serviceM->select()->where('code=?',$code);
$serviceInfo = $serviceM->fetchRow($sql);
if ( null !== $serviceInfo ) {
$userM = new Application_Model_Sdv1Utilisateurs();
$userM->update(array('droits'=>$serviceInfo->droits), 'login='.$login);
}
return $result;
}
/**
* List Services
* @param int $idClient
* @throws SoapFault
* @return Service[]
*/
public function getServices($idClient)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
try {
$serviceM = new Application_Model_Sdv1ClientsServices();
$sql = $serviceM->select()
->where('idClient=?', $idClient)
->order('label ASC');
$rows = $serviceM->fetchAll($sql);
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
$output = array();
if ($rows->count()>0) {
foreach($rows as $item) {
$service = new Service();
$service->code = $item->code;
$service->label = $item->label;
$output[] = $service;
}
}
return $output;
}
/**
* Get service parameters
* @param int $idClient
* @param string $code
* @throws SoapFault
* @return string|boolean
*/
public function getService($idClient, $code)
{
$this->authenticate();
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
try {
$serviceM = new Application_Model_Sdv1ClientsServices();
$sql = $serviceM->select()
->where('idClient=?', $idClient)
->where('serviceCode=?', $code);
$row = $serviceM->fetchRow($sql);
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
if (null !== $row) {
return json_encode($row->toArray());
}
return false;
}
/**
* Retourne la liste des utilisateurs par service
* @param int $idClient
* @param string $serviceCode
* @throws SoapFault
* @return Utilisateur[]
*/
public function getServiceUsers($idClient, $serviceCode=null)
{
$this->authenticate();
if ( null === $serviceCode ) {
$serviceCode = 'DEFAULT';
}
if ($this->tabInfoUser['profil']!='SuperAdministrateur') {
$this->sendError('0902');
}
$output = array();
//Login by service
if ($serviceCode != 'DEFAULT') {
//Check if service exist
try {
$servicesM = new Application_Model_Sdv1ClientsServices();
$sql = $servicesM->select()
->from($servicesM, array('code'))
->where('idClient=?', $idClient);
$result = $servicesM->fetchAll($sql);
if ($result->count()==0) {
throw new SoapFault('ERR', 'Service inexistant');
}
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
//List login
try {
$userserviceM = new Application_Model_Sdv1UtilisateursService();
$sql = $userserviceM->select()
->where('serviceCode=?', $serviceCode)
->order('login ASC');
$logins = $userserviceM->fetchAll($sql);
} catch (Zend_Db_Adapter_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
} catch (Zend_Exception $e) {
throw new SoapFault('ERR', "Application error");
}
//Get login informations
$userM = new Application_Model_Sdv1Utilisateurs();
if ($logins->count()>0) {
foreach($logins as $login) {
$sql = $userM->select()
->where('login=?', $login->login)
->where('deleted=0');
$result = $userM->fetchRow($sql);
$utilisateur = new Utilisateur();
$utilisateur->idUti = $result->id;
$utilisateur->login = $result->login;
$utilisateur->actif = $result->actif;
$utilisateur->nom = $result->nom;
$utilisateur->prenom = $result->prenom;
$utilisateur->email = $result->email;
$utilisateur->reference = $result->reference;
$output[] = $utilisateur;
}
}
}
//Select all logins which have service to display only login without service
else {
$servicesM = new Application_Model_Sdv1ClientsServices();
$sql = $servicesM->select()
->from($servicesM, array('code'))
->where('idClient=?', $idClient);
$result = $servicesM->fetchAll($sql);
$codes = array();
$notlogins = array();
if ($result->count()>0) {
foreach ($result as $item) {
$codes[] = $item->code;
}
}
if (count($codes)>0) {
try {
$userserviceM = new Application_Model_Sdv1UtilisateursService();
$sql = $userserviceM->select()
->where('serviceCode IN ("'.join(',',$codes).'")');
$result = $userserviceM->fetchAll($sql);
} catch (Zend_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
if ($result->count()>0) {
foreach ($result as $item) {
$notlogins[] = $item->login;
}
}
}
$userM = new Application_Model_Sdv1Utilisateurs();
if (count($notlogins)>0) {
$sql = $userM->select()
->from($userM, array('id', 'login', 'actif', 'nom', 'prenom', 'email', 'referenceParDefaut'))
->where('login NOT IN('.join(',',$notlogins).')')
->where('idClient=?',$idClient)
->where('deleted=0')
->order('login ASC');
} else {
$sql = $userM->select()
->from($userM, array('id', 'login', 'actif', 'nom', 'prenom', 'email', 'referenceParDefaut'))
->where('idClient=?',$idClient)
->where('deleted=0')
->order('login ASC');
}
$logins = $userM->fetchAll($sql);
if ($logins->count()>0) {
foreach ($logins as $result) {
$utilisateur = new Utilisateur();
$utilisateur->idUti = $result->id;
$utilisateur->login = $result->login;
$utilisateur->actif = $result->actif;
$utilisateur->nom = $result->nom;
$utilisateur->prenom = $result->prenom;
$utilisateur->email = $result->email;
$utilisateur->reference = $result->referenceParDefaut;
$output[] = $utilisateur;
}
}
}
return $output;
}
/**
* Override user params
* @param string $element
* @param int $idClient
* @param string $service
* @param string $login
* @throws SoapFault
* @return boolean
*/
public function setParam($element, $idClient, $service, $login=null)
{
$this->authenticate();
$element = strtoupper($element);
if ( !in_array($element, array('IP', 'ACCESS')) ) {
throw new SoapFault('ERR', 'Invalid parameters');
}
switch ($element) {
case 'IP':
break;
case 'ACCESS':
$logins = $this->_serviceLogins($idClient, $service);
if ( $login !== null ) {
if ( !in_array($login,$logins) ) {
throw new SoapFault('ERR', "Login not found");
} else {
$logins = array($login);
}
}
if ( count($logins)>0 ) {
//Get access
if ($service == 'DEFAULT') {
$clientM = new Application_Model_Sdv1Clients();
$sql = $clientM->select()->where('idClient=?', $idClient);
$row = $clientM->fetchRow($sql);
$droits = $row->droits;
} else {
$serviceM = new Application_Model_Sdv1ClientsServices();
$sql = $serviceM->select()->where('code=?', $service);
$row = $serviceM->fetchRow($sql);
$droits = $row->droits;
}
//Override access
$userM = new Application_Model_Sdv1Utilisateurs();
$result = $userM->update(array('droits'=>$droits), 'login IN ('.join(',',$logins).')');
}
break;
}
if (empty($result)) {
return false;
}
return true;
}
/**
* Override email(s) in surveillances
* @param string $login
* @param string $email
* @throws SoapFault
* @return int
*/
public function setSurveillancesMail($login, $email)
{
$this->authenticate();
try {
$surveillanceM = new Application_Model_JoSurveillancesSite();
$result = $surveillanceM->update(array('email'=>$email), "login='$login'");
} catch (Zend_Exception $e) {
if ($this->tabInfoUser['idClient']==1) {
throw new SoapFault('ERR', $e->getMessage());
} else {
throw new SoapFault('ERR', "Application error");
}
}
return $result;
}
/**
* Remove access to the children
* @param array $toDelete
* @param int $idClient
* @param string $service
*/
protected function _deleteAccess($toDelete, $idClient, $service=null)
{
$iDbCrm = new WDB('sdv1');
$strLogin = '';
//Modification sur les droits des services
if ( count(toDelete)>0 ) {
foreach ($toDelete as $droit) {
$query = "UPDATE clients_services SET droits=TRIM(REPLACE(droits, ' $droit ', ' ')) WHERE idClient='$idClient'";
$iDbCrm->query($query);
}
}
if (null !== $service) {
//Récupération des logins utilisateurs du service
$logins = $this->_serviceLogins($idClient, $service);
if (count($logins)>0) {
$strLogin = ' AND login IN('.join(',',$logins->toArray()).')';
}
}
//Modification sur les droits utilisateurs
if ( count(toDelete)>0 ) {
foreach ($toDelete as $droit) {
$query = "UPDATE utilisateurs set droits=TRIM(REPLACE(droits, ' $droit ', ' ')) WHERE idClient='$idClient'";
$query.= $strLogin;
$iDbCrm->query($query);
}
}
}
/**
* Give a list of logins in a service
* @param int $idClient
* @param string $serviceCode
* @return array
*/
protected function _serviceLogins($idClient, $serviceCode)
{
$serviceCode = strtoupper($serviceCode);
$userM = new Application_Model_Sdv1Utilisateurs();
$sql = $userM->select()
->from(array('u'=>'utilisateurs'), array('login'))
->join(array('s'=>'utilisateurs_service'), 'u.login=s.login');
if ($serviceCode == 'DEFAULT') {
$sql->where('serviceCode=?', null);
} else {
$sql->where('serviceCode=?', $serviceCode);
}
$logins = $userM->fetchAll($sql)->toArray();
return $logins;
}
}