2016-07-04 09:04:49 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace api\core;
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
use \database\core\Repo;
|
2016-11-08 08:34:02 +00:00
|
|
|
use \error\core\Error;
|
2017-01-30 17:39:21 +00:00
|
|
|
use \error\core\Err;
|
|
|
|
use \api\core\AuthSystem;
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 18:59:06 +00:00
|
|
|
class AuthSystemDefault implements AuthSystem{
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2017-01-30 17:39:21 +00:00
|
|
|
public function __construct(){
|
|
|
|
self::check();
|
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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-06 15:36:26 +00:00
|
|
|
if( !isset($_SESSION['AUTH']) ) $_SESSION['AUTH'] = [];
|
|
|
|
if( !isset($_SESSION['PERM']) ) $_SESSION['PERM'] = [];
|
|
|
|
if( !isset($_SESSION['WAREHOUSE']) ) $_SESSION['WAREHOUSE'] = [];
|
|
|
|
if( !isset($_SESSION['ADMIN']) ) $_SESSION['ADMIN'] = [];
|
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
|
|
|
|
---------------------------------------------------------*/
|
2017-02-21 14:16:21 +00:00
|
|
|
/* (1) Triple authentification (warehouse+SATS_token+SATS_nexttoken) */
|
|
|
|
if( preg_match('/^([a-f0-9]{128})([a-f0-9]{128})([a-f0-9]{128})$/', $AUTH, $match) )
|
|
|
|
$_SESSION['AUTH'] = [ $match[1], $match[2], $match[3] ];
|
|
|
|
|
|
|
|
/* (2) Double authentification (warehouse+admin) */
|
|
|
|
else if( preg_match('/^([a-f0-9]{128})([a-f0-9]{128})$/', $AUTH, $match) )
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [ $match[1], $match[2] ];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
/* (3) Authentification unique (warehouse) */
|
|
|
|
else if( preg_match('/^[a-f0-9]{128}$/', $AUTH, $match) )
|
2016-07-04 13:45:29 +00:00
|
|
|
$_SESSION['AUTH'] = [ $match[0] ];
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
/* (4) Aucune authentification */
|
2016-07-06 15:36:26 +00:00
|
|
|
else{
|
|
|
|
$_SESSION['AUTH'] = [];
|
|
|
|
$_SESSION['PERM'] = [];
|
|
|
|
$_SESSION['WAREHOUSE'] = [];
|
|
|
|
$_SESSION['ADMIN'] = [];
|
|
|
|
}
|
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-06 15:36:26 +00:00
|
|
|
if( !self::deepCheck() ){
|
|
|
|
$_SESSION['AUTH'] = [];
|
|
|
|
$_SESSION['PERM'] = [];
|
|
|
|
$_SESSION['WAREHOUSE'] = [];
|
|
|
|
$_SESSION['ADMIN'] = [];
|
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2017-02-21 14:39:28 +00:00
|
|
|
private static function deepCheck(){
|
2017-02-21 14:16:21 +00:00
|
|
|
|
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
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
|
|
|
|
/* [2] Si authentification unique -> WAREHOUSE
|
2016-07-04 09:04:49 +00:00
|
|
|
=========================================================*/
|
2016-07-04 15:23:19 +00:00
|
|
|
if( self::auth() >= 1 ){
|
2017-02-21 14:16:21 +00:00
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
$checkRoot = new Repo('warehouse/getByToken', [ $_SESSION['AUTH'][0] ]);
|
2016-07-04 09:29:48 +00:00
|
|
|
|
2016-07-06 09:32:51 +00:00
|
|
|
/* (1) Si le token n'existe pas, on retourne une erreur */
|
2016-07-06 15:36:26 +00:00
|
|
|
if( $checkRoot->answer() == false )
|
2016-07-04 09:29:48 +00:00
|
|
|
return false;
|
2016-07-05 13:21:32 +00:00
|
|
|
|
2016-07-06 09:32:51 +00:00
|
|
|
/* (2) On met à jour les informations de l'entrepot */
|
2016-07-05 13:21:32 +00:00
|
|
|
$_SESSION['WAREHOUSE'] = [
|
2016-07-12 08:53:26 +00:00
|
|
|
'id' => (int) $checkRoot->answer()[0]['id_warehouse'],
|
|
|
|
'name' => $checkRoot->answer()[0]['name'],
|
2016-07-12 09:02:46 +00:00
|
|
|
'theme' => '#'.$checkRoot->answer()[0]['theme']
|
2016-07-05 13:21:32 +00:00
|
|
|
];
|
2016-07-06 09:32:51 +00:00
|
|
|
|
|
|
|
/* (3) On récupère les modules de l'entrepot */
|
|
|
|
$getModules = new Repo('warehouse/getModules', [ $_SESSION['WAREHOUSE']['id'] ]);
|
|
|
|
|
|
|
|
$_SESSION['WAREHOUSE']['modules'] = $getModules->answer();
|
2017-02-21 14:16:21 +00:00
|
|
|
|
2016-07-04 09:29:48 +00:00
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
|
|
|
|
/* [3] Si authentification double -> WAREHOUSE + ADMIN
|
2016-07-04 09:04:49 +00:00
|
|
|
=========================================================*/
|
2017-02-21 14:16:21 +00:00
|
|
|
if( self::auth() == 2 ){
|
|
|
|
|
2016-07-14 07:25:09 +00:00
|
|
|
$checkBranch = new Repo('admin/getByToken', [ $_SESSION['WAREHOUSE']['id'], $_SESSION['AUTH'][1] ]);
|
2016-07-04 09:29:48 +00:00
|
|
|
|
2016-07-06 09:32:51 +00:00
|
|
|
/* (1) Si le token n'existe pas, on retourne une erreur */
|
2016-07-06 15:36:26 +00:00
|
|
|
if( $checkBranch->answer() == false )
|
2016-07-04 09:29:48 +00:00
|
|
|
return false;
|
2016-07-14 07:36:18 +00:00
|
|
|
|
2016-07-14 07:25:09 +00:00
|
|
|
/* (2) On met à jour les informations de l'administrateur */
|
2016-07-05 13:21:32 +00:00
|
|
|
$_SESSION['ADMIN'] = [
|
2016-07-14 07:36:18 +00:00
|
|
|
'id' => (int) $checkBranch->answer()['id_admin'],
|
|
|
|
'username' => $checkBranch->answer()['username'],
|
|
|
|
'mail' => $checkBranch->answer()['mail']
|
2016-07-05 13:21:32 +00:00
|
|
|
];
|
2017-02-21 14:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] Si authentification triple -> WAREHOUSE + SATS_token + SATS_nexttoken
|
|
|
|
=========================================================*/
|
|
|
|
if( self::auth() == 3 ){
|
|
|
|
|
|
|
|
|
|
|
|
$checkBranch = new Repo('machine/checkToken', [ $_SESSION['WAREHOUSE']['id'], $_SESSION['AUTH'][1], $_SESSION['AUTH'][2] ]);
|
|
|
|
|
|
|
|
/* (1) Si le token n'est pas valide, on retourne une erreur */
|
2017-02-21 14:55:40 +00:00
|
|
|
if( $checkBranch->answer() === false )
|
2017-02-21 14:16:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) On met à jour les informations de l'administrateur */
|
|
|
|
$_SESSION['SATS'] = [
|
2017-02-21 14:55:40 +00:00
|
|
|
'id' => (int) $checkBranch->answer()
|
2017-02-21 14:16:21 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
2016-07-04 09:29:48 +00:00
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
|
|
|
|
/* [5] 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
|
|
|
|
|
|
|
|
2017-02-21 14:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 15:23:19 +00:00
|
|
|
/* VERIFICATION DES ACCES EN FONCTION DE PERMISSIONS ATTENDUES
|
|
|
|
*
|
2016-11-08 08:34:02 +00:00
|
|
|
* @module<String> Module concerné
|
2017-02-21 14:39:28 +00:00
|
|
|
* @expected<array> Liste de listes de combinaisons de permissions attendues
|
2016-07-04 15:23:19 +00:00
|
|
|
*
|
2017-01-30 17:39:21 +00:00
|
|
|
* @return error<Error> Si FALSE, pas la permission, sinon si
|
2016-07-04 15:23:19 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-11-08 08:34:02 +00:00
|
|
|
public static function permission($module, $expected){
|
2017-02-21 14:39:28 +00:00
|
|
|
|
2017-09-20 12:47:17 +00:00
|
|
|
$error_propag = [];
|
|
|
|
|
2017-02-21 14:39:28 +00:00
|
|
|
/* [1] Check format -> if not array of array(s) -> ERROR
|
|
|
|
=========================================================*/
|
2017-09-20 23:55:53 +00:00
|
|
|
/* (1) If not array -> ERROR */
|
|
|
|
if( !is_array($expected) )
|
|
|
|
return new Error(Err::FormatError);
|
|
|
|
|
|
|
|
/* (2) If not array of array(s) -> ERROR */
|
2017-02-21 14:39:28 +00:00
|
|
|
foreach($expected as $permissions)
|
|
|
|
if( !is_array($permissions) )
|
|
|
|
return new Error(Err::FormatError);
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Foreach each set of permission
|
|
|
|
=========================================================*/
|
|
|
|
foreach($expected as $permission_group){
|
|
|
|
|
|
|
|
/* If granted -> don't go further */
|
2017-09-20 12:47:17 +00:00
|
|
|
$error_propag[]= self::check_permission_group($module, $permission_group);
|
2017-09-20 23:55:53 +00:00
|
|
|
|
2017-09-20 12:47:17 +00:00
|
|
|
if( $error_propag[count($error_propag)-1] == Err::Success )
|
2017-02-21 14:39:28 +00:00
|
|
|
return new Error(Err::Success);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] By default return `PermissionError`
|
2016-07-04 15:23:19 +00:00
|
|
|
=========================================================*/
|
2017-09-20 12:47:17 +00:00
|
|
|
if( count($error_propag) > 0 )
|
|
|
|
return new Error($error_propag[count($error_propag)-1]);
|
|
|
|
|
2017-02-21 14:39:28 +00:00
|
|
|
return new Error(Err::PermissionError);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-20 12:47:17 +00:00
|
|
|
|
2017-02-21 14:39:28 +00:00
|
|
|
/* VERIFICATION DES ACCES EN FONCTION DE PERMISSIONS ATTENDUES
|
|
|
|
*
|
|
|
|
* @module<String> Module concerné
|
|
|
|
* @expected<array> Liste des permissions attendues
|
|
|
|
*
|
|
|
|
* @return error<int> Err:: error constants
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function check_permission_group($module, $expected){
|
2016-07-04 15:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Gestion de l'AUTH (authentification)
|
|
|
|
=========================================================*/
|
|
|
|
|
|
|
|
/* (1) Si entrepot requis, mais manquant
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( in_array('warehouse', $expected) && self::auth() < 1 )
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::PermissionError;
|
2016-07-04 15:23:19 +00:00
|
|
|
|
|
|
|
/* (2) Si admin requis, mais manquant
|
|
|
|
---------------------------------------------------------*/
|
2017-09-20 23:55:53 +00:00
|
|
|
if( in_array('admin', $expected) && self::auth() != 2 )
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::PermissionError;
|
2016-07-04 15:23:19 +00:00
|
|
|
|
2017-02-21 14:16:21 +00:00
|
|
|
/* (3) Si SATS requis, mais manquant
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( in_array('sats', $expected) && self::auth() < 3 )
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::TokenError;
|
2017-02-21 14:16:21 +00:00
|
|
|
|
|
|
|
/* (4) On retire 'warehouse', 'admin' et 'sats' de @expected
|
2016-07-04 15:23:19 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
$warehouseIndex = array_search('warehouse', $expected);
|
|
|
|
$adminIndex = array_search('admin', $expected);
|
2017-02-21 14:16:21 +00:00
|
|
|
$satsIndex = array_search('sats', $expected);
|
2016-07-04 15:23:19 +00:00
|
|
|
if( is_int($warehouseIndex) ) unset($expected[$warehouseIndex]);
|
|
|
|
if( is_int($adminIndex) ) unset($expected[$adminIndex]);
|
2017-02-21 14:16:21 +00:00
|
|
|
if( is_int($satsIndex) ) unset($expected[$satsIndex]);
|
2016-07-04 15:23:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Gestion des permissions
|
|
|
|
=========================================================*/
|
2017-09-20 23:55:53 +00:00
|
|
|
|
2016-11-08 08:34:02 +00:00
|
|
|
/* (1) Vérification de toutes les permissions requises */
|
|
|
|
foreach($expected as $permission)
|
|
|
|
// Si il manque au minimum une permission, on retourne FALSE
|
|
|
|
if( !in_array($permission, $_SESSION['PERM']) )
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::PermissionError;
|
2016-07-04 15:23:19 +00:00
|
|
|
|
|
|
|
|
2016-11-08 08:34:02 +00:00
|
|
|
/* [3] Vérification que le module est actif pour l'entrepot
|
2016-07-04 15:23:19 +00:00
|
|
|
=========================================================*/
|
2016-11-08 08:34:02 +00:00
|
|
|
|
|
|
|
/* (1) On vérifie que le module est actif dans l'entrepot */
|
|
|
|
$allowedModule = isset($_SESSION['WAREHOUSE']['modules'])
|
|
|
|
&& is_array($_SESSION['WAREHOUSE']['modules'])
|
|
|
|
&& in_array($module, $_SESSION['WAREHOUSE']['modules']);
|
|
|
|
|
|
|
|
/* (2) On vérifie si le module est un module "Default" */
|
|
|
|
$defaultModule = preg_match('/^(\w+)Default$/', $module);
|
|
|
|
|
|
|
|
/* (3) Si aucune autorisation et pas module "Default" */
|
|
|
|
if( !$allowedModule && !$defaultModule )
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::DisabledModule;
|
2016-11-08 08:34:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] Si on a toutes les permissions requises
|
|
|
|
=========================================================*/
|
2017-02-21 14:39:28 +00:00
|
|
|
return Err::Success;
|
2016-07-04 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
?>
|