803 |
gduche |
1 |
<?php
|
1820 |
jpm |
2 |
// declare(encoding='UTF-8');
|
803 |
gduche |
3 |
/**
|
1795 |
jpm |
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 |
*/
|
803 |
gduche |
17 |
class Utilisateurs extends RestService {
|
|
|
18 |
|
|
|
19 |
private $parametres = array();
|
|
|
20 |
private $ressources = array();
|
|
|
21 |
private $methode = null;
|
1826 |
jpm |
22 |
private $serviceNom = 'utilisateurs';
|
|
|
23 |
private $sousServiceNom = null;
|
803 |
gduche |
24 |
private $cheminCourant = null;
|
|
|
25 |
|
|
|
26 |
private $conteneur;
|
1666 |
jpm |
27 |
|
803 |
gduche |
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;
|
1826 |
jpm |
33 |
$this->conteneur = new Conteneur();
|
803 |
gduche |
34 |
}
|
|
|
35 |
|
|
|
36 |
public function consulter($ressources, $parametres) {
|
|
|
37 |
$this->methode = 'consulter';
|
1826 |
jpm |
38 |
$this->initialiserRessourcesEtParametres($ressources, $parametres);
|
|
|
39 |
return $this->executerService();
|
803 |
gduche |
40 |
}
|
1666 |
jpm |
41 |
|
1826 |
jpm |
42 |
public function modifier($ressources, $requeteDonnees) {
|
1174 |
aurelien |
43 |
$this->methode = 'modifier';
|
1826 |
jpm |
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() {
|
1174 |
aurelien |
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 |
}
|
1666 |
jpm |
65 |
|
803 |
gduche |
66 |
private function traiterRessources() {
|
1826 |
jpm |
67 |
$this->analyserRessources();
|
803 |
gduche |
68 |
$retour = $this->initialiserService();
|
|
|
69 |
return $retour;
|
|
|
70 |
}
|
|
|
71 |
|
1826 |
jpm |
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 {
|
2077 |
mathias |
78 |
if(count($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
|
1826 |
jpm |
79 |
$this->sousServiceNom = 'preferences';
|
2049 |
aurelien |
80 |
} else if(count($this->ressources) == 2 && $this->ressources[1] == 'activite') {
|
|
|
81 |
$this->sousServiceNom = 'activite';
|
1826 |
jpm |
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 |
}
|
1666 |
jpm |
91 |
|
1826 |
jpm |
92 |
if ($this->sousServiceNom == null) {
|
|
|
93 |
$this->lancerMessageErreurRessource();
|
|
|
94 |
}
|
803 |
gduche |
95 |
}
|
|
|
96 |
|
1826 |
jpm |
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);
|
803 |
gduche |
111 |
}
|
|
|
112 |
|
|
|
113 |
private function initialiserService() {
|
1826 |
jpm |
114 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
803 |
gduche |
115 |
$chemins = array();
|
1826 |
jpm |
116 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
803 |
gduche |
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);
|
1174 |
aurelien |
126 |
} elseif ($this->methode == 'modifier') {
|
1666 |
jpm |
127 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
803 |
gduche |
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
1666 |
jpm |
131 |
|
803 |
gduche |
132 |
if (is_null($service)) {
|
1826 |
jpm |
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}' !";
|
803 |
gduche |
136 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
137 |
throw new Exception($message, $code);
|
|
|
138 |
}
|
|
|
139 |
return $retour;
|
|
|
140 |
}
|
1666 |
jpm |
141 |
|
803 |
gduche |
142 |
private function obtenirNomClasseService($mot) {
|
|
|
143 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
144 |
return $classeNom;
|
|
|
145 |
}
|
1826 |
jpm |
146 |
}
|