Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 139 | Rev 845 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 139 Rev 387
1
<?php
1
<?php
2
class Utilisateurs {
2
class Utilisateurs {
3
	const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
3
	const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
4
 
4
 
5
	private $courriels = array();
5
	private $courriels = array();
-
 
6
	private $identites = array();
6
	private $clientRest = null;
7
	private $clientRest = null;
7
 
8
 
8
	/**
9
	/**
9
	* Prend en paramêtre un tableau de courriels.
10
	* Prend en paramêtre un tableau de courriels.
10
	*
11
	*
11
	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
12
	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
12
	*/
13
	*/
13
	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
14
	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
14
		$this->courriels = $courriels;
15
		$this->courriels = $courriels;
15
		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
16
		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
16
	}
17
	}
17
 
18
 
18
	public function setCourriels($courriels) {
19
	public function setCourriels($courriels) {
19
		$this->courriels = $courriels;
20
		$this->courriels = $courriels;
20
	}
21
	}
-
 
22
	/**
-
 
23
	 * Lance l'interrogation du service de l'annuaire.
-
 
24
	 *
-
 
25
	 * @return null.
-
 
26
	 */
-
 
27
	public function chargerIdentites() {
-
 
28
		$this->identites = $this->getIdentites();
-
 
29
	}
21
 
30
 
22
	/**
31
	/**
23
	* Retourne, avoir interrogé un service de l'annuaire, les intitulés correspondant aux
32
	* Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
24
	* courriels des utilisateurs.
33
	* courriels des utilisateurs.
25
	*
34
	*
26
	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
35
	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
27
	* afficher, false en cas d'erreur ou de résultat vide.
36
	* afficher, false en cas d'erreur ou de résultat vide.
28
	*/
37
	*/
29
	public function getIntitules() {
38
	public function getIntitules() {
30
		$intitules = false;
39
		$intitules = false;
-
 
40
		$this->chargerIdentites();
31
		if ($identites = $this->getIdentites()) {
41
		if ($this->identites) {
32
			$intitules = array();
42
			$intitules = array();
33
			foreach ($identites as $courriel => $infos) {
43
			foreach ($this->identites as $courriel => $infos) {
34
				$intitules[$courriel] = $infos['intitule'];
44
				$intitules[$courriel] = $infos['intitule'];
35
			}
45
			}
36
		}
46
		}
37
		return $intitules;
47
		return $intitules;
38
	}
48
	}
-
 
49
 
-
 
50
	/**
-
 
51
	 * Retourne un intitulé en fonction d'un courriel.
-
 
52
	 *
-
 
53
	 * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
-
 
54
	 */
-
 
55
	public function getIntitule($courriel) {
-
 
56
		$intitule = '';
-
 
57
		if ($this->contenirCourriel($courriel)) {
-
 
58
			$intitule = $this->identites[$courriel]['intitule'];
-
 
59
		}
-
 
60
		return $intitule;
-
 
61
	}
-
 
62
 
-
 
63
	/**
-
 
64
	 * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
-
 
65
	 *
-
 
66
	 * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
-
 
67
	 */
-
 
68
	public function getId($courriel) {
-
 
69
		$id = '';
-
 
70
		if ($this->contenirCourriel($courriel)) {
-
 
71
			$id = $this->identites[$courriel]['id'];
-
 
72
		}
-
 
73
		return $id;
-
 
74
	}
-
 
75
 
-
 
76
	private function contenirCourriel($courriel) {
-
 
77
		$ok = ($this->identites && isset($this->identites[$courriel])) ? true : false;
-
 
78
		return $ok;
-
 
79
	}
39
 
80
 
40
	private function getIdentites() {
81
	private function getIdentites() {
41
		$utilisateursInfos = $this->consulterServiceAnnuaire();
82
		$utilisateursInfos = $this->consulterServiceAnnuaire();
42
		$identites = $this->extraireIdentites($utilisateursInfos);
83
		$identites = $this->extraireIdentites($utilisateursInfos);
43
		return $identites;
84
		return $identites;
44
	}
85
	}
45
 
86
 
46
	private function consulterServiceAnnuaire() {
87
	private function consulterServiceAnnuaire() {
47
		$url = sprintf(self::TPL_URL_WS_ANNUAIRE, implode(',', $this->courriels));
88
		$url = sprintf(self::TPL_URL_WS_ANNUAIRE, implode(',', $this->courriels));
48
		$json = $this->clientRest->consulter($url);
89
		$json = $this->clientRest->consulter($url);
49
		$utilisateurs = json_decode($json, true);
90
		$utilisateurs = json_decode($json, true);
50
		return $utilisateurs;
91
		return $utilisateurs;
51
	}
92
	}
52
 
93
 
53
	private function extraireIdentites($utilisateursInfos) {
94
	private function extraireIdentites($utilisateursInfos) {
54
		$identites = array();
95
		$identites = array();
55
		foreach ($this->courriels as $courriel) {
96
		foreach ($this->courriels as $courriel) {
56
			$info = array('id' => null, 'intitule' => '');
97
			$info = array('id' => null, 'intitule' => '');
57
			if (isset($utilisateursInfos[$courriel])) {
98
			if (isset($utilisateursInfos[$courriel])) {
58
				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
99
				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
59
				$info['id'] = $utilisateursInfos[$courriel]['id'];
100
				$info['id'] = $utilisateursInfos[$courriel]['id'];
60
			} else {
101
			} else {
61
				$info['intitule'] = $this->tronquerCourriel($courriel);
102
				$info['intitule'] = $this->tronquerCourriel($courriel);
62
			}
103
			}
63
			$identites[$courriel] = $info;
104
			$identites[$courriel] = $info;
64
		}
105
		}
65
		return $identites;
106
		return $identites;
66
	}
107
	}
67
 
108
 
68
	private function tronquerCourriel($courriel) {
109
	private function tronquerCourriel($courriel) {
69
		$courriel = str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
110
		$courriel = str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
70
		return $courriel;
111
		return $courriel;
71
	}
112
	}
72
}
113
}
73
?>
114
?>