2016-07-04 13:45:29 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace database\repo;
|
2016-11-05 13:57:35 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-07-04 13:45:29 +00:00
|
|
|
use \manager\sessionManager;
|
2016-10-18 14:03:03 +00:00
|
|
|
use \orm\core\Table;
|
|
|
|
use \orm\core\Rows;
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
class admin extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'admin'; return $table_name; }
|
|
|
|
|
|
|
|
/* CREATION D'UN ADMINISTRATEUR
|
|
|
|
*
|
2017-10-12 12:29:52 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepôt
|
2016-07-04 13:45:29 +00:00
|
|
|
* @username<String> Identifiant de l'administrateur
|
|
|
|
* @mail<String> Adresse mail de l'administrateur
|
|
|
|
* @password<String> Mot de passe de l'administrateur
|
|
|
|
*
|
|
|
|
* @return id_admin<int> Renvoie l'UID de l'administrateur cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
2017-10-12 12:29:52 +00:00
|
|
|
public static function create($id_warehouse, $username, $mail, $password){
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [1] Création de l'administrateur
|
2016-07-04 13:45:29 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$inserted = Table::get('admin')
|
|
|
|
->insert([
|
2017-10-12 12:29:52 +00:00
|
|
|
'id_admin' => Rows::INSERT_DEFAULT,
|
|
|
|
'id_warehouse' => $id_warehouse,
|
|
|
|
'username' => $username,
|
|
|
|
'mail' => $mail,
|
|
|
|
'password' => \secure_hash( $password, $username ),
|
|
|
|
'token' => \secure_hash( uniqid(), $username.'-*-'.$mail )
|
2016-07-23 15:52:24 +00:00
|
|
|
]);
|
2016-07-04 15:44:21 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
// Si username ou mail déja pris
|
|
|
|
if( !$inserted )
|
2016-07-04 13:45:29 +00:00
|
|
|
return false;
|
|
|
|
|
2016-07-04 15:44:21 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne l'id_admin ou FALSE si erreur
|
2016-07-04 13:45:29 +00:00
|
|
|
=========================================================*/
|
2017-10-12 12:29:52 +00:00
|
|
|
$check_admin = self::getByUsername($id_warehouse, $username);
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
2016-07-23 16:06:39 +00:00
|
|
|
if( $check_admin === false )
|
2016-07-04 13:45:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
2016-07-23 16:06:39 +00:00
|
|
|
return $check_admin['id_admin'];
|
2016-07-23 15:52:24 +00:00
|
|
|
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MISE A JOUR DU TOKEN D'UN ADMINISTRATEUR
|
|
|
|
*
|
|
|
|
* @id_admin<int> UID de l'administrateur
|
|
|
|
*
|
|
|
|
* @return new_token<String> Renvoie le nouveau token
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function updateToken($id_admin=null){
|
|
|
|
/* [1] On vérifie que l'administrateur existe
|
|
|
|
=========================================================*/
|
|
|
|
if( self::getById($id_admin) === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On met à jour le token
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On crée un nouveau token */
|
2016-07-07 15:59:31 +00:00
|
|
|
$new_token = sessionManager::secure_hash( uniqid() );
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
$updated = Table::get('admin')
|
|
|
|
->whereId($id_admin)
|
|
|
|
->edit([ 'token' => $new_token ]);
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
/* [3] On renvoie le token
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return ( $updated ) ? $new_token : false;
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MODIFICATION D'UN ADMINISTRATEUR
|
|
|
|
*
|
|
|
|
* @id_admin<int> UID de l'administrateur
|
|
|
|
* @username<String> Identifiant de l'administrateur
|
|
|
|
* @mail<String> Adresse mail de l'administrateur
|
|
|
|
* @password<String> Mot de passe de l'administrateur
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Renvoie si oui ou non tout s'est bien passe
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function edit($id_admin=null, $username=null, $mail=null, $password=null){
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [1] On rédige/exécute la requête
|
2016-07-04 13:45:29 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$updated = Table::get('admin')
|
|
|
|
->whereId($id_admin)
|
|
|
|
->edit([
|
|
|
|
'username' => $username,
|
|
|
|
'mail' => $mail,
|
|
|
|
'password' => $password
|
|
|
|
]);
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne l'etat de la modification
|
2016-07-04 13:45:29 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $updated; // FAUX si @username ou @mail déja pris ou autre erreur
|
2016-07-04 13:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UN ENTREPOT DONNE
|
|
|
|
*
|
2017-11-09 11:37:27 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepôt en question
|
|
|
|
* @id_admin<int> UID de l'administrateur en question
|
2016-07-04 13:45:29 +00:00
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'administrateur a bien ete supprime
|
|
|
|
*
|
|
|
|
*/
|
2017-11-09 11:37:27 +00:00
|
|
|
public static function delete($id_warehouse, $id_admin){
|
2016-07-04 13:45:29 +00:00
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$deleted = Table::get('admin')
|
2017-11-09 11:37:27 +00:00
|
|
|
->whereIdWarehouse($id_warehouse)
|
2016-07-23 15:52:24 +00:00
|
|
|
->whereId($id_admin)
|
|
|
|
->delete();
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $deleted;
|
2016-07-04 13:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-14 07:25:09 +00:00
|
|
|
/* RETOURNE UN ADMINISTRATEUR PARTICULIER
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @id_admin<String> Token de l'administrateur en question
|
|
|
|
*
|
|
|
|
* @return admin<Array> Retourne le premier résultat
|
|
|
|
* FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getById($id_warehouse, $id_admin){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$admin = Table::get('admin')
|
|
|
|
->whereId($id_admin)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->unique()
|
2016-07-23 16:06:39 +00:00
|
|
|
->select('*')
|
2016-07-23 15:52:24 +00:00
|
|
|
->fetch();
|
2016-07-14 07:25:09 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne le résultat
|
2016-07-14 07:25:09 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $admin;
|
2016-07-14 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN ADMINISTRATEUR PARTICULIER
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @username<String> Identifiant de l'administrateur en question
|
|
|
|
*
|
|
|
|
* @return admin<Array> Retourne le premier résultat
|
|
|
|
* FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByUsername($id_warehouse, $username){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$admin = Table::get('admin')
|
|
|
|
->whereUsername($username)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->unique()
|
2016-07-23 16:06:39 +00:00
|
|
|
->select('*')
|
2016-07-23 15:52:24 +00:00
|
|
|
->fetch();
|
2016-07-14 07:25:09 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne le résultat
|
2016-07-14 07:25:09 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $admin;
|
2016-07-14 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN ADMINISTRATEUR PARTICULIER
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @mail<String> Adresse mail de l'administrateur en question
|
|
|
|
*
|
|
|
|
* @return admin<Array> Retourne le premier résultat
|
|
|
|
* FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByMail($id_warehouse, $mail){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$admin = Table::get('admin')
|
|
|
|
->whereMail($mail)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->unique()
|
2016-07-23 16:06:39 +00:00
|
|
|
->select('*')
|
2016-07-23 15:52:24 +00:00
|
|
|
->fetch();
|
2016-07-14 07:25:09 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne le résultat
|
2016-07-14 07:25:09 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $admin;
|
2016-07-14 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN ADMINISTRATEUR PARTICULIER
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @token<String> Token de l'administrateur en question
|
|
|
|
*
|
|
|
|
* @return admin<Array> Retourne le premier résultat
|
|
|
|
* FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByToken($id_warehouse, $token){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
$admin = Table::get('admin')
|
|
|
|
->whereToken($token)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->unique()
|
2016-07-23 16:06:39 +00:00
|
|
|
->select('*')
|
2016-07-23 15:52:24 +00:00
|
|
|
->fetch();
|
2016-07-14 07:25:09 +00:00
|
|
|
|
2016-07-23 15:52:24 +00:00
|
|
|
/* [2] On retourne le résultat
|
2016-07-14 07:25:09 +00:00
|
|
|
=========================================================*/
|
2016-07-23 15:52:24 +00:00
|
|
|
return $admin;
|
2016-07-14 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|