2016-07-04 13:45:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
|
|
|
use \manager\Database;
|
|
|
|
use \manager\sessionManager;
|
|
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
|
|
|
|
class admin extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'admin'; return $table_name; }
|
|
|
|
|
|
|
|
/* CREATION D'UN ADMINISTRATEUR
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($username, $mail, $password){
|
|
|
|
/* [1] Verification de l'identifiant du nom+mail (username, mail)
|
|
|
|
=========================================================*/
|
|
|
|
$check_unique = count(self::getByUsername($username)) == 0;
|
|
|
|
$check_unique = $check_unique && count(self::getByMail($mail)) == 0;
|
|
|
|
|
2016-07-04 15:44:21 +00:00
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
// Si un administrateur a deja cet identifiant ou mail, on renvoie une erreur
|
|
|
|
if( !$check_unique )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Creation de l'administrateur
|
|
|
|
=========================================================*/
|
|
|
|
$insert_admin = Database::getPDO()->prepare("INSERT INTO admin(id_admin, username, mail, password, token)
|
|
|
|
VALUES(DEFAULT, :username, :mail, :password, :token)");
|
|
|
|
$insert_admin->execute([
|
|
|
|
':username' => $username,
|
|
|
|
':mail' => $mail,
|
2016-07-07 15:59:31 +00:00
|
|
|
':password' => sessionManager::secure_hash( $password ),
|
|
|
|
':token' => sessionManager::secure_hash( uniqid() )
|
2016-07-04 13:45:29 +00:00
|
|
|
]);
|
|
|
|
|
2016-07-04 15:44:21 +00:00
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
/* [3] On retourne l'id_admin ou FALSE si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$check_admin = self::getByUsername($username);
|
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
2016-07-04 15:44:21 +00:00
|
|
|
if( count($check_admin) == 0 )
|
2016-07-04 13:45:29 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return intval( $check_admin[0]['id_admin'] );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* (2) On applique le nouveau token */
|
|
|
|
$update_token = Database::getPDO()->prepare("UPDATE admin
|
|
|
|
SET token = :token
|
|
|
|
WHERE id_admin = :id_admin");
|
|
|
|
|
|
|
|
$update_token->execute([
|
|
|
|
':token' => $new_token,
|
|
|
|
':id_admin' => $id_admin
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On renvoie le token
|
|
|
|
=========================================================*/
|
|
|
|
return $new_token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
|
|
|
/* [1] Verification de l'unicite du nom (name) (si different)
|
|
|
|
=========================================================*/
|
2016-07-04 15:44:21 +00:00
|
|
|
// On recupere les administrateurs ayant le meme identifant ou mail (si existent)
|
|
|
|
// pour éviter les collisions (car l'identifiant et le mail doivent être uniques)
|
|
|
|
$getbyuname = self::getByUsername($username);
|
2016-07-04 13:45:29 +00:00
|
|
|
$getbymail = self::getByMail($mail);
|
|
|
|
|
2016-07-04 15:44:21 +00:00
|
|
|
$check_unique = count($getbyuname) > 0 && $getbyuname['id_user'] == $id_user || !is_array($getbyuname);
|
|
|
|
$check_unique = $check_unique && count($getbymail) > 0 && $getbymail['id_user'] == $id_user || !is_array($getbymail);
|
2016-07-04 13:45:29 +00:00
|
|
|
|
2016-07-04 15:44:21 +00:00
|
|
|
// Si un administrateur a deja cet identifiant ou ce mail (excepté lui-même), on renvoie une erreur
|
2016-07-04 13:45:29 +00:00
|
|
|
if( !$check_unique )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Modification de l'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
$edit_admin = Database::getPDO()->prepare("UPDATE admin
|
2016-07-04 15:44:21 +00:00
|
|
|
SET username = :username,
|
|
|
|
mail = :mail,
|
|
|
|
password = :password
|
2016-07-04 13:45:29 +00:00
|
|
|
WHERE id_admin = :id_admin");
|
|
|
|
$edit_admin->execute([
|
2016-07-04 15:44:21 +00:00
|
|
|
':username' => $username,
|
|
|
|
':mail' => $mail,
|
|
|
|
':password' => $password,
|
2016-07-04 13:45:29 +00:00
|
|
|
':id_admin' => $id_admin
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$check_admin = self::getById($id_admin);
|
|
|
|
|
|
|
|
$checker = $check_admin['id_admin'] == $id_admin;
|
|
|
|
$checker = $checker && $check_admin['name'] == $name;
|
|
|
|
$checker = $checker && $check_admin['password'] == $password;
|
|
|
|
|
|
|
|
// On retourne l'etat de la modification
|
|
|
|
return $checker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UN ENTREPOT DONNE
|
|
|
|
*
|
|
|
|
* @id_admin<int> UID de l'administrateur en question
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'administrateur a bien ete supprime
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delete($id_admin){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete_admin = Database::getPDO()->prepare("DELETE FROM admin WHERE id_admin = :id_admin");
|
|
|
|
$delete_admin->execute([ ':id_admin' => $id_admin ]);
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
|
|
=========================================================*/
|
|
|
|
return ( self::getById($id_admin) === false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|