2016-07-04 09:04:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager;
|
|
|
|
|
|
|
|
use \manager\Repo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Authentification{
|
|
|
|
|
|
|
|
// Constantes
|
|
|
|
public static $config_path = 'f/json/modules/auth';
|
|
|
|
|
|
|
|
// Contiendra les erreurs
|
|
|
|
public $error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* INITIALISATION DU SYSTEME ET MISE A JOUR CONSTANTES D'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2016-07-04 15:23:19 +00:00
|
|
|
public static function check(){
|
2016-07-04 09:04:49 +00:00
|
|
|
/* (1) Initialisation
|
|
|
|
---------------------------------------------------------*/
|
2016-07-04 13:45:29 +00:00
|
|
|
if( !isset($_SESSION['AUTH']) ) $_SESSION['AUTH'] = [];
|
|
|
|
if( !isset($_SESSION['PERM']) ) $_SESSION['PERM'] = [];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) Gestion de AUTH (authentification)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si Auth dans HEADER, on le récupère */
|
|
|
|
$AUTH = isset($_SERVER['PHP_AUTH_DIGEST']) ? $_SERVER['PHP_AUTH_DIGEST'] : '';
|
|
|
|
|
|
|
|
/* (2) Si Auth dans SESSION, on le récupère */
|
|
|
|
if( $AUTH == '' && isset($_SESSION['AUTH']) )
|
|
|
|
$AUTH = implode('', $_SESSION['AUTH']);
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Gestion de AUTH en fonction des tokens
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Double authentification */
|
|
|
|
if( preg_match('/^([a-f0-9]{40})([a-f0-9]{40})$/', $AUTH, $match) )
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [ $match[1], $match[2] ];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* (2) Authentification unique */
|
|
|
|
else if( preg_match('/^[a-f0-9]{40}$/', $AUTH, $match) )
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [ $match[0] ];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* (3) Aucune authentification */
|
|
|
|
else
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
|
2016-07-04 09:29:48 +00:00
|
|
|
/* (4) On vérifie l'authentification par BDD
|
2016-07-04 09:04:49 +00:00
|
|
|
---------------------------------------------------------*/
|
2016-07-04 15:23:19 +00:00
|
|
|
if( !self::deepCheck() )
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [];
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2016-07-04 15:23:19 +00:00
|
|
|
public static function deepCheck(){
|
2016-07-04 09:04:49 +00:00
|
|
|
/* [1] Si aucune authentification
|
|
|
|
=========================================================*/
|
2016-07-04 15:23:19 +00:00
|
|
|
if( self::auth() == 0 )
|
2016-07-04 13:45:29 +00:00
|
|
|
return false;
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* [2] Si authentification unique
|
|
|
|
=========================================================*/
|
2016-07-04 15:23:19 +00:00
|
|
|
if( self::auth() >= 1 ){
|
2016-07-04 13:45:29 +00:00
|
|
|
$checkRoot = new Repo('warehouse/getByToken', [ $_SESSION['AUTH'][0] ]);
|
2016-07-04 09:29:48 +00:00
|
|
|
|
|
|
|
// Si le token n'existe pas
|
2016-07-04 13:45:29 +00:00
|
|
|
if( count($checkRoot->answer()) < 1 )
|
2016-07-04 09:29:48 +00:00
|
|
|
return false;
|
2016-07-05 13:21:32 +00:00
|
|
|
|
|
|
|
// On met à jour les informations
|
|
|
|
$_SESSION['WAREHOUSE'] = [
|
2016-07-06 09:10:21 +00:00
|
|
|
'id' => (int) $checkRoot->answer()[0]['id_warehouse'],
|
|
|
|
'name' => $checkRoot->answer()[0]['name'],
|
|
|
|
'modules' => ['upload', 'method']
|
2016-07-05 13:21:32 +00:00
|
|
|
];
|
2016-07-04 09:29:48 +00:00
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* [3] Si authentification double
|
|
|
|
=========================================================*/
|
2016-07-04 15:23:19 +00:00
|
|
|
if( self::auth() >= 2 ){
|
2016-07-04 13:45:29 +00:00
|
|
|
$checkBranch = new Repo('admin/getByToken', [ $_SESSION['AUTH'][1] ]);
|
2016-07-04 09:29:48 +00:00
|
|
|
|
|
|
|
// Si le token n'existe pas
|
2016-07-04 13:45:29 +00:00
|
|
|
if( count($checkBranch->answer()) < 1 )
|
2016-07-04 09:29:48 +00:00
|
|
|
return false;
|
2016-07-05 13:21:32 +00:00
|
|
|
|
2016-07-05 13:34:31 +00:00
|
|
|
// Si pas administrateur de l'entrepot
|
|
|
|
if( $checkBranch->answer()[0]['id_warehouse'] != $_SESSION['WAREHOUSE']['id'] )
|
|
|
|
return false;
|
|
|
|
|
2016-07-05 13:21:32 +00:00
|
|
|
// On met à jour les informations
|
|
|
|
$_SESSION['ADMIN'] = [
|
2016-07-05 13:50:24 +00:00
|
|
|
'id' => (int) $checkBranch->answer()[0]['id_admin'],
|
2016-07-05 13:21:32 +00:00
|
|
|
'username' => $checkBranch->answer()[0]['username'],
|
|
|
|
'mail' => $checkBranch->answer()[0]['mail']
|
|
|
|
];
|
2016-07-04 09:29:48 +00:00
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2016-07-04 10:13:35 +00:00
|
|
|
/* [4] Si pas d'erreur d'authentification, on retourne TRUE
|
2016-07-04 09:29:48 +00:00
|
|
|
=========================================================*/
|
|
|
|
return true;
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-04 15:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DES ACCES EN FONCTION DE PERMISSIONS ATTENDUES
|
|
|
|
*
|
|
|
|
* @expected<array> Liste des permissions attendues
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Si FALSE, pas la permission, sinon si
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function permission($expected){
|
|
|
|
/* [0] Mise à jour de l'authentification
|
|
|
|
=========================================================*/
|
|
|
|
self::check();
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Gestion de l'AUTH (authentification)
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
/* (1) Si entrepot requis, mais manquant
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( in_array('warehouse', $expected) && self::auth() < 1 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Si admin requis, mais manquant
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( in_array('admin', $expected) && self::auth() < 2 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) On retire 'warehouse' et 'admin' de @expected
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$warehouseIndex = array_search('warehouse', $expected);
|
|
|
|
$adminIndex = array_search('admin', $expected);
|
|
|
|
if( is_int($warehouseIndex) ) unset($expected[$warehouseIndex]);
|
|
|
|
if( is_int($adminIndex) ) unset($expected[$adminIndex]);
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Gestion des permissions
|
|
|
|
=========================================================*/
|
|
|
|
foreach($expected as $permission)
|
|
|
|
// Si il manque au minimum une permission, on retourne FALSE
|
|
|
|
if( !in_array($permission, $_SESSION['PERM']) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Si on a toutes les permissions requises
|
|
|
|
=========================================================*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LE NIVEAU D'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
* @return auth<int> Niveau d'authentification (0 à 2)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function auth(){
|
|
|
|
return !is_array($_SESSION['AUTH']) ? 0 : count($_SESSION['AUTH']);
|
|
|
|
}
|
|
|
|
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|