diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index 8fde8c7..facfe67 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -6,6 +6,7 @@ use crate::api_data::res_get_security_questions::ResGetSecurityQuestions; use crate::controllers::routes::RequestResult; use crate::data::error::ResultBoxError; use crate::data::http_request_handler::HttpRequestHandler; +use crate::data::new_account::NewAccount; use crate::data::user::User; use crate::helpers::{account_helper, user_helper}; @@ -25,6 +26,30 @@ impl HttpRequestHandler { } } +/// Create a new account +pub fn create(r: &mut HttpRequestHandler) -> RequestResult { + // TODO : limit request + + // Get & check email + let email = r.post_email("emailAddress")?; + if account_helper::exists_mail(&email)? { + r.conflict("This email address already belongs to an account!".to_string())?; + } + + let new_account = NewAccount { + first_name: r.post_content("firstName", 3, true)?, + last_name: r.post_content("lastName", 3, true)?, + email, + password: r.post_string_opt("password", 3, true)?, + }; + + account_helper::create(&new_account)?; + + // TODO : limit request + + r.success("Account created!") +} + /// Sign in user pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { let email = request.post_email("userMail")?; diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 1309374..664c6bd 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -67,6 +67,7 @@ pub fn get_routes() -> Vec { Route::get_without_login("/", Box::new(server_controller::main_index)), // Account controller + Route::post("/account/create", Box::new(account_controller::create)), Route::post_without_login("/account/login", Box::new(account_controller::login_user)), Route::post_without_login("/user/connectUSER", Box::new(account_controller::login_user)), Route::post("/account/logout", Box::new(account_controller::logout_user)), diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 4e8dc08..b23cd46 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -137,6 +137,13 @@ impl HttpRequestHandler { Err(Box::new(ExecError::new(&message))) } + /// Conflict (409) + pub fn conflict(&mut self, message: String) -> RequestResult { + self.response = Some(HttpResponse::Conflict().json( + HttpError::new(409, &message))); + Err(Box::new(ExecError::new(&message))) + } + /// If result is not OK, return a bad request pub fn ok_or_bad_request(&mut self, res: ResultBoxError, msg: &str) -> ResultBoxError { match res { @@ -595,12 +602,10 @@ impl HttpRequestHandler { )?; if comment.user_id != self.user_id_or_invalid() { - let post = posts_helper::get_single(comment.post_id)?; if posts_helper::get_access_level(&post, &self.user_id_opt())? == PostAccessLevel::NO_ACCESS { self.forbidden("You are not allowed to access this post information !".to_string())?; } - } Ok(comment) diff --git a/src/data/mod.rs b/src/data/mod.rs index 2c039e1..2921e3e 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -25,4 +25,5 @@ pub mod survey; pub mod comment; pub mod new_survey; pub mod notification; -pub mod user_membership; \ No newline at end of file +pub mod user_membership; +pub mod new_account; \ No newline at end of file diff --git a/src/data/new_account.rs b/src/data/new_account.rs new file mode 100644 index 0000000..8515cd8 --- /dev/null +++ b/src/data/new_account.rs @@ -0,0 +1,10 @@ +//! # New account +//! +//! @author Pierre Hubert + +pub struct NewAccount { + pub first_name: String, + pub last_name: String, + pub email: String, + pub password: String, +} \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 4966a9f..d37e71d 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -1,17 +1,29 @@ use crate::constants::database_tables_names::{USER_ACCESS_TOKENS_TABLE, USERS_TABLE}; use crate::data::api_client::APIClient; use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::new_account::NewAccount; use crate::data::user::UserID; use crate::data::user_token::UserAccessToken; use crate::helpers::{database, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo}; use crate::utils::crypt_utils::{crypt_pass, rand_str}; -use crate::utils::date_utils::time; +use crate::utils::date_utils::{mysql_date, time}; /// Account helper /// /// @author Pierre Hubert +/// Create a new account +pub fn create(new_account: &NewAccount) -> ResultBoxError { + database::InsertQuery::new(USERS_TABLE) + .add_str("nom", &new_account.first_name) + .add_str("prenom", &new_account.last_name) + .add_str("date_creation", &mysql_date()) + .add_str("mail", &new_account.email) + .add_str("password", &crypt_pass(&new_account.password)?) + .insert_drop_result() +} + /// Attempt to sign-in user /// /// In this version of the api, we consider that there is only one login token required