Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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