2016-02-11 09:34:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
2016-02-13 17:41:19 +00:00
|
|
|
use \manager\Database;
|
2016-02-14 11:37:19 +00:00
|
|
|
use \manager\repo\cluster as clusterRepo;
|
2016-02-11 09:34:26 +00:00
|
|
|
|
|
|
|
class user{
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
/* CREATION D'UN UTILISATEUR ET DE SON GROUPE INDIVIDUEL
|
|
|
|
*
|
|
|
|
* @code<String> Code RFID de l'utilisateur
|
|
|
|
* @username<String> Identifiant l'utilisateur
|
|
|
|
* @firstname<String> Prenom de l'utilisateur
|
|
|
|
* @lastcode<String> Nom de l'utilisateur
|
|
|
|
* @mail<String> Adresse email de l'utilisateur
|
|
|
|
* @password<String> Mot de passe de l'utilisateur
|
|
|
|
* @status<String> Status de l'utilisateur
|
|
|
|
*
|
2016-02-14 11:37:19 +00:00
|
|
|
* @return id_user<int> Renvoie l'UID de l'utilisateur cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
2016-02-13 17:41:19 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($code, $username, $firstname, $lastname, $mail, $password, $status){
|
2016-02-14 11:37:19 +00:00
|
|
|
/* [1] Verification de l'unicite de code+username
|
2016-02-13 17:41:19 +00:00
|
|
|
=========================================================*/
|
|
|
|
$check_unique = ( self::getByCode($code) == false );
|
|
|
|
$check_unique = $check_unique && ( self::getByUsername($username) == false );
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
// Si un utilisateur a deja ce code ou cet username, on renvoie une erreur
|
|
|
|
if( !$check_unique )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Creation de l'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
$insert_user = Database::getPDO()->prepare("INSERT INTO user(id_user, code, username, firstname, lastname, mail, password, status)
|
|
|
|
VALUES(DEFAULT, :code, :username, :firstname, :lastname, :mail, :password, :status)");
|
|
|
|
$insert_user->execute(array(
|
|
|
|
':code' => $code,
|
|
|
|
':username' => $username,
|
|
|
|
':firstname' => $firstname,
|
|
|
|
':lastname' => $lastname,
|
|
|
|
':mail' => $mail,
|
|
|
|
':password' => $password,
|
|
|
|
':status' => $status
|
|
|
|
));
|
|
|
|
|
|
|
|
/* [3] On retourne l'id_user ou FALSE si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$check_user = self::getByCode($code);
|
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
|
|
if( $check_user === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return $check_user['id_user'];
|
2016-02-13 17:41:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-11 09:34:26 +00:00
|
|
|
|
|
|
|
/* RENVOIE LA LISTE DE TOUS LES UTILISATEURS
|
|
|
|
*
|
|
|
|
* @return utilisateurs<Array> Liste des utilisateurs de la BDD
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll(){
|
|
|
|
// On ecrit la requete
|
2016-02-15 21:01:00 +00:00
|
|
|
$request = Database::getPDO()->query("SELECT * FROM user ORDER BY username");
|
2016-02-11 09:34:26 +00:00
|
|
|
|
|
|
|
// On recupere le resutat
|
|
|
|
$answer = $request->fetchAll();
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
2016-02-13 17:41:19 +00:00
|
|
|
return Database::delNumeric( $answer );
|
2016-02-11 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-11 15:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-11 09:34:26 +00:00
|
|
|
/* RENVOIE L'UTILISATEUR D'ID DONNE
|
|
|
|
*
|
2016-02-11 15:00:41 +00:00
|
|
|
* @id<int> Id de l'utilisateur en question
|
|
|
|
*
|
2016-02-11 09:34:26 +00:00
|
|
|
* @return utilisateur<Array> Renvoie l'utilisateur d'id donne si existe
|
|
|
|
* @return error<Boolean> Retourne FALSE si aucun resultat
|
|
|
|
*
|
|
|
|
*/
|
2016-02-11 15:00:41 +00:00
|
|
|
public static function getById($id){
|
2016-02-11 09:34:26 +00:00
|
|
|
// On ecrit la requete
|
2016-02-13 17:41:19 +00:00
|
|
|
$request = Database::getPDO()->prepare("SELECT * FROM user
|
2016-02-11 09:34:26 +00:00
|
|
|
WHERE id_user = :id");
|
|
|
|
|
|
|
|
// On execute la requete
|
|
|
|
$request->execute(array(
|
|
|
|
':id' => $id
|
|
|
|
));
|
|
|
|
|
|
|
|
// On recupere 1 seul utilisateur
|
|
|
|
$answer = $request->fetch();
|
|
|
|
|
|
|
|
// Gestion d'erreur -> aucun resultat
|
2016-02-14 11:37:19 +00:00
|
|
|
if( $answer === false )
|
2016-02-11 09:34:26 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
2016-02-13 17:41:19 +00:00
|
|
|
return Database::delNumeric( $answer, true );
|
2016-02-11 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-11 15:00:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE L'UTILISATEUR DE CODE DONNE
|
|
|
|
*
|
|
|
|
* @code<String> Code de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return utilisateur<Array> Renvoie l'utilisateur de code donne si existe
|
|
|
|
* @return error<Boolean> Retourne FALSE si aucun resultat
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByCode($code){
|
|
|
|
// On ecrit la requete
|
2016-02-13 17:41:19 +00:00
|
|
|
$request = Database::getPDO()->prepare("SELECT * FROM user
|
2016-02-11 15:00:41 +00:00
|
|
|
WHERE code = :code");
|
|
|
|
|
|
|
|
// On execute la requete
|
|
|
|
$request->execute(array(
|
|
|
|
':code' => $code
|
|
|
|
));
|
|
|
|
|
|
|
|
// On recupere 1 seul utilisateur
|
|
|
|
$answer = $request->fetch();
|
|
|
|
|
|
|
|
// Gestion d'erreur -> aucun resultat
|
2016-02-14 11:37:19 +00:00
|
|
|
if( $answer === false )
|
2016-02-11 15:00:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
2016-02-13 17:41:19 +00:00
|
|
|
return Database::delNumeric( $answer, true );
|
|
|
|
|
|
|
|
|
2016-02-11 15:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
/* RENVOIE L'UTILISATEUR D'USERNAME DONNE
|
2016-02-11 15:00:41 +00:00
|
|
|
*
|
2016-02-13 17:41:19 +00:00
|
|
|
* @username<String> Identifiant de l'utilisateur en question
|
2016-02-11 15:00:41 +00:00
|
|
|
*
|
2016-02-13 17:41:19 +00:00
|
|
|
* @return utilisateur<Array> Renvoie l'utilisateur d'username donne si existe
|
|
|
|
* @return error<Boolean> Retourne FALSE si aucun resultat
|
2016-02-11 15:00:41 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-02-13 17:41:19 +00:00
|
|
|
public static function getByUsername($username){
|
|
|
|
// On ecrit la requete
|
|
|
|
$request = Database::getPDO()->prepare("SELECT * FROM user
|
|
|
|
WHERE username = :username");
|
|
|
|
|
|
|
|
// On execute la requete
|
|
|
|
$request->execute(array(
|
|
|
|
':username' => $username
|
|
|
|
));
|
|
|
|
|
|
|
|
// On recupere 1 seul utilisateur
|
|
|
|
$answer = $request->fetch();
|
|
|
|
|
|
|
|
// Gestion d'erreur -> aucun resultat
|
2016-02-14 11:37:19 +00:00
|
|
|
if( $answer === false )
|
2016-02-13 17:41:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
|
|
|
return Database::delNumeric( $answer, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES GROUPE AUQUELS UN UTILISATEUR DONNE APPARTIENT
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return groups<Array> Renvoie un tableau d'UID des groupes
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getGroups($id_user){
|
|
|
|
/* [1] On redige/execute la requete
|
2016-02-11 15:00:41 +00:00
|
|
|
=========================================================*/
|
2016-02-13 17:41:19 +00:00
|
|
|
$select_groups = Database::getPDO()->prepare("SELECT gm.id_group FROM user as u, group_merge as gm
|
|
|
|
WHERE u.id_user = gm.id_user
|
|
|
|
AND u.id_user = :id_user
|
|
|
|
ORDER BY gm.id_user ASC");
|
|
|
|
$select_groups->execute(array(
|
|
|
|
':id_user' => $id_user
|
|
|
|
));
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
|
|
|
return Database::delNumeric( $select_groups->fetchAll() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
/* RENVOIE LES GROUPES AUQUEL APPARTIENT UN UTILISATEUR DONNE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return clusters<Array> Retourne la liste des groupes auquel appartient l'utilisateur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getClusters($id_user){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$get_clusters = Database::getPDO()->prepare("SELECT c.* FROM cluster as c, cluster_merge as cm
|
|
|
|
WHERE cm.id_cluster = c.id_cluster
|
|
|
|
AND cm.id_entity = :id_user
|
2016-02-14 14:17:26 +00:00
|
|
|
AND cm.class = :class
|
2016-02-14 12:28:35 +00:00
|
|
|
ORDER BY c.id_cluster");
|
|
|
|
$get_clusters->execute(array(
|
2016-02-14 14:17:26 +00:00
|
|
|
':id_user' => $id_user,
|
|
|
|
':class' => clusterRepo::USER_CLASS
|
2016-02-14 12:28:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
/* [2] On retourne la liste des groupes
|
|
|
|
=========================================================*/
|
|
|
|
return Database::delNumeric( $get_clusters->fetchAll() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
/* SUPPRIME UN UTILISATEUR DONNE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'utilisateur a bien ete supprime
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delete($id_user){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete_user = Database::getPDO()->prepare("DELETE FROM user WHERE id_user = :id_user");
|
|
|
|
$delete_user->execute(array(
|
|
|
|
':id_user' => $id_user
|
|
|
|
));
|
2016-02-11 15:00:41 +00:00
|
|
|
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
|
|
=========================================================*/
|
|
|
|
return ( self::getById($id_user) === false );
|
2016-02-11 15:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-13 17:41:19 +00:00
|
|
|
|
|
|
|
|
2016-02-11 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|