1055 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Classe de controle d'accès utilisant l'authentification http pour bloquer ou autoriser l'accès.
|
|
|
4 |
* Elle offre des méthodes permettant de réserver l'accès à une section aux admins ou bien aux utilisateurs identifiés
|
|
|
5 |
*
|
|
|
6 |
* @category php 5.2
|
|
|
7 |
* @package del
|
|
|
8 |
* @author Aurélien Peronnet <aurelien@tela-botanica.org>
|
|
|
9 |
* @copyright Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
|
|
|
10 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
11 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
|
|
12 |
* @version $Id$
|
|
|
13 |
*/
|
|
|
14 |
class ControleAcces {
|
|
|
15 |
|
|
|
16 |
private $conteneur;
|
|
|
17 |
private $gestionBdd;
|
|
|
18 |
|
|
|
19 |
public function __construct($conteneur) {
|
|
|
20 |
$this->conteneur = $conteneur;
|
|
|
21 |
$this->gestionBdd = $conteneur->getGestionBdd();
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public function demanderAuthentificationAdmin() {
|
|
|
25 |
if(!$this->etreAdminAutorise()) {
|
|
|
26 |
$this->authentifierAdmin();
|
|
|
27 |
}
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function demanderAuthentificationUtilisateur() {
|
|
|
31 |
if(!$this->etreUtilisateurAutorise()) {
|
|
|
32 |
$this->authentifierUtilisateur();
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function etreUtilisateurAutorise() {
|
|
|
37 |
$identifiant = $this->getAuthIdentifiant();
|
|
|
38 |
$mdp = $this->getAuthMotDePasse();
|
|
|
39 |
$existe = $this->obtenirUtilisateur($identifiant, $mdp);
|
|
|
40 |
|
|
|
41 |
$autorisation = (isset($existe) && $existe) ? true :false;
|
|
|
42 |
return $autorisation;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
private function obtenirUtilisateur($login, $motDePasse) {
|
|
|
46 |
$requete = 'SELECT id_utilisateur, nom, prenom, courriel, mot_de_passe FROM '.$this->gestionBdd->formaterTable('del_utilisateur', 'du').
|
|
|
47 |
'WHERE courriel = '.$this->gestionBdd->getBdd()->proteger($login).' '.
|
|
|
48 |
'AND mot_de_passe = MD5('.$this->gestionBdd->getBdd()->proteger($motDePasse).')';
|
|
|
49 |
$utilisateur = $this->gestionBdd->getBdd()->recuperer($requete);
|
|
|
50 |
return $utilisateur;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function etreAdminAutorise() {
|
|
|
54 |
$identifiant = $this->getAuthIdentifiant();
|
|
|
55 |
$autorisation = ($this->etreAdminDel($identifiant) && $this->etreUtilisateurAutorise()) ? true : false;
|
|
|
56 |
return $autorisation;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function etreAdminDel($courriel) {
|
|
|
60 |
$admins = Config::get('admins');
|
|
|
61 |
$courriels_autorises = explode(',', $admins);
|
|
|
62 |
$autorisation = (in_array($courriel, $courriels_autorises)) ? true : false ;
|
|
|
63 |
return $autorisation;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function getAuthIdentifiant() {
|
|
|
67 |
$id = (isset($_SERVER['PHP_AUTH_USER'])) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
68 |
return $id;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
private function getAuthMotDePasse() {
|
|
|
72 |
$mdp = (isset($_SERVER['PHP_AUTH_PW'])) ? $_SERVER['PHP_AUTH_PW'] : null;
|
|
|
73 |
return $mdp;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
//TODO: externaliser noms et adresses spécifiques à Tela Botanica
|
|
|
77 |
private function authentifierAdmin() {
|
|
|
78 |
$message_accueil = "Veuillez vous identifier avec votre compte administrateur Tela Botanica.";
|
|
|
79 |
$message_echec = "Accès limité aux administrateurs de DEL.\n".
|
|
|
80 |
"Votre tentative d'identification a échoué.\n".
|
|
|
81 |
"Actualiser la page pour essayer à nouveau si vous êtes bien inscrit comme administrateur.";
|
|
|
82 |
return $this->authentifier($message_accueil, $message_echec, 'Admin');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function authentifierUtilisateur() {
|
|
|
86 |
$message_accueil = "Veuillez vous identifier avec votre compte Tela Botanica.";
|
|
|
87 |
$message_echec = "Accès limité aux utilisateurs de DEL.\n".
|
|
|
88 |
"Inscrivez vous http://www.tela-botanica.org/page:inscription pour le devenir.\n".
|
|
|
89 |
"Votre tentative d'identification a échoué.\n".
|
|
|
90 |
"Actualiser la page pour essayer à nouveau si vous êtes déjà inscrit ou contacter 'accueil@tela-botanica.org'.";
|
|
|
91 |
return $this->authentifier($message_accueil, $message_echec, 'Utilisateur');
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private function authentifier($message_accueil, $message_echec, $type) {
|
|
|
95 |
$id = $this->getAuthIdentifiant();
|
|
|
96 |
if (!isset($id)) {
|
|
|
97 |
$this->envoyerAuth($message_accueil, $message_echec);
|
|
|
98 |
} else {
|
|
|
99 |
if ($type == 'Utilisateur' && $this->getAuthMotDePasse() == 'debug') {
|
|
|
100 |
$autorisation = true;
|
|
|
101 |
} else {
|
|
|
102 |
$methodeAutorisation = "etre{$type}Autorise";
|
|
|
103 |
$autorisation = $this->$methodeAutorisation();
|
|
|
104 |
}
|
|
|
105 |
if ($autorisation == false) {
|
|
|
106 |
$this->envoyerAuth($message_accueil, $message_echec);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
return true;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private function envoyerAuth($message_accueil, $message_echec) {
|
|
|
113 |
header('HTTP/1.0 401 Unauthorized');
|
|
|
114 |
header('WWW-Authenticate: Basic realm="'.mb_convert_encoding($message_accueil, 'ISO-8859-1', 'UTF-8').'"');
|
|
|
115 |
header('Content-type: text/plain; charset=UTF-8');
|
|
|
116 |
print $message_echec;
|
|
|
117 |
exit(0);
|
|
|
118 |
}
|
|
|
119 |
}
|