1 |
<?php
|
1 |
<?php
|
- |
|
2 |
// declare(encoding='UTF-8');
|
2 |
/**
|
3 |
/**
|
3 |
* Classe de controle d'accès aux services de DEL.
|
4 |
* Classe de controle d'accès aux services de DEL.
|
4 |
*
|
5 |
*
|
5 |
* Cette classe propose des méthodes permettant :
|
6 |
* Cette classe propose des méthodes permettant :
|
6 |
* - l'authentification http pour bloquer ou autoriser l'accès
|
7 |
* - l'authentification http pour bloquer ou autoriser l'accès
|
7 |
* - de déterminer les droits des utilisateurs
|
8 |
* - de déterminer les droits des utilisateurs
|
8 |
*
|
9 |
*
|
9 |
* @category DEL
|
10 |
* @category DEL
|
- |
|
11 |
* @package Services
|
10 |
* @package Bibliotheque
|
12 |
* @package Bibliotheque
|
11 |
* @version 0.1
|
13 |
* @version 0.1
|
12 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
14 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
13 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
15 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
14 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
16 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
15 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
17 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
16 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
18 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
17 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
19 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
18 |
*/
|
20 |
*/
|
19 |
class ControleAcces {
|
21 |
class ControleAcces {
|
20 |
|
22 |
|
21 |
private $conteneur;
|
23 |
private $conteneur;
|
22 |
private $bdd;
|
24 |
private $bdd;
|
23 |
|
25 |
|
24 |
public function __construct($conteneur) {
|
26 |
public function __construct($conteneur) {
|
25 |
$this->conteneur = $conteneur;
|
27 |
$this->conteneur = $conteneur;
|
26 |
$this->bdd = $this->conteneur->getBdd();
|
28 |
$this->bdd = $this->conteneur->getBdd();
|
27 |
}
|
29 |
}
|
28 |
|
30 |
|
29 |
public function controlerIpAutorisees() {
|
31 |
public function controlerIpAutorisees() {
|
30 |
$ipsAutorisees = $this->conteneur->getParametreTableau('ip_autorisees');
|
32 |
$ipsAutorisees = $this->conteneur->getParametreTableau('ip_autorisees');
|
31 |
|
33 |
|
32 |
$remoteIp = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
|
34 |
$remoteIp = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
|
33 |
$serverIp = filter_input(INPUT_SERVER, 'SERVER_ADDR', FILTER_VALIDATE_IP);
|
35 |
$serverIp = filter_input(INPUT_SERVER, 'SERVER_ADDR', FILTER_VALIDATE_IP);
|
34 |
if (in_array($remoteIp, $ipsAutorisees) == false) {
|
36 |
if (in_array($remoteIp, $ipsAutorisees) == false) {
|
35 |
if ($remoteIp != $serverIp) {// ATTENTION : maintenir ce test à l'intérieur du précédent
|
37 |
if ($remoteIp != $serverIp) {// ATTENTION : maintenir ce test à l'intérieur du précédent
|
36 |
$message = "Accès interdit. \n".
|
38 |
$message = "Accès interdit. \n".
|
37 |
"Vous n'êtes pas autorisé à accéder à ce service depuis '$remoteIp' !\n";
|
39 |
"Vous n'êtes pas autorisé à accéder à ce service depuis '$remoteIp' !\n";
|
38 |
$code = RestServeur::HTTP_CODE_ACCES_NON_AUTORISE;
|
40 |
$code = RestServeur::HTTP_CODE_ACCES_NON_AUTORISE;
|
39 |
throw new Exception($message, $code);
|
41 |
throw new Exception($message, $code);
|
40 |
}
|
42 |
}
|
41 |
}
|
43 |
}
|
42 |
return true;
|
44 |
return true;
|
43 |
}
|
45 |
}
|
44 |
|
46 |
|
45 |
public function demanderAuthentificationAdmin() {
|
47 |
public function demanderAuthentificationAdmin() {
|
46 |
if (!$this->etreAdminAutoriseParHttp()) {
|
48 |
if (!$this->etreAdminAutoriseParHttp()) {
|
47 |
$this->authentifierAdmin();
|
49 |
$this->authentifierAdmin();
|
48 |
}
|
50 |
}
|
49 |
}
|
51 |
}
|
50 |
|
52 |
|
51 |
public function demanderAuthentificationUtilisateur() {
|
53 |
public function demanderAuthentificationUtilisateur() {
|
52 |
if (!$this->etreUtilisateurAutoriseParHttp()) {
|
54 |
if (!$this->etreUtilisateurAutoriseParHttp()) {
|
53 |
$this->authentifierUtilisateur();
|
55 |
$this->authentifierUtilisateur();
|
54 |
}
|
56 |
}
|
55 |
}
|
57 |
}
|
56 |
|
58 |
|
57 |
private function etreUtilisateurAutoriseParHttp() {
|
59 |
private function etreUtilisateurAutoriseParHttp() {
|
58 |
$identifiant = $this->getAuthIdentifiant();
|
60 |
$identifiant = $this->getAuthIdentifiant();
|
59 |
$mdp = $this->getAuthMotDePasse();
|
61 |
$mdp = $this->getAuthMotDePasse();
|
60 |
$existe = $this->obtenirUtilisateur($identifiant, $mdp);
|
62 |
$existe = $this->obtenirUtilisateur($identifiant, $mdp);
|
61 |
|
63 |
|
62 |
$autorisation = (isset($existe) && $existe) ? true :false;
|
64 |
$autorisation = (isset($existe) && $existe) ? true :false;
|
63 |
return $autorisation;
|
65 |
return $autorisation;
|
64 |
}
|
66 |
}
|
65 |
|
67 |
|
66 |
private function obtenirUtilisateur($login, $motDePasse) {
|
68 |
private function obtenirUtilisateur($login, $motDePasse) {
|
67 |
$login = $this->bdd->proteger($login);
|
69 |
$login = $this->bdd->proteger($login);
|
68 |
$motDePasse = $this->bdd->proteger($motDePasse);
|
70 |
$motDePasse = $this->bdd->proteger($motDePasse);
|
69 |
$requete = 'SELECT id_utilisateur, nom, prenom, courriel, mot_de_passe '.
|
71 |
$requete = 'SELECT id_utilisateur, nom, prenom, courriel, mot_de_passe '.
|
70 |
'FROM del_utilisateur AS du '.
|
72 |
'FROM del_utilisateur AS du '.
|
71 |
"WHERE courriel = $login ".
|
73 |
"WHERE courriel = $login ".
|
72 |
" AND mot_de_passe = MD5($motDePasse) ".
|
74 |
" AND mot_de_passe = MD5($motDePasse) ".
|
73 |
' -- '.__FILE__.':'.__LINE__."\n";
|
75 |
' -- '.__FILE__.':'.__LINE__."\n";
|
74 |
$utilisateur = $this->bdd->recuperer($requete);
|
76 |
$utilisateur = $this->bdd->recuperer($requete);
|
75 |
return $utilisateur;
|
77 |
return $utilisateur;
|
76 |
}
|
78 |
}
|
77 |
|
79 |
|
78 |
private function etreAdminAutoriseParHttp() {
|
80 |
private function etreAdminAutoriseParHttp() {
|
79 |
$identifiant = $this->getAuthIdentifiant();
|
81 |
$identifiant = $this->getAuthIdentifiant();
|
80 |
$autorisation = ($this->etreAdmin($identifiant) && $this->etreUtilisateurAutorise()) ? true : false;
|
82 |
$autorisation = ($this->etreAdmin($identifiant) && $this->etreUtilisateurAutorise()) ? true : false;
|
81 |
return $autorisation;
|
83 |
return $autorisation;
|
82 |
}
|
84 |
}
|
83 |
|
85 |
|
84 |
private function etreAdmin($courriel) {
|
86 |
private function etreAdmin($courriel) {
|
85 |
$courriel = $this->bdd->proteger($courriel);
|
87 |
$courriel = $this->bdd->proteger($courriel);
|
86 |
$requete = 'SELECT dui.admin '.
|
88 |
$requete = 'SELECT dui.admin '.
|
87 |
'FROM del_utilisateur AS du LEFT JOIN del_user_infos AS dui ON (du.id_utilisateur = dui.id_utilisateur) '.
|
89 |
'FROM del_utilisateur AS du LEFT JOIN del_user_infos AS dui ON (du.id_utilisateur = dui.id_utilisateur) '.
|
88 |
"WHERE du.courriel = $courriel ".
|
90 |
"WHERE du.courriel = $courriel ".
|
89 |
' -- '.__FILE__.':'.__LINE__."\n";
|
91 |
' -- '.__FILE__.':'.__LINE__."\n";
|
90 |
$infoUtilisateur = $this->bdd->recuperer($requete);
|
92 |
$infoUtilisateur = $this->bdd->recuperer($requete);
|
91 |
|
93 |
|
92 |
$etreAdmin = $this->verifierDroitAdmin($infoUtilisateur['admin']);
|
94 |
$etreAdmin = $this->verifierDroitAdmin($infoUtilisateur['admin']);
|
93 |
return $etreAdmin;
|
95 |
return $etreAdmin;
|
94 |
}
|
96 |
}
|
95 |
|
97 |
|
96 |
private function verifierDroitAdmin($droit) {
|
98 |
private function verifierDroitAdmin($droit) {
|
97 |
$droitAdmin = $this->conteneur->getParametre('droit_superadmin');
|
99 |
$droitAdmin = $this->conteneur->getParametre('droit_superadmin');
|
98 |
$etreAdmin = false;
|
100 |
$etreAdmin = false;
|
99 |
if (isset($droit) && $droit == $droitAdmin) {
|
101 |
if (isset($droit) && $droit == $droitAdmin) {
|
100 |
$etreAdmin = true;
|
102 |
$etreAdmin = true;
|
101 |
}
|
103 |
}
|
102 |
return $etreAdmin;
|
104 |
return $etreAdmin;
|
103 |
}
|
105 |
}
|
104 |
|
106 |
|
105 |
private function getAuthIdentifiant() {
|
107 |
private function getAuthIdentifiant() {
|
106 |
$id = (isset($_SERVER['PHP_AUTH_USER'])) ? $_SERVER['PHP_AUTH_USER'] : null;
|
108 |
$id = (isset($_SERVER['PHP_AUTH_USER'])) ? $_SERVER['PHP_AUTH_USER'] : null;
|
107 |
return $id;
|
109 |
return $id;
|
108 |
}
|
110 |
}
|
109 |
|
111 |
|
110 |
private function getAuthMotDePasse() {
|
112 |
private function getAuthMotDePasse() {
|
111 |
$mdp = (isset($_SERVER['PHP_AUTH_PW'])) ? $_SERVER['PHP_AUTH_PW'] : null;
|
113 |
$mdp = (isset($_SERVER['PHP_AUTH_PW'])) ? $_SERVER['PHP_AUTH_PW'] : null;
|
112 |
return $mdp;
|
114 |
return $mdp;
|
113 |
}
|
115 |
}
|
114 |
|
116 |
|
115 |
//TODO: externaliser noms et adresses spécifiques à Tela Botanica
|
117 |
//TODO: externaliser noms et adresses spécifiques à Tela Botanica
|
116 |
private function authentifierAdmin() {
|
118 |
private function authentifierAdmin() {
|
117 |
$message_accueil = "Veuillez vous identifier avec votre compte administrateur Tela Botanica.";
|
119 |
$message_accueil = "Veuillez vous identifier avec votre compte administrateur Tela Botanica.";
|
118 |
$message_echec = "Accès limité aux administrateurs de DEL.\n".
|
120 |
$message_echec = "Accès limité aux administrateurs de DEL.\n".
|
119 |
"Votre tentative d'identification a échoué.\n".
|
121 |
"Votre tentative d'identification a échoué.\n".
|
120 |
"Actualiser la page pour essayer à nouveau si vous êtes bien inscrit comme administrateur.";
|
122 |
"Actualiser la page pour essayer à nouveau si vous êtes bien inscrit comme administrateur.";
|
121 |
return $this->authentifier($message_accueil, $message_echec, 'Admin');
|
123 |
return $this->authentifier($message_accueil, $message_echec, 'Admin');
|
122 |
}
|
124 |
}
|
123 |
|
125 |
|
124 |
private function authentifierUtilisateur() {
|
126 |
private function authentifierUtilisateur() {
|
125 |
$message_accueil = "Veuillez vous identifier avec votre compte Tela Botanica.";
|
127 |
$message_accueil = "Veuillez vous identifier avec votre compte Tela Botanica.";
|
126 |
$message_echec = "Accès limité aux utilisateurs de DEL.\n".
|
128 |
$message_echec = "Accès limité aux utilisateurs de DEL.\n".
|
127 |
"Inscrivez vous http://www.tela-botanica.org/page:inscription pour le devenir.\n".
|
129 |
"Inscrivez vous http://www.tela-botanica.org/page:inscription pour le devenir.\n".
|
128 |
"Votre tentative d'identification a échoué.\n".
|
130 |
"Votre tentative d'identification a échoué.\n".
|
129 |
"Actualiser la page pour essayer à nouveau si vous êtes déjà inscrit ou contacter 'accueil@tela-botanica.org'.";
|
131 |
"Actualiser la page pour essayer à nouveau si vous êtes déjà inscrit ou contacter 'accueil@tela-botanica.org'.";
|
130 |
return $this->authentifier($message_accueil, $message_echec, 'Utilisateur');
|
132 |
return $this->authentifier($message_accueil, $message_echec, 'Utilisateur');
|
131 |
}
|
133 |
}
|
132 |
|
134 |
|
133 |
private function authentifier($message_accueil, $message_echec, $type) {
|
135 |
private function authentifier($message_accueil, $message_echec, $type) {
|
134 |
$id = $this->getAuthIdentifiant();
|
136 |
$id = $this->getAuthIdentifiant();
|
135 |
if (!isset($id)) {
|
137 |
if (!isset($id)) {
|
136 |
$this->envoyerAuth($message_accueil, $message_echec);
|
138 |
$this->envoyerAuth($message_accueil, $message_echec);
|
137 |
} else {
|
139 |
} else {
|
138 |
if ($type == 'Utilisateur' && $this->getAuthMotDePasse() == 'debug') {
|
140 |
if ($type == 'Utilisateur' && $this->getAuthMotDePasse() == 'debug') {
|
139 |
$autorisation = true;
|
141 |
$autorisation = true;
|
140 |
} else {
|
142 |
} else {
|
141 |
$methodeAutorisation = "etre{$type}Autorise";
|
143 |
$methodeAutorisation = "etre{$type}Autorise";
|
142 |
$autorisation = $this->$methodeAutorisation();
|
144 |
$autorisation = $this->$methodeAutorisation();
|
143 |
}
|
145 |
}
|
144 |
if ($autorisation == false) {
|
146 |
if ($autorisation == false) {
|
145 |
$this->envoyerAuth($message_accueil, $message_echec);
|
147 |
$this->envoyerAuth($message_accueil, $message_echec);
|
146 |
}
|
148 |
}
|
147 |
}
|
149 |
}
|
148 |
return true;
|
150 |
return true;
|
149 |
}
|
151 |
}
|
150 |
|
152 |
|
151 |
public function etreUtilisateurAvecDroitAdmin() {
|
153 |
public function etreUtilisateurAvecDroitAdmin() {
|
152 |
$infos = $this->getInfosUtilisateurConnecte();
|
154 |
$infos = $this->getInfosUtilisateurConnecte();
|
153 |
|
155 |
|
154 |
$etreAdmin = false;
|
156 |
$etreAdmin = false;
|
155 |
if (isset($infos['admin'])) {
|
157 |
if (isset($infos['admin'])) {
|
156 |
$etreAdmin = $this->verifierDroitAdmin($infos['admin']);
|
158 |
$etreAdmin = $this->verifierDroitAdmin($infos['admin']);
|
157 |
}
|
159 |
}
|
158 |
|
160 |
|
159 |
if ($etreAdmin == false) {
|
161 |
if ($etreAdmin == false) {
|
160 |
$message = "Vous ne pouvez pas accéder à ce service car vous n'avez pas les droits d'administrateur !\n";
|
162 |
$message = "Vous ne pouvez pas accéder à ce service car vous n'avez pas les droits d'administrateur !\n";
|
161 |
$code = RestServeur::HTTP_CODE_ACCES_NON_AUTORISE;
|
163 |
$code = RestServeur::HTTP_CODE_ACCES_NON_AUTORISE;
|
162 |
throw new Exception($message, $code);
|
164 |
throw new Exception($message, $code);
|
163 |
}
|
165 |
}
|
164 |
return $etreAdmin;
|
166 |
return $etreAdmin;
|
165 |
}
|
167 |
}
|
166 |
|
168 |
|
167 |
public function getInfosUtilisateurConnecte() {
|
169 |
public function getInfosUtilisateurConnecte() {
|
168 |
$utilisateur = array();
|
170 |
$utilisateur = array();
|
169 |
if (isset($_COOKIE['del_courriel'])) {
|
171 |
if (isset($_COOKIE['del_courriel'])) {
|
170 |
$courriel = $_COOKIE['del_courriel'];
|
172 |
$courriel = $_COOKIE['del_courriel'];
|
171 |
$motDePasse = $_COOKIE['del_mot_de_passe'];
|
173 |
$motDePasse = $_COOKIE['del_mot_de_passe'];
|
172 |
$utilisateur = $this->obtenirUtilisateurSansEncryptionMdp($courriel, $motDePasse);
|
174 |
$utilisateur = $this->obtenirUtilisateurSansEncryptionMdp($courriel, $motDePasse);
|
173 |
}
|
175 |
}
|
174 |
return $utilisateur;
|
176 |
return $utilisateur;
|
175 |
}
|
177 |
}
|
176 |
|
178 |
|
177 |
private function obtenirUtilisateurSansEncryptionMdp($login, $motDePasseEncrypte) {
|
179 |
private function obtenirUtilisateurSansEncryptionMdp($login, $motDePasseEncrypte) {
|
178 |
$login = $this->bdd->proteger($login);
|
180 |
$login = $this->bdd->proteger($login);
|
179 |
$motDePasseEncrypte = $this->bdd->proteger($motDePasseEncrypte);
|
181 |
$motDePasseEncrypte = $this->bdd->proteger($motDePasseEncrypte);
|
180 |
$requete = 'SELECT du.*, admin, preferences, date_premiere_utilisation '.
|
182 |
$requete = 'SELECT du.*, admin, preferences, date_premiere_utilisation '.
|
181 |
'FROM del_utilisateur AS du '.
|
183 |
'FROM del_utilisateur AS du '.
|
182 |
' LEFT JOIN del_utilisateur_infos AS dui ON (du.id_utilisateur = dui.id_utilisateur) '.
|
184 |
' LEFT JOIN del_utilisateur_infos AS dui ON (du.id_utilisateur = dui.id_utilisateur) '.
|
183 |
"WHERE du.courriel = $login ".
|
185 |
"WHERE du.courriel = $login ".
|
184 |
" AND du.mot_de_passe = $motDePasseEncrypte ".
|
186 |
" AND du.mot_de_passe = $motDePasseEncrypte ".
|
185 |
' -- '.__FILE__.':'.__LINE__."\n";
|
187 |
' -- '.__FILE__.':'.__LINE__."\n";
|
186 |
$utilisateur = $this->bdd->recuperer($requete);
|
188 |
$utilisateur = $this->bdd->recuperer($requete);
|
187 |
return $utilisateur;
|
189 |
return $utilisateur;
|
188 |
}
|
190 |
}
|
189 |
|
191 |
|
190 |
public function getIdAnonymeTemporaire() {
|
192 |
public function getIdAnonymeTemporaire() {
|
191 |
$this->demarrerSession();
|
193 |
$this->demarrerSession();
|
192 |
return session_id();
|
194 |
return session_id();
|
193 |
}
|
195 |
}
|
194 |
|
196 |
|
195 |
private function demarrerSession() {
|
197 |
private function demarrerSession() {
|
196 |
if (session_id() == '') {
|
198 |
if (session_id() == '') {
|
197 |
// TODO : modifier ce test lors du passage en php 5.4
|
199 |
// TODO : modifier ce test lors du passage en php 5.4
|
198 |
session_start();
|
200 |
session_start();
|
199 |
}
|
201 |
}
|
200 |
}
|
202 |
}
|
201 |
}
|
203 |
}
|