Repository 'admin' utilise entièrement ORM
This commit is contained in:
parent
9c9ddc1ca9
commit
debc23b72d
|
@ -3,7 +3,8 @@
|
|||
namespace manager\repo;
|
||||
use \manager\Database;
|
||||
use \manager\sessionManager;
|
||||
use \manager\repo\cluster as clusterRepo;
|
||||
use \manager\ORM\Table;
|
||||
use \manager\ORM\Rows;
|
||||
|
||||
class admin extends parentRepo{
|
||||
|
||||
|
@ -20,29 +21,23 @@
|
|||
*
|
||||
*/
|
||||
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;
|
||||
$check_unique = $check_unique && count(self::getByMail($mail)) == 0;
|
||||
|
||||
|
||||
// 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,
|
||||
':password' => sessionManager::secure_hash( $password ),
|
||||
':token' => sessionManager::secure_hash( uniqid() )
|
||||
$inserted = Table::get('admin')
|
||||
->insert([
|
||||
'id_admin' => Rows::DEFAULT,
|
||||
'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);
|
||||
|
||||
|
@ -51,7 +46,10 @@
|
|||
return false;
|
||||
|
||||
// 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){
|
||||
|
||||
/* [1] On vérifie que l'administrateur existe
|
||||
=========================================================*/
|
||||
if( self::getById($id_admin) === false )
|
||||
|
@ -81,20 +78,13 @@
|
|||
/* (1) On crée un nouveau token */
|
||||
$new_token = sessionManager::secure_hash( uniqid() );
|
||||
|
||||
/* (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
|
||||
]);
|
||||
|
||||
$updated = Table::get('admin')
|
||||
->whereId($id_admin)
|
||||
->edit([ 'token' => $new_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){
|
||||
/* [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)
|
||||
// pour éviter les collisions (car l'identifiant et le mail doivent être uniques)
|
||||
$getbyuname = self::getByUsername($username);
|
||||
$getbymail = self::getByMail($mail);
|
||||
|
||||
$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);
|
||||
|
||||
// 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
|
||||
$updated = Table::get('admin')
|
||||
->whereId($id_admin)
|
||||
->edit([
|
||||
'username' => $username,
|
||||
'mail' => $mail,
|
||||
'password' => $password
|
||||
]);
|
||||
|
||||
|
||||
|
||||
/* [3] On retourne si tout a ete modifie, si erreur
|
||||
/* [2] On retourne l'etat de la modification
|
||||
=========================================================*/
|
||||
$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;
|
||||
return $updated; // FAUX si @username ou @mail déja pris ou autre erreur
|
||||
}
|
||||
|
||||
|
||||
|
@ -175,13 +139,13 @@
|
|||
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 ]);
|
||||
|
||||
$deleted = Table::get('admin')
|
||||
->whereId($id_admin)
|
||||
->delete();
|
||||
|
||||
/* [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){
|
||||
/* [1] On redige/execute la requete
|
||||
=========================================================*/
|
||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
||||
FROM admin
|
||||
WHERE id_warehouse = :id_warehouse
|
||||
AND id_admin = :id_admin");
|
||||
$get_admin->execute([
|
||||
':id_warehouse' => $id_warehouse,
|
||||
':id_admin' => $id_admin
|
||||
]);
|
||||
$admin = Table::get('admin')
|
||||
->whereId($id_admin)
|
||||
->whereIdWarehouse($id_warehouse)
|
||||
->unique()
|
||||
->fetch();
|
||||
|
||||
$fetched = $get_admin->fetch();
|
||||
|
||||
/* [2] On vérifie qu'on a trouvé qqch
|
||||
/* [2] On retourne le résultat
|
||||
=========================================================*/
|
||||
if( $fetched === false )
|
||||
return false;
|
||||
|
||||
// On retourne le résultat
|
||||
return Database::delNumeric( $fetched, true );
|
||||
return $admin;
|
||||
}
|
||||
|
||||
|
||||
|
@ -242,24 +197,15 @@
|
|||
public static function getByUsername($id_warehouse, $username){
|
||||
/* [1] On redige/execute la requete
|
||||
=========================================================*/
|
||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
||||
FROM admin
|
||||
WHERE id_warehouse = :id_warehouse
|
||||
AND username = :username");
|
||||
$get_admin->execute([
|
||||
':id_warehouse' => $id_warehouse,
|
||||
':username' => $username
|
||||
]);
|
||||
$admin = Table::get('admin')
|
||||
->whereUsername($username)
|
||||
->whereIdWarehouse($id_warehouse)
|
||||
->unique()
|
||||
->fetch();
|
||||
|
||||
$fetched = $get_admin->fetch();
|
||||
|
||||
/* [2] On vérifie qu'on a trouvé qqch
|
||||
/* [2] On retourne le résultat
|
||||
=========================================================*/
|
||||
if( $fetched === false )
|
||||
return false;
|
||||
|
||||
// On retourne le résultat
|
||||
return Database::delNumeric( $fetched, true );
|
||||
return $admin;
|
||||
}
|
||||
|
||||
|
||||
|
@ -281,24 +227,15 @@
|
|||
public static function getByMail($id_warehouse, $mail){
|
||||
/* [1] On redige/execute la requete
|
||||
=========================================================*/
|
||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
||||
FROM admin
|
||||
WHERE id_warehouse = :id_warehouse
|
||||
AND mail = :mail");
|
||||
$get_admin->execute([
|
||||
':id_warehouse' => $id_warehouse,
|
||||
':mail' => $mail
|
||||
]);
|
||||
$admin = Table::get('admin')
|
||||
->whereMail($mail)
|
||||
->whereIdWarehouse($id_warehouse)
|
||||
->unique()
|
||||
->fetch();
|
||||
|
||||
$fetched = $get_admin->fetch();
|
||||
|
||||
/* [2] On vérifie qu'on a trouvé qqch
|
||||
/* [2] On retourne le résultat
|
||||
=========================================================*/
|
||||
if( $fetched === false )
|
||||
return false;
|
||||
|
||||
// On retourne le résultat
|
||||
return Database::delNumeric( $fetched, true );
|
||||
return $admin;
|
||||
}
|
||||
|
||||
|
||||
|
@ -320,24 +257,15 @@
|
|||
public static function getByToken($id_warehouse, $token){
|
||||
/* [1] On redige/execute la requete
|
||||
=========================================================*/
|
||||
$get_admin = Database::getPDO()->prepare("SELECT *
|
||||
FROM admin
|
||||
WHERE id_warehouse = :id_warehouse
|
||||
AND token = :token");
|
||||
$get_admin->execute([
|
||||
':id_warehouse' => $id_warehouse,
|
||||
':token' => $token
|
||||
]);
|
||||
$admin = Table::get('admin')
|
||||
->whereToken($token)
|
||||
->whereIdWarehouse($id_warehouse)
|
||||
->unique()
|
||||
->fetch();
|
||||
|
||||
$fetched = $get_admin->fetch();
|
||||
|
||||
/* [2] On vérifie qu'on a trouvé qqch
|
||||
/* [2] On retourne le résultat
|
||||
=========================================================*/
|
||||
if( $fetched === false )
|
||||
return false;
|
||||
|
||||
// On retourne le résultat
|
||||
return Database::delNumeric( $fetched, true );
|
||||
return $admin;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue