281 |
aurelien |
1 |
<?php
|
|
|
2 |
//+----------------------------------------------------------------------------------------------------------------+
|
|
|
3 |
// GESTION DE L'IDENTIFICATION
|
|
|
4 |
Class ControleUtilisateur extends JRestService {
|
|
|
5 |
|
285 |
aurelien |
6 |
private function envoyerMessageNonAutorise() {
|
|
|
7 |
header('HTTP/1.0 401 Unauthorized');
|
|
|
8 |
echo 'Accès interdit';
|
|
|
9 |
exit(0);
|
|
|
10 |
}
|
|
|
11 |
|
281 |
aurelien |
12 |
private function envoyerAuth($message_accueil, $message_echec) {
|
|
|
13 |
header('HTTP/1.0 401 Unauthorized');
|
282 |
aurelien |
14 |
header('WWW-Authenticate: Basic realm="www.obs-saisons.fr"');
|
281 |
aurelien |
15 |
header('Content-type: text/plain; charset=UTF-8');
|
|
|
16 |
print $message_echec;
|
|
|
17 |
exit(0);
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
protected function getAuthIdentifiant() {
|
|
|
21 |
$id = (isset($_SERVER['PHP_AUTH_USER'])) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
22 |
return $id;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
protected function getAuthMotDePasse() {
|
|
|
26 |
$mdp = (isset($_SERVER['PHP_AUTH_PW'])) ? $_SERVER['PHP_AUTH_PW'] : null;
|
|
|
27 |
return $mdp;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function authentifierAdmin() {
|
|
|
31 |
$message_accueil = "Veuillez vous identifier avec votre compte Observatoire des saisons.";
|
|
|
32 |
$message_echec = "Accès limité aux administrateurs de l'ods.\n".
|
|
|
33 |
"Votre tentative d'identification a échoué.\n".
|
|
|
34 |
"Actualiser la page pour essayer à nouveau si vous êtes bien inscrit comme administrateur.";
|
|
|
35 |
return $this->authentifier($message_accueil, $message_echec, 'Admin');
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private function authentifier($message_accueil, $message_echec, $type) {
|
|
|
39 |
$id = $this->getAuthIdentifiant();
|
|
|
40 |
if (!isset($id)) {
|
|
|
41 |
$this->envoyerAuth($message_accueil, $message_echec);
|
|
|
42 |
} else {
|
|
|
43 |
$methodeAutorisation = "etre{$type}Autorise";
|
|
|
44 |
$autorisation = $this->$methodeAutorisation();
|
|
|
45 |
|
|
|
46 |
if ($autorisation == false) {
|
|
|
47 |
$this->envoyerAuth($message_accueil, $message_echec);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
return true;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function etreUtilisateurAutorise() {
|
|
|
54 |
$identifiant = $this->getAuthIdentifiant();
|
|
|
55 |
$mdp = md5($this->getAuthMotDePasse());
|
|
|
56 |
|
285 |
aurelien |
57 |
$requete = 'SELECT COUNT(*) as existe FROM drupal_users du '.
|
281 |
aurelien |
58 |
'WHERE name = '.$this->proteger($identifiant).' '.
|
|
|
59 |
'AND pass = '.$this->proteger($mdp);
|
|
|
60 |
|
|
|
61 |
$existe = $this->executerRequete($requete);
|
|
|
62 |
|
|
|
63 |
$autorisation = (!empty($existe) && $existe['existe'] > 0) ? true :false;
|
|
|
64 |
return $autorisation;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function etreAdminAutorise() {
|
|
|
68 |
$identifiant = $this->getAuthIdentifiant();
|
|
|
69 |
$mdp = md5($this->getAuthMotDePasse());
|
|
|
70 |
|
282 |
aurelien |
71 |
$requete = 'SELECT COUNT(*) as existe FROM drupal_users du '.
|
|
|
72 |
'INNER JOIN drupal_users_roles dur '.
|
281 |
aurelien |
73 |
'ON du.uid = dur.uid '.
|
|
|
74 |
'WHERE name = '.$this->proteger($identifiant).' '.
|
|
|
75 |
'AND pass = '.$this->proteger($mdp).' '.
|
|
|
76 |
'AND rid = 3 ';
|
|
|
77 |
$existe = $this->executerRequete($requete);
|
|
|
78 |
|
|
|
79 |
$autorisation = (!empty($existe) && $existe[0]['existe'] > 0) ? true :false;
|
|
|
80 |
return $autorisation;
|
|
|
81 |
}
|
285 |
aurelien |
82 |
|
|
|
83 |
public function controleAppelIpAutorisee() {
|
|
|
84 |
$ips_autorisees = explode(',', $this->config['jrest_admin']['ip_autorisees']);
|
|
|
85 |
$ip_appelante = $_SERVER['REMOTE_ADDR'];
|
|
|
86 |
if(!in_array($ip_appelante, $ips_autorisees) && $ip_appelante != $SERVER['SERVER_ADDR']) {
|
|
|
87 |
$this->envoyerMessageNonAutorise();
|
|
|
88 |
}
|
|
|
89 |
return true;
|
|
|
90 |
}
|
281 |
aurelien |
91 |
}
|
|
|
92 |
?>
|