2212 |
arthur |
1 |
<?php
|
|
|
2 |
// declare(encoding='UTF-8');
|
|
|
3 |
/**
|
|
|
4 |
* Classe principale de chargement des sous-services liés aux utilisateurs.
|
|
|
5 |
*
|
|
|
6 |
* @category DEL
|
|
|
7 |
* @package Services
|
|
|
8 |
* @subpackage Utilisateurs
|
|
|
9 |
* @version 0.1
|
|
|
10 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
13 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
14 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
15 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
16 |
*/
|
|
|
17 |
class Utilisateurs extends RestService {
|
|
|
18 |
|
|
|
19 |
private $parametres = array();
|
|
|
20 |
private $ressources = array();
|
|
|
21 |
private $methode = null;
|
|
|
22 |
private $serviceNom = 'utilisateurs';
|
|
|
23 |
private $sousServiceNom = null;
|
|
|
24 |
private $cheminCourant = null;
|
|
|
25 |
|
|
|
26 |
private $conteneur;
|
|
|
27 |
|
|
|
28 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
29 |
protected $utilisationParametresBruts = true;
|
|
|
30 |
|
|
|
31 |
public function __construct() {
|
|
|
32 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
33 |
$this->conteneur = new Conteneur();
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function consulter($ressources, $parametres) {
|
|
|
37 |
$this->methode = 'consulter';
|
|
|
38 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
39 |
return $this->executerService();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function modifier($ressources, $requeteDonnees) {
|
|
|
43 |
$this->methode = 'modifier';
|
|
|
44 |
$this->initialiserRessourcesEtParametres($ressources, $requeteDonnees);
|
|
|
45 |
return $this->executerService();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
private function initialiserRessourcesEtParametres($ressources, $parametres = array()) {
|
|
|
49 |
$this->ressources = $ressources;
|
|
|
50 |
$this->parametres = $parametres;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
private function executerService() {
|
|
|
54 |
$reponseHttp = new ReponseHttp();
|
|
|
55 |
try {
|
|
|
56 |
$resultat = $this->traiterRessources();
|
|
|
57 |
$reponseHttp->setResultatService($resultat);
|
|
|
58 |
} catch (Exception $e) {
|
|
|
59 |
$reponseHttp->ajouterErreur($e);
|
|
|
60 |
}
|
|
|
61 |
$reponseHttp->emettreLesEntetes();
|
|
|
62 |
$corps = $reponseHttp->getCorps();
|
|
|
63 |
return $corps;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function traiterRessources() {
|
|
|
67 |
$this->analyserRessources();
|
|
|
68 |
$retour = $this->initialiserService();
|
|
|
69 |
return $retour;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function analyserRessources() {
|
|
|
73 |
if ($this->methode == 'consulter') {
|
|
|
74 |
//S'il n'y a pas de ressources => envoyer sur identification anonyme
|
|
|
75 |
if (!isset($this->ressources) || empty($this->ressources)) {
|
|
|
76 |
$this->sousServiceNom = 'identification';
|
|
|
77 |
} else {
|
|
|
78 |
if(count($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
|
|
|
79 |
$this->sousServiceNom = 'preferences';
|
|
|
80 |
} else if(count($this->ressources) == 2 && $this->ressources[1] == 'activite') {
|
|
|
81 |
$this->sousServiceNom = 'activite';
|
|
|
82 |
} else {
|
|
|
83 |
$this->sousServiceNom = 'identification';
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
} else if ($this->methode == 'modifier') {
|
|
|
87 |
if (count($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
|
|
|
88 |
$this->sousServiceNom = 'preferences';
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if ($this->sousServiceNom == null) {
|
|
|
93 |
$this->lancerMessageErreurRessource();
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function lancerMessageErreurRessource() {
|
|
|
98 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
99 |
$message = "La ressource demandée '$ressource' ".
|
|
|
100 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
101 |
"Les URLs disponibles sont : \n".
|
|
|
102 |
" * en GET : \n".
|
|
|
103 |
" - utilisateurs : identification anonyme à partir des cookies (par défaut quelque soit l'url)\n".
|
|
|
104 |
" - utilisateurs/#login/#mot-de-passe : connecte l'utilisateur\n".
|
|
|
105 |
" - utilisateurs/deconnecter : déconnecte l'utilisateur actuellement connecté par cookie\n".
|
|
|
106 |
" - utilisateurs/#id/preferences : fourni les préférences de l'utilisateur #id\n".
|
|
|
107 |
" * en POST : \n".
|
|
|
108 |
" - utilisateurs/#id/preferences : permet de modifier \n";
|
|
|
109 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
110 |
throw new Exception($message, $code);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private function initialiserService() {
|
|
|
114 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
|
|
115 |
$chemins = array();
|
|
|
116 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
|
|
117 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
118 |
$retour = '';
|
|
|
119 |
$service = null;
|
|
|
120 |
foreach ($chemins as $chemin) {
|
|
|
121 |
if (file_exists($chemin)) {
|
|
|
122 |
require_once $chemin;
|
|
|
123 |
$service = new $classe($this->conteneur);
|
|
|
124 |
if ($this->methode == 'consulter') {
|
|
|
125 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
126 |
} elseif ($this->methode == 'modifier') {
|
|
|
127 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if (is_null($service)) {
|
|
|
133 |
$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
|
|
|
134 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
135 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
|
|
136 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
137 |
throw new Exception($message, $code);
|
|
|
138 |
}
|
|
|
139 |
return $retour;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
private function obtenirNomClasseService($mot) {
|
|
|
143 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
144 |
return $classeNom;
|
|
|
145 |
}
|
|
|
146 |
}
|