From 32ad923e942dded215dd382294833c309dc150ed Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 11 Apr 2018 11:29:25 +0200 Subject: [PATCH] Can create accounts. --- RestControllers/accountController.php | 65 +++++++++++++++++++++++---- classes/components/account.php | 24 ++++++++++ 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/RestControllers/accountController.php b/RestControllers/accountController.php index baba859..2f45768 100644 --- a/RestControllers/accountController.php +++ b/RestControllers/accountController.php @@ -47,15 +47,64 @@ class accountController { * @url POST /user/disconnectUSER * @url POST /account/logout */ - public function disconnectUSER(){ + public function disconnectUSER(){ - user_login_required(); + user_login_required(); - //Try to delete token - if(!CS::get()->components->account->deleteUserLoginToken(userID, APIServiceID)) - throw new RestException(500, "Something went wrong while trying to logout user !"); + //Try to delete token + if(!CS::get()->components->account->deleteUserLoginToken(userID, APIServiceID)) + throw new RestException(500, "Something went wrong while trying to logout user !"); - //Everything is ok - return array("success" => "The user has been disconnected !"); - } + //Everything is ok + return array("success" => "The user has been disconnected !"); + } + + /** + * Create an account + * + * @url POST /account/create + */ + public function createAccount(){ + + //Check post fields existence + if(!check_post_parametres(array("emailAddress", "firstName", "lastName", "password"))) + Rest_fatal_error(400, "Please check given parameters"); + + //Check the first and the last name of the user + $firstName = $_POST["firstName"]; + $lastName = $_POST["lastName"]; + if(strlen($firstName) < 2 || strlen($lastName) < 2) + Rest_fatal_error(400, "Please check the length of the first and the last name"); + + //Check the given email address + $email = $_POST['emailAddress']; + if(!filter_var($email, FILTER_VALIDATE_EMAIL)) + Rest_fatal_error(400, "Specified email address is invalid !"); + + //Check the given password + $password = $_POST["password"]; + if(strlen($password) < 3) + Rest_fatal_error(400, "Please specify a stronger password !"); + + + //Check if the email address is already associated with an account + if(components()->account->exists_email($email)) + Rest_fatal_error(401, "The specified email address is already associated with an account!"); + + //Create new account object + $newAccount = new NewAccount(); + $newAccount->firstName = $firstName; + $newAccount->lastName = $lastName; + $newAccount->email = $email; + $newAccount->password = $password; + + //Try to create the account + if(!components()->account->create($newAccount)) + Rest_fatal_error(500, "An error occured while trying to create the account !"); + + //Success + return array( + "success" => "The account has been created !" + ); + } } \ No newline at end of file diff --git a/classes/components/account.php b/classes/components/account.php index 7117d40..5772c07 100644 --- a/classes/components/account.php +++ b/classes/components/account.php @@ -174,6 +174,30 @@ class Account { } + /** + * Intend to create an account + * + * @param NewAccount $account The new account to create + * @return bool TRUE in case of success / FALSE else + */ + public function create(NewAccount $newAccount) : bool { + + //Crypt password + $password = $this->cryptPassword($newAccount->password); + + //Set the values + $values = array( + "nom" => $newAccount->lastName, + "prenom" => $newAccount->firstName, + "date_creation" => mysql_date(), + "mail" => $newAccount->email, + "password" => $password + ); + + //Try to insert the user in the database + return CS::get()->db->addLine(self::USER_TABLE, $values); + } + /** * Crypt user password *