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 {
|
|
|
78 |
if (count($this->ressources) == 1 && ($this->ressources[0] == 'deconnecter')) {
|
|
|
79 |
$this->sousServiceNom = 'deconnecter';
|
|
|
80 |
} else if(count($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
|
|
|
81 |
$this->sousServiceNom = 'preferences';
|
2049 |
aurelien |
82 |
} else if(count($this->ressources) == 2 && $this->ressources[1] == 'activite') {
|
|
|
83 |
$this->sousServiceNom = 'activite';
|
1826 |
jpm |
84 |
} else if (count($this->ressources) == 2) {
|
|
|
85 |
$this->sousServiceNom = 'connecter';
|
|
|
86 |
} else {
|
|
|
87 |
$this->sousServiceNom = 'identification';
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
} else if ($this->methode == 'modifier') {
|
|
|
91 |
if (count($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
|
|
|
92 |
$this->sousServiceNom = 'preferences';
|
|
|
93 |
}
|
|
|
94 |
}
|
1666 |
jpm |
95 |
|
1826 |
jpm |
96 |
if ($this->sousServiceNom == null) {
|
|
|
97 |
$this->lancerMessageErreurRessource();
|
|
|
98 |
}
|
803 |
gduche |
99 |
}
|
|
|
100 |
|
1826 |
jpm |
101 |
private function lancerMessageErreurRessource() {
|
|
|
102 |
$ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources);
|
|
|
103 |
$message = "La ressource demandée '$ressource' ".
|
|
|
104 |
"n'est pas disponible pour le service ".$this->serviceNom." !\n".
|
|
|
105 |
"Les URLs disponibles sont : \n".
|
|
|
106 |
" * en GET : \n".
|
|
|
107 |
" - utilisateurs : identification anonyme à partir des cookies (par défaut quelque soit l'url)\n".
|
|
|
108 |
" - utilisateurs/#login/#mot-de-passe : connecte l'utilisateur\n".
|
|
|
109 |
" - utilisateurs/deconnecter : déconnecte l'utilisateur actuellement connecté par cookie\n".
|
|
|
110 |
" - utilisateurs/#id/preferences : fourni les préférences de l'utilisateur #id\n".
|
|
|
111 |
" * en POST : \n".
|
|
|
112 |
" - utilisateurs/#id/preferences : permet de modifier \n";
|
|
|
113 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
114 |
throw new Exception($message, $code);
|
803 |
gduche |
115 |
}
|
|
|
116 |
|
|
|
117 |
private function initialiserService() {
|
1826 |
jpm |
118 |
$classe = $this->obtenirNomClasseService($this->sousServiceNom);
|
803 |
gduche |
119 |
$chemins = array();
|
1826 |
jpm |
120 |
$chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php';
|
803 |
gduche |
121 |
$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
|
|
|
122 |
$retour = '';
|
|
|
123 |
$service = null;
|
|
|
124 |
foreach ($chemins as $chemin) {
|
|
|
125 |
if (file_exists($chemin)) {
|
|
|
126 |
require_once $chemin;
|
|
|
127 |
$service = new $classe($this->conteneur);
|
|
|
128 |
if ($this->methode == 'consulter') {
|
|
|
129 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
1174 |
aurelien |
130 |
} elseif ($this->methode == 'modifier') {
|
1666 |
jpm |
131 |
$retour = $service->modifier($this->ressources, $this->parametres);
|
803 |
gduche |
132 |
}
|
|
|
133 |
}
|
|
|
134 |
}
|
1666 |
jpm |
135 |
|
803 |
gduche |
136 |
if (is_null($service)) {
|
1826 |
jpm |
137 |
$ressource = $this->serviceNom.'/'.implode('/', $this->ressources);
|
|
|
138 |
$message = "Le classe '$classe' correspondant à la ressource '$ressource' ".
|
|
|
139 |
"n'existe pas dans le service '{$this->serviceNom}' !";
|
803 |
gduche |
140 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
141 |
throw new Exception($message, $code);
|
|
|
142 |
}
|
|
|
143 |
return $retour;
|
|
|
144 |
}
|
1666 |
jpm |
145 |
|
803 |
gduche |
146 |
private function obtenirNomClasseService($mot) {
|
|
|
147 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
148 |
return $classeNom;
|
|
|
149 |
}
|
1826 |
jpm |
150 |
}
|