Subversion Repositories eFlore/Applications.del

Rev

Rev 1666 | Rev 1820 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
803 gduche 1
<?php
2
/**
1795 jpm 3
 * Classe principale de chargement des sous-services liés aux utilisateurs.
4
 *
5
 * @category   DEL
6
 * @package    Services
7
 * @subpackage Utilisateurs
8
 * @version    0.1
9
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
10
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
12
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
13
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
14
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
15
 */
803 gduche 16
class Utilisateurs extends RestService {
17
 
1666 jpm 18
 
803 gduche 19
	private $parametres = array();
20
	private $ressources = array();
21
	private $methode = null;
22
	private $projetNom = array();
23
	private $serviceNom = array();
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;
33
	}
34
 
35
	public function consulter($ressources, $parametres) {
36
		$this->methode = 'consulter';
37
		$resultat = '';
38
		$reponseHttp = new ReponseHttp();
39
		try {
40
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
41
			$this->conteneur = new Conteneur($this->parametres);
42
			$resultat = $this->traiterRessources();
43
			$reponseHttp->setResultatService($resultat);
44
		} catch (Exception $e) {
45
			$reponseHttp->ajouterErreur($e);
46
		}
47
		$reponseHttp->emettreLesEntetes();
48
		$corps = $reponseHttp->getCorps();
49
		return $corps;
50
	}
1666 jpm 51
 
1174 aurelien 52
	public function modifier($ressources, $parametres) {
53
		$this->methode = 'modifier';
54
		$resultat = '';
55
		$reponseHttp = new ReponseHttp();
56
		try {
57
			$this->initialiserRessourcesEtParametres($ressources, $parametres);
58
			$this->conteneur = new Conteneur($this->parametres);
59
			$resultat = $this->traiterRessources();
60
			$reponseHttp->setResultatService($resultat);
61
		} catch (Exception $e) {
62
			$reponseHttp->ajouterErreur($e);
63
		}
64
		$reponseHttp->emettreLesEntetes();
65
		$corps = $reponseHttp->getCorps();
66
		return $corps;
67
	}
1666 jpm 68
 
803 gduche 69
	private function initialiserRessourcesEtParametres($ressources, $parametres) {
70
		$this->ressources = $ressources;
71
		$this->parametres = $parametres;
72
	}
73
 
74
	private function traiterRessources() {
75
		$retour = '';
76
		$this->initialiserProjet();
77
		$retour = $this->initialiserService();
78
		return $retour;
79
	}
80
 
1666 jpm 81
 
803 gduche 82
	/*------------------------------------------------------------------------------------------------------------------
83
										CONFIGURATION DU PROJET
84
	------------------------------------------------------------------------------------------------------------------*/
85
	private function initialiserProjet() {
86
		$this->projetNom = 'utilisateurs';
87
		$this->chargerConfigProjet();
88
	}
89
 
90
	private function chargerConfigProjet() {
91
		$projet = $this->projetNom;
92
		$chemin = Config::get('chemin_configurations')."config_$projet.ini";
93
		Config::charger($chemin);
94
	}
95
 
96
	/*------------------------------------------------------------------------------------------------------------------
97
								CONFIGURATION DU SERVICE
98
	------------------------------------------------------------------------------------------------------------------*/
99
	private function initialiserService() {
1666 jpm 100
 
803 gduche 101
		$this->chargerNomService();
1666 jpm 102
 
803 gduche 103
		$classe = $this->obtenirNomClasseService($this->serviceNom);
104
		$chemins = array();
105
		$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
106
		$chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php';
107
		$retour = '';
108
		$service = null;
109
		foreach ($chemins as $chemin) {
110
			if (file_exists($chemin)) {
111
				$this->conteneur->chargerConfiguration('config_'.$this->projetNom.'.ini');
1666 jpm 112
 
803 gduche 113
				require_once $chemin;
114
				$service = new $classe($this->conteneur);
115
				if ($this->methode == 'consulter') {
116
					$retour = $service->consulter($this->ressources, $this->parametres);
117
				} elseif ($this->methode == 'ajouter') {
1666 jpm 118
					$retour = $service->ajouter($this->ressources, $this->parametres);
1174 aurelien 119
				} elseif ($this->methode == 'modifier') {
1666 jpm 120
					$retour = $service->modifier($this->ressources, $this->parametres);
803 gduche 121
				}
122
			}
123
		}
1666 jpm 124
 
803 gduche 125
		if (is_null($service)) {
126
			$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
127
			$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
128
			throw new Exception($message, $code);
129
		}
130
		return $retour;
131
	}
1666 jpm 132
 
803 gduche 133
	private function chargerNomService() {
134
		//S'il n'y a pas de ressources => envoyer sur identification anonyme
135
		if (!isset($this->ressources) || empty($this->ressources)) {
136
			$this->serviceNom = 'identification-anonyme';
137
		} else {
138
			//S'il y a 1 ressource et que celle ci est 'deconnecter', envoyer vers deconnecter
139
			if (sizeof($this->ressources) == 1 && ($this->ressources[0] == 'deconnecter')) {
140
				$this->serviceNom = 'deconnecter';
1174 aurelien 141
			} else if(sizeof($this->ressources) == 2 && $this->ressources[1] == 'preferences') {
142
				$this->serviceNom = 'preferences';
803 gduche 143
			} else if (sizeof($this->ressources) == 2) {
144
				$this->serviceNom = 'connecter';
145
			} else {
146
				$this->serviceNom = 'identification-anonyme';
147
			}
148
		}
149
	}
150
 
151
	private function obtenirNomClasseService($mot) {
152
		$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
153
		return $classeNom;
154
	}
155
}
156
?>