Repository 'admin' utilise entièrement ORM
This commit is contained in:
parent
4993a30f7d
commit
7bcf154afb
|
@ -3,7 +3,8 @@
|
||||||
namespace manager\repo;
|
namespace manager\repo;
|
||||||
use \manager\Database;
|
use \manager\Database;
|
||||||
use \manager\sessionManager;
|
use \manager\sessionManager;
|
||||||
use \manager\repo\cluster as clusterRepo;
|
use \manager\ORM\Table;
|
||||||
|
use \manager\ORM\Rows;
|
||||||
|
|
||||||
class admin extends parentRepo{
|
class admin extends parentRepo{
|
||||||
|
|
||||||
|
@ -20,29 +21,23 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function create($username, $mail, $password){
|
public static function create($username, $mail, $password){
|
||||||
/* [1] Verification de l'identifiant du nom+mail (username, mail)
|
/* [1] Création de l'administrateur
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$check_unique = count(self::getByUsername($username)) == 0;
|
$inserted = Table::get('admin')
|
||||||
$check_unique = $check_unique && count(self::getByMail($mail)) == 0;
|
->insert([
|
||||||
|
'id_admin' => Rows::DEFAULT,
|
||||||
|
'username' => $username,
|
||||||
// Si un administrateur a deja cet identifiant ou mail, on renvoie une erreur
|
'mail' => $mail,
|
||||||
if( !$check_unique )
|
'password' => sessionManager::secure_hash( $password ),
|
||||||
return false;
|
'token' => sessionManager::secure_hash( uniqid() )
|
||||||
|
|
||||||
/* [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,
|
|
||||||
':password' => sessionManager::secure_hash( $password ),
|
|
||||||
':token' => sessionManager::secure_hash( uniqid() )
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Si username ou mail déja pris
|
||||||
|
if( !$inserted )
|
||||||
|
return false;
|
||||||
|
|
||||||
/* [3] On retourne l'id_admin ou FALSE si erreur
|
|
||||||
|
/* [2] On retourne l'id_admin ou FALSE si erreur
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$check_admin = self::getByUsername($username);
|
$check_admin = self::getByUsername($username);
|
||||||
|
|
||||||
|
@ -51,7 +46,10 @@
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Sinon, on retourne son id
|
// Sinon, on retourne son id
|
||||||
return intval( $check_admin[0]['id_admin'] );
|
return $check_admin[0]['id_admin'];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +67,6 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function updateToken($id_admin=null){
|
public static function updateToken($id_admin=null){
|
||||||
|
|
||||||
/* [1] On vérifie que l'administrateur existe
|
/* [1] On vérifie que l'administrateur existe
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( self::getById($id_admin) === false )
|
if( self::getById($id_admin) === false )
|
||||||
|
@ -81,20 +78,13 @@
|
||||||
/* (1) On crée un nouveau token */
|
/* (1) On crée un nouveau token */
|
||||||
$new_token = sessionManager::secure_hash( uniqid() );
|
$new_token = sessionManager::secure_hash( uniqid() );
|
||||||
|
|
||||||
/* (2) On applique le nouveau token */
|
$updated = Table::get('admin')
|
||||||
$update_token = Database::getPDO()->prepare("UPDATE admin
|
->whereId($id_admin)
|
||||||
SET token = :token
|
->edit([ 'token' => $new_token ]);
|
||||||
WHERE id_admin = :id_admin");
|
|
||||||
|
|
||||||
$update_token->execute([
|
|
||||||
':token' => $new_token,
|
|
||||||
':id_admin' => $id_admin
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
|
||||||
/* [3] On renvoie le token
|
/* [3] On renvoie le token
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
return $new_token;
|
return ( $updated ) ? $new_token : false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,46 +106,20 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static function edit($id_admin=null, $username=null, $mail=null, $password=null){
|
public static function edit($id_admin=null, $username=null, $mail=null, $password=null){
|
||||||
/* [1] Verification de l'unicite du nom (name) (si different)
|
/* [1] On rédige/exécute la requête
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
// On recupere les administrateurs ayant le meme identifant ou mail (si existent)
|
$updated = Table::get('admin')
|
||||||
// pour éviter les collisions (car l'identifiant et le mail doivent être uniques)
|
->whereId($id_admin)
|
||||||
$getbyuname = self::getByUsername($username);
|
->edit([
|
||||||
$getbymail = self::getByMail($mail);
|
'username' => $username,
|
||||||
|
'mail' => $mail,
|
||||||
$check_unique = count($getbyuname) > 0 && $getbyuname['id_user'] == $id_user || !is_array($getbyuname);
|
'password' => $password
|
||||||
$check_unique = $check_unique && count($getbymail) > 0 && $getbymail['id_user'] == $id_user || !is_array($getbymail);
|
|
||||||
|
|
||||||
// Si un administrateur a deja cet identifiant ou ce mail (excepté lui-même), on renvoie une erreur
|
|
||||||
if( !$check_unique )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
/* [2] Modification de l'utilisateur
|
|
||||||
=========================================================*/
|
|
||||||
$edit_admin = Database::getPDO()->prepare("UPDATE admin
|
|
||||||
SET username = :username,
|
|
||||||
mail = :mail,
|
|
||||||
password = :password
|
|
||||||
WHERE id_admin = :id_admin");
|
|
||||||
$edit_admin->execute([
|
|
||||||
':username' => $username,
|
|
||||||
':mail' => $mail,
|
|
||||||
':password' => $password,
|
|
||||||
':id_admin' => $id_admin
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
/* [2] On retourne l'etat de la modification
|
||||||
/* [3] On retourne si tout a ete modifie, si erreur
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$check_admin = self::getById($id_admin);
|
return $updated; // FAUX si @username ou @mail déja pris ou autre erreur
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,13 +139,13 @@
|
||||||
public static function delete($id_admin){
|
public static function delete($id_admin){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$delete_admin = Database::getPDO()->prepare("DELETE FROM admin WHERE id_admin = :id_admin");
|
$deleted = Table::get('admin')
|
||||||
$delete_admin->execute([ ':id_admin' => $id_admin ]);
|
->whereId($id_admin)
|
||||||
|
->delete();
|
||||||
|
|
||||||
/* [2] On verifie que l'utilisateur n'existe plus
|
/* [2] On verifie que l'utilisateur n'existe plus
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
return ( self::getById($id_admin) === false );
|
return $deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -203,24 +167,15 @@
|
||||||
public static function getById($id_warehouse, $id_admin){
|
public static function getById($id_warehouse, $id_admin){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
$admin = Table::get('admin')
|
||||||
FROM admin
|
->whereId($id_admin)
|
||||||
WHERE id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND id_admin = :id_admin");
|
->unique()
|
||||||
$get_admin->execute([
|
->fetch();
|
||||||
':id_warehouse' => $id_warehouse,
|
|
||||||
':id_admin' => $id_admin
|
|
||||||
]);
|
|
||||||
|
|
||||||
$fetched = $get_admin->fetch();
|
/* [2] On retourne le résultat
|
||||||
|
|
||||||
/* [2] On vérifie qu'on a trouvé qqch
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( $fetched === false )
|
return $admin;
|
||||||
return false;
|
|
||||||
|
|
||||||
// On retourne le résultat
|
|
||||||
return Database::delNumeric( $fetched, true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -242,24 +197,15 @@
|
||||||
public static function getByUsername($id_warehouse, $username){
|
public static function getByUsername($id_warehouse, $username){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
$admin = Table::get('admin')
|
||||||
FROM admin
|
->whereUsername($username)
|
||||||
WHERE id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND username = :username");
|
->unique()
|
||||||
$get_admin->execute([
|
->fetch();
|
||||||
':id_warehouse' => $id_warehouse,
|
|
||||||
':username' => $username
|
|
||||||
]);
|
|
||||||
|
|
||||||
$fetched = $get_admin->fetch();
|
/* [2] On retourne le résultat
|
||||||
|
|
||||||
/* [2] On vérifie qu'on a trouvé qqch
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( $fetched === false )
|
return $admin;
|
||||||
return false;
|
|
||||||
|
|
||||||
// On retourne le résultat
|
|
||||||
return Database::delNumeric( $fetched, true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -281,24 +227,15 @@
|
||||||
public static function getByMail($id_warehouse, $mail){
|
public static function getByMail($id_warehouse, $mail){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
$admin = Table::get('admin')
|
||||||
FROM admin
|
->whereMail($mail)
|
||||||
WHERE id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND mail = :mail");
|
->unique()
|
||||||
$get_admin->execute([
|
->fetch();
|
||||||
':id_warehouse' => $id_warehouse,
|
|
||||||
':mail' => $mail
|
|
||||||
]);
|
|
||||||
|
|
||||||
$fetched = $get_admin->fetch();
|
/* [2] On retourne le résultat
|
||||||
|
|
||||||
/* [2] On vérifie qu'on a trouvé qqch
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( $fetched === false )
|
return $admin;
|
||||||
return false;
|
|
||||||
|
|
||||||
// On retourne le résultat
|
|
||||||
return Database::delNumeric( $fetched, true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -320,24 +257,15 @@
|
||||||
public static function getByToken($id_warehouse, $token){
|
public static function getByToken($id_warehouse, $token){
|
||||||
/* [1] On redige/execute la requete
|
/* [1] On redige/execute la requete
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
$admin = Table::get('admin')
|
||||||
FROM admin
|
->whereToken($token)
|
||||||
WHERE id_warehouse = :id_warehouse
|
->whereIdWarehouse($id_warehouse)
|
||||||
AND token = :token");
|
->unique()
|
||||||
$get_admin->execute([
|
->fetch();
|
||||||
':id_warehouse' => $id_warehouse,
|
|
||||||
':token' => $token
|
|
||||||
]);
|
|
||||||
|
|
||||||
$fetched = $get_admin->fetch();
|
/* [2] On retourne le résultat
|
||||||
|
|
||||||
/* [2] On vérifie qu'on a trouvé qqch
|
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( $fetched === false )
|
return $admin;
|
||||||
return false;
|
|
||||||
|
|
||||||
// On retourne le résultat
|
|
||||||
return Database::delNumeric( $fetched, true );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue