2016-04-12 12:27:54 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace api\module;
|
2016-04-12 12:27:54 +00:00
|
|
|
use \manager\sessionManager;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-04-12 12:27:54 +00:00
|
|
|
use \manager\ManagerError;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \database\core\Repo;
|
2016-04-12 12:27:54 +00:00
|
|
|
|
2016-04-14 10:45:54 +00:00
|
|
|
class user{
|
2016-04-12 12:27:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DES LOGINS D'UN UTILISATEUR
|
|
|
|
*
|
|
|
|
* @login<String> Username ou adresse mail de l'utilisateur
|
|
|
|
* @password<String> Password de l'utilisateur
|
|
|
|
*
|
|
|
|
* @return id_user<int> Retourne l'id de l'utilisateur si les logins sont corrects
|
|
|
|
*
|
|
|
|
*/
|
2016-04-18 09:30:38 +00:00
|
|
|
public static function login($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-04-12 12:27:54 +00:00
|
|
|
/* [1] On verifie les logins
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$check = new Repo('user/login', [$login, $password]);
|
2016-04-12 14:30:14 +00:00
|
|
|
$user = $check->answer();
|
2016-04-12 12:27:54 +00:00
|
|
|
|
|
|
|
// Si le login/password est incorrect, on retourne une erreur
|
2016-09-12 16:04:53 +00:00
|
|
|
if( $user === false ) return ['ModuleError' => ManagerError::ModuleError];
|
2016-04-12 12:27:54 +00:00
|
|
|
|
|
|
|
|
2016-04-12 14:30:14 +00:00
|
|
|
/* [2] Gestion du login
|
2016-04-12 12:27:54 +00:00
|
|
|
=========================================================*/
|
2016-04-12 14:30:14 +00:00
|
|
|
$_SESSION['userid'] = $user['id_user'];
|
|
|
|
$_SESSION['username'] = $user['login'];
|
2016-10-24 13:47:28 +00:00
|
|
|
$_SESSION['permission'] = ['admin'];
|
2016-04-12 14:30:14 +00:00
|
|
|
|
|
|
|
/* [4] Retour de l'id user
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-12 12:27:54 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-04-12 14:30:14 +00:00
|
|
|
'id_user' => $user['id_user']
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-12 12:27:54 +00:00
|
|
|
|
|
|
|
}
|
2016-04-12 14:47:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* DECONNEXION D'UN UTILISATEUR
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function logout(){
|
|
|
|
$_SESSION['userid'] = null;
|
|
|
|
$_SESSION['username'] = null;
|
2016-09-12 16:04:53 +00:00
|
|
|
$_SESSION['permission'] = [];
|
2016-10-24 13:47:28 +00:00
|
|
|
|
|
|
|
return ['ModuleError' => ManagerError::Success];
|
2016-04-12 14:47:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 10:45:54 +00:00
|
|
|
|
|
|
|
|
2016-04-19 12:35:40 +00:00
|
|
|
|
|
|
|
/* RETOURNE L'UTILISATEUR D'ID DONNE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return user<Array> Tableau contenant les informations de l'utilisateur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getById($params){
|
|
|
|
extract($params);
|
|
|
|
|
|
|
|
/* [1] On recupere les donnees de l'utilisateur
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$getUser = new Repo('user/getById', [$id_user]);
|
2016-04-19 12:35:40 +00:00
|
|
|
$user_data = $getUser->answer();
|
|
|
|
|
|
|
|
// Si aucun utilisateur n'est trouve
|
2016-09-12 16:04:53 +00:00
|
|
|
if( count($user_data) == 0 ) return ['ModuleError' => ManagerError::ModuleError];
|
2016-04-19 12:35:40 +00:00
|
|
|
|
|
|
|
/* [3] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-19 12:35:40 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
2016-04-19 13:30:33 +00:00
|
|
|
'user' => $user_data[0]
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-19 12:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-14 12:59:21 +00:00
|
|
|
/* RETOURNE LA LISTE DE TOUS LES UTILISATEURS
|
|
|
|
*
|
|
|
|
* @return users<Array> Tableau contenant les informations de tous les utilisateurs
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll(){
|
|
|
|
/* [1] On recupere les donnees des utilisateurs
|
|
|
|
=========================================================*/
|
|
|
|
$getUsers = new Repo('user/getAll');
|
|
|
|
$users_data = $getUsers->answer();
|
|
|
|
|
|
|
|
// Si aucun utilisateur n'est trouve
|
2016-09-12 16:04:53 +00:00
|
|
|
if( $users_data === false ) return ['ModuleError' => ManagerError::ModuleError];
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
/* [3] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-14 12:59:21 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'users' => $users_data
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-14 12:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-14 16:49:07 +00:00
|
|
|
/* CREATION D'UN UTILISATEUR
|
2016-04-14 12:59:21 +00:00
|
|
|
*
|
|
|
|
* @login<String> Login (identifiant) de l'utilisateur
|
|
|
|
* @password<String> Password de l'utilisateur
|
|
|
|
* @mail<String> Adresse mail de l'utilisateur
|
|
|
|
*
|
|
|
|
* @return id_user<int> Renvoie l'id de l'utilisateur cree
|
|
|
|
*
|
|
|
|
*/
|
2016-04-18 09:30:38 +00:00
|
|
|
public static function create($params){
|
|
|
|
extract($params);
|
|
|
|
|
2016-04-14 12:59:21 +00:00
|
|
|
/* [0] Verification et formattage des INPUT
|
|
|
|
=========================================================*/
|
|
|
|
$password = sessionManager::sha1($password);
|
2016-04-18 17:09:25 +00:00
|
|
|
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
/* [1] Creation de l'utilisateur
|
|
|
|
=========================================================*/
|
2016-10-24 13:34:18 +00:00
|
|
|
$create = new Repo('user/create', [$login, $password, $mail]);
|
2016-04-14 12:59:21 +00:00
|
|
|
$created_id = $create->answer();
|
|
|
|
|
|
|
|
// Si erreur de creation, on retourne une erreur
|
2016-09-12 16:04:53 +00:00
|
|
|
if( $created_id === false ) return [ 'ModuleError' => ManagerError::ModuleError ];
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
/* [2] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-14 12:59:21 +00:00
|
|
|
'ModuleError' => ManagerError::Success,
|
|
|
|
'id_user' => $created_id
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-14 12:59:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 16:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRESSION D'UN UTILISATEUR
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
*/
|
2016-04-18 09:30:38 +00:00
|
|
|
public static function remove($params){
|
|
|
|
extract($params);
|
2016-04-18 17:09:25 +00:00
|
|
|
|
2016-04-14 16:49:07 +00:00
|
|
|
/* [1] On verifie que l'utilisateur existe
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$exists = new Repo('user/getById', [$id_user]);
|
2016-04-14 16:49:07 +00:00
|
|
|
$exists_status = $exists->answer();
|
|
|
|
|
|
|
|
// Si l'utilisateur n'existe pas, on retourne une erreur
|
2016-09-12 16:04:53 +00:00
|
|
|
if( count($exists_status) == 0 ) return [ 'ModuleError' => ManagerError::ModuleError ];
|
2016-04-14 16:49:07 +00:00
|
|
|
|
|
|
|
/* [2] Suppression de l'utilisateur
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$remove = new Repo('user/remove', [$id_user]);
|
2016-04-14 16:49:07 +00:00
|
|
|
$remove_status = $remove->answer();
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On verifie que l'utilisateur est bien supprime
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$removed = new Repo('user/getById', [$id_user]);
|
2016-04-14 16:49:07 +00:00
|
|
|
$removed_status = $removed->answer();
|
|
|
|
|
|
|
|
// Si l'utilisateur n'existe pas, on retourne une erreur
|
2016-09-12 16:04:53 +00:00
|
|
|
if( count($removed_status) > 0 ) return [ 'ModuleError' => ManagerError::ModuleError ];
|
2016-04-14 16:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
return [ 'ModuleError' => ManagerError::Success ];
|
2016-04-14 16:49:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 12:27:54 +00:00
|
|
|
// [x] "login",
|
2016-04-12 14:47:06 +00:00
|
|
|
// [x] "logout",
|
2016-04-14 10:45:54 +00:00
|
|
|
// [x] "getById",
|
2016-04-14 16:49:07 +00:00
|
|
|
// [x] "getAll",
|
|
|
|
// [x] "create",
|
2016-04-12 12:27:54 +00:00
|
|
|
// [ ] "remove",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-18 09:30:38 +00:00
|
|
|
?>
|