2016-04-12 12:27:54 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace database\repo;
|
2016-11-05 10:56:03 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \api\core\Checker;
|
2016-04-14 12:59:21 +00:00
|
|
|
use \manager\sessionManager;
|
2016-10-18 13:11:37 +00:00
|
|
|
use \database\repo\parentRepo;
|
2016-04-12 12:27:54 +00:00
|
|
|
|
2016-04-19 11:44:41 +00:00
|
|
|
class user extends parentRepo{
|
2016-04-12 12:27:54 +00:00
|
|
|
|
2016-04-19 11:44:41 +00:00
|
|
|
protected static function table_name(){ static $table_name = 'users'; return $table_name; }
|
2016-04-19 09:14:28 +00:00
|
|
|
|
|
|
|
|
2016-04-18 17:09:25 +00:00
|
|
|
|
2016-04-12 12:27:54 +00:00
|
|
|
/* VERIFICATION DU LOGIN/PASSWORD D'UN UTILISATEUR
|
|
|
|
*
|
|
|
|
* @login<String> Username ou Adresse mail de l'utilisateur
|
|
|
|
* @password<String> Password de l'utilisateur
|
|
|
|
*
|
|
|
|
* @return id_user<int> Retourne l'id de l'utilisateur si ses credentials sont bons
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function login($login, $password){
|
|
|
|
/* [0] Gestion des INPUT
|
|
|
|
=========================================================*/
|
2016-09-12 15:03:48 +00:00
|
|
|
$checker = Checker::run('varchar(3,50)', $login);
|
|
|
|
$checker = $checker && Checker::run('text', $password);
|
2016-04-12 12:27:54 +00:00
|
|
|
|
|
|
|
// Si les parametres sont incorrects, on retourne une erreur
|
|
|
|
if( !$checker ) return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] On verifie son login/password dans la BDD
|
|
|
|
=========================================================*/
|
2016-11-05 10:56:03 +00:00
|
|
|
$check = DatabaseDriver::getPDO()->prepare("SELECT id_user, login
|
2016-04-12 12:27:54 +00:00
|
|
|
FROM users
|
|
|
|
WHERE ( login = :username OR mail = :mail ) AND password = :password");
|
|
|
|
$check->execute(array(
|
|
|
|
':username' => $login,
|
|
|
|
':mail' => $login,
|
2016-04-14 12:59:21 +00:00
|
|
|
':password' => sessionManager::sha1($password)
|
2016-04-12 12:27:54 +00:00
|
|
|
));
|
|
|
|
$result = $check->fetch();
|
|
|
|
|
|
|
|
/* [2] Gestion du retour
|
|
|
|
=========================================================*/
|
|
|
|
// Si on a rien trouve, on retourne une erreur
|
|
|
|
if( $result === false ) return false;
|
|
|
|
|
|
|
|
|
|
|
|
// On retourne le resultat
|
2016-11-05 10:56:03 +00:00
|
|
|
return DatabaseDriver::delNumeric( $result );
|
2016-04-12 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-14 10:45:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
/* CREE UN UTILISATEUR
|
|
|
|
*
|
|
|
|
* @login<String> Login (identifiant) de l'utilisateur
|
|
|
|
* @password<String> Password de l'utilisateur
|
|
|
|
* @mail<String> Adresse mail de l'utilisateur
|
|
|
|
*
|
|
|
|
* @return id_user<int> Renvoie l'id de l'utilisateur cree ou FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
2016-10-24 13:34:18 +00:00
|
|
|
public static function create($login, $password, $mail){
|
2016-04-14 12:59:21 +00:00
|
|
|
/* [0] Verification et formattage des INPUT
|
|
|
|
=========================================================*/
|
2016-09-12 15:24:47 +00:00
|
|
|
$checkInput = Checker::run('hash', $password);
|
2016-09-12 15:03:48 +00:00
|
|
|
$checkInput = $checkInput && Checker::run('varchar(3, 30)', $login);
|
|
|
|
$checkInput = $checkInput && Checker::run('mail', $mail);
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
// Si erreur en entree, on retourne FAUX
|
|
|
|
if( !$checkInput ) return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] On verifie que le login/mail et reference sont uniques
|
|
|
|
=========================================================*/
|
2016-11-05 10:56:03 +00:00
|
|
|
$checkUnique = DatabaseDriver::getPDO()->prepare("SELECT id_user
|
2016-04-14 12:59:21 +00:00
|
|
|
FROM users
|
|
|
|
WHERE login = :login
|
2016-10-24 13:34:18 +00:00
|
|
|
OR mail = :mail ");
|
2016-04-14 12:59:21 +00:00
|
|
|
$checkUnique->execute(array(
|
|
|
|
':login' => $login,
|
2016-10-24 13:34:18 +00:00
|
|
|
':mail' => $mail
|
2016-04-14 12:59:21 +00:00
|
|
|
));
|
|
|
|
|
2016-04-19 09:14:28 +00:00
|
|
|
// Si un utilisateur a le meme LOGIN/MAIL ou REFERENCE qu'un autre, on retourne une erreur
|
|
|
|
if( $checkUnique->fetch() !== false ) return false;
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Creation de l'utilisateur
|
|
|
|
=========================================================*/
|
2016-11-05 10:56:03 +00:00
|
|
|
$create = DatabaseDriver::getPDO()->prepare("INSERT INTO users(id_user, login, password, mail)
|
2016-10-24 13:34:18 +00:00
|
|
|
VALUES(DEFAULT, :login, :password, :mail)");
|
|
|
|
$create->execute(array(
|
|
|
|
':login' => $login,
|
|
|
|
':password' => $password,
|
|
|
|
':mail' => $mail
|
|
|
|
));
|
2016-04-14 12:59:21 +00:00
|
|
|
|
|
|
|
/* [3] Verification de la creation + recuperation id
|
|
|
|
=========================================================*/
|
2016-11-05 10:56:03 +00:00
|
|
|
$checkCreate = DatabaseDriver::getPDO()->prepare("SELECT id_user
|
2016-04-14 12:59:21 +00:00
|
|
|
FROM users
|
|
|
|
WHERE login = :login
|
|
|
|
AND password = :password
|
2016-10-24 13:34:18 +00:00
|
|
|
AND mail = :mail");
|
2016-04-14 12:59:21 +00:00
|
|
|
$checkCreate->execute(array(
|
|
|
|
':login' => $login,
|
|
|
|
':password' => $password,
|
2016-10-24 13:34:18 +00:00
|
|
|
':mail' => $mail
|
2016-04-14 12:59:21 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// On recupere l'id de l'utilisateur
|
|
|
|
$id_user = $checkCreate->fetch();
|
|
|
|
|
|
|
|
// Si erreur, on retourne FALSE
|
|
|
|
if( $id_user === false ) return false;
|
|
|
|
|
|
|
|
/* [4] Gestion du retour
|
|
|
|
=========================================================*/
|
|
|
|
return $id_user['id_user'];
|
2016-04-14 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 16:49:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPRESSION D'UN UTILISATEUR D'ID DONNE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function remove($id_user){
|
|
|
|
/* [1] On effectue la suppression
|
|
|
|
=========================================================*/
|
2016-11-05 10:56:03 +00:00
|
|
|
$getUser = DatabaseDriver::getPDO()->prepare("DELETE FROM users WHERE id_user = :id_user");
|
2016-04-14 16:49:07 +00:00
|
|
|
$getUser->execute(array( ':id_user' => $id_user ));
|
|
|
|
}
|
|
|
|
|
2016-04-12 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-18 17:09:25 +00:00
|
|
|
?>
|