1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-18 23:12:39 +00:00
comunicapiv3/src/helpers/account_helper.rs

324 lines
11 KiB
Rust
Raw Normal View History

use crate::constants::{PASSWORD_RESET_TOKEN_LENGTH, PASSWORD_RESET_TOKEN_LIFETIME};
2020-06-29 15:53:39 +02:00
use crate::constants::database_tables_names::{USER_ACCESS_TOKENS_TABLE, USERS_TABLE};
2020-07-13 19:38:51 +02:00
use crate::data::account_export::AccountExport;
2020-05-23 19:17:48 +02:00
use crate::data::api_client::APIClient;
2020-05-24 16:35:54 +02:00
use crate::data::error::{ExecError, ResultBoxError};
2020-07-14 11:36:15 +02:00
use crate::data::general_settings::GeneralSettings;
2020-07-14 13:33:17 +02:00
use crate::data::lang_settings::LangSettings;
2020-07-13 13:35:25 +02:00
use crate::data::new_account::NewAccount;
2021-01-19 18:48:56 +01:00
use crate::data::security_settings::SecuritySettings;
use crate::data::user::{AccountImageVisibility, UserID, UserPageStatus};
2020-05-24 16:35:54 +02:00
use crate::data::user_token::UserAccessToken;
2020-07-14 08:30:24 +02:00
use crate::helpers::{comments_helper, conversations_helper, database, friends_helper, groups_helper, likes_helper, movies_helper, posts_helper, survey_helper, user_helper};
2020-06-26 08:58:00 +02:00
use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo};
2020-05-24 16:35:54 +02:00
use crate::utils::crypt_utils::{crypt_pass, rand_str};
2020-07-13 13:35:25 +02:00
use crate::utils::date_utils::{mysql_date, time};
2021-01-19 18:27:56 +01:00
use crate::utils::user_data_utils::user_data_path;
2020-05-23 19:17:48 +02:00
/// Account helper
///
/// @author Pierre Hubert
2020-07-13 13:35:25 +02:00
/// 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()
}
2020-05-23 19:17:48 +02:00
/// Attempt to sign-in user
///
/// In this version of the api, we consider that there is only one login token required
/// This is why I returns just a simple string, the token created for the user in case of success
pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxError<String> {
let user = user_helper::find_user_by_email(email)?;
2020-05-24 16:35:54 +02:00
// Validate user password
let password = crypt_pass(password)?;
if !user.password.eq(&password) {
return Err(ExecError::boxed_new("The user gave an invalid password!"));
}
2020-05-23 19:17:48 +02:00
2020-05-24 16:35:54 +02:00
// Check if we already have a login token for this user
2020-06-25 10:08:34 +02:00
if let Ok(token) = get_client_tokens(&user.id, client) {
2020-05-24 16:35:54 +02:00
return Ok(token.token);
}
2020-05-23 19:17:48 +02:00
2020-05-24 16:35:54 +02:00
// Create new login tokens
let new_token = UserAccessToken {
2020-06-25 10:08:34 +02:00
user_id: user.id.clone(),
2020-05-24 16:35:54 +02:00
client_id: client.id,
2020-05-24 17:57:47 +02:00
token: rand_str(150),
2020-05-24 16:35:54 +02:00
};
// Save it
database::insert(
InsertQuery::new(USER_ACCESS_TOKENS_TABLE)
2020-06-25 10:08:34 +02:00
.add_user_id("user_id", &new_token.user_id)
2020-05-24 16:35:54 +02:00
.add_u32("service_id", client.id)
.add_str("token1", &new_token.token)
.add_str("token2", "dummy_data")
)?;
Ok(new_token.token)
}
/// Get user login tokens
2020-06-25 10:08:34 +02:00
fn get_client_tokens(user_id: &UserID, client: &APIClient) -> ResultBoxError<UserAccessToken> {
2020-05-24 16:35:54 +02:00
database::query_row(
QueryInfo::new(USER_ACCESS_TOKENS_TABLE)
2020-06-25 10:08:34 +02:00
.cond_user_id("user_id", user_id)
2020-05-24 16:39:48 +02:00
.cond_u32("service_id", client.id),
2020-05-24 16:35:54 +02:00
|res| {
Ok(UserAccessToken {
2020-06-25 10:08:34 +02:00
user_id: res.get_user_id("user_id")?,
2020-05-24 16:35:54 +02:00
client_id: res.get_int64("service_id")? as u32,
token: res.get_str("token1")?,
})
},
)
2020-05-24 17:57:47 +02:00
}
/// Find a user ID based on login token
pub fn get_user_by_login_token(token: &str, client: &APIClient) -> ResultBoxError<UserID> {
database::query_row(
QueryInfo::new(USER_ACCESS_TOKENS_TABLE)
.cond_u32("service_id", client.id)
2020-05-24 18:02:19 +02:00
.cond("token1", token)
.add_field("user_id"),
2020-06-25 10:08:34 +02:00
|res| res.get_user_id("user_id"),
2020-05-24 17:57:47 +02:00
)
2020-05-24 19:19:07 +02:00
}
/// Check out whether an email address exists or not
pub fn exists_mail(mail: &str) -> ResultBoxError<bool> {
database::QueryInfo::new(USERS_TABLE)
.cond("mail", mail)
.exec_count()
.map(|r| r > 0)
}
2020-05-24 19:19:07 +02:00
/// Destroy a given user login tokens
2020-06-25 10:08:34 +02:00
pub fn destroy_login_tokens(id: &UserID, client: &APIClient) -> ResultBoxError<()> {
2020-05-24 19:19:07 +02:00
database::delete(DeleteQuery::new(USER_ACCESS_TOKENS_TABLE)
.cond_u32("service_id", client.id)
2020-06-25 10:08:34 +02:00
.cond_user_id("user_id", id)
2020-05-24 19:19:07 +02:00
)?;
Ok(())
2020-06-26 08:58:00 +02:00
}
/// Destroy all login tokens of a user
pub fn destroy_all_user_tokens(id: &UserID) -> ResultBoxError {
database::DeleteQuery::new(USER_ACCESS_TOKENS_TABLE)
.cond_user_id("user_id", id)
.exec()
}
/// Generate a new password reset token
pub fn generate_password_reset_token(user_id: &UserID) -> ResultBoxError<String> {
let token = rand_str(PASSWORD_RESET_TOKEN_LENGTH);
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("password_reset_token", &token)
.set_u64("password_reset_token_time_create", time())
.exec()?;
Ok(token)
}
2020-07-13 15:33:18 +02:00
/// Remove password reset token for a given user
pub fn destroy_password_reset_token_for_user(user_id: &UserID) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("password_reset_token", "")
.set_u64("password_reset_token_time_create", 0)
.exec()
}
/// Get the ID of a user based on a password reset token
pub fn get_user_id_from_password_reset_token(token: &str) -> ResultBoxError<UserID> {
database::QueryInfo::new(USERS_TABLE)
.cond("password_reset_token", token)
.set_custom_where("password_reset_token_time_create > ?")
.add_custom_where_argument_u64(time() - PASSWORD_RESET_TOKEN_LIFETIME)
.query_row(|r| r.get_user_id("ID"))
}
2020-07-13 18:56:36 +02:00
/// Check current user's password
pub fn check_user_password(user_id: &UserID, password: &str) -> ResultBoxError<bool> {
let crypt_pass = crypt_pass(password)?;
database::QueryInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.cond("password", &crypt_pass)
.exec_count()
.map(|r| r > 0)
}
2020-07-13 15:33:18 +02:00
/// Change the password of a user
pub fn change_password(user_id: &UserID, new_password: &String) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("password", &crypt_pass(new_password)?)
.exec()
}
2020-06-26 08:58:00 +02:00
/// Check out whether a virtual directory is taken by a user or not
pub fn check_user_directory_availability(dir: &str, user_id: Option<UserID>) -> ResultBoxError<bool> {
let found_user = user_helper::find_user_by_virtual_directory(dir);
match (found_user, user_id) {
// A user was found, but we did not specify a user
(Ok(_), None) => Ok(false),
// A user was found, and we specified a user ID, we check if the IDs are the same
(Ok(user), Some(id)) => Ok(user.id == id),
// No user was found, virtual directory is considered as available
(Err(_), _) => Ok(true)
}
2020-06-29 15:53:39 +02:00
}
/// Update the last activity of a user
pub fn update_last_activity(user_id: &UserID) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_u64("last_activity", time())
.exec()
2020-07-13 19:12:39 +02:00
}
2020-07-14 11:36:15 +02:00
/// Set new general settings of an account
pub fn set_general(settings: &GeneralSettings) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", &settings.id)
.set_str("prenom", &settings.first_name)
.set_str("nom", &settings.last_name)
.set_legacy_bool("public", settings.page_status != UserPageStatus::PRIVATE)
.set_legacy_bool("pageouverte", settings.page_status == UserPageStatus::OPEN)
.set_legacy_bool("bloquecommentaire", settings.block_comments)
.set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends)
.set_legacy_bool("autorise_mail", settings.allow_mails)
.set_legacy_bool("liste_amis_publique", settings.friends_list_public)
.set_opt_str("sous_repertoire", settings.virtual_directory.clone())
.set_opt_str("site_web", settings.personal_website.clone())
.set_opt_str("public_note", settings.public_note.clone())
.exec()
}
2020-07-14 13:33:17 +02:00
/// Set new language settings
pub fn set_language_settings(settings: &LangSettings) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", &settings.id)
.set_str("lang", &settings.lang)
.exec()
}
2021-01-19 17:47:48 +01:00
/// Set new security settings
pub fn set_security_settings(settings: &SecuritySettings) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", &settings.id)
.set_opt_str("question1", settings.question1.question())
.set_opt_str("reponse1", settings.question1.answer())
.set_opt_str("question2", settings.question2.question())
.set_opt_str("reponse2", settings.question2.answer())
.exec()
}
2021-01-19 18:27:56 +01:00
/// Delete user account image
pub fn delete_account_image(user_id: &UserID) -> ResultBoxError {
let user = user_helper::find_user_by_id(user_id)?;
if !user.has_account_image() {
return Ok(());
}
let path = user_data_path(user.account_image_path.unwrap().as_ref());
if path.exists()
{
std::fs::remove_file(path)?;
}
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("account_image_path", "")
.exec()
}
/// Set a new account image
pub fn set_account_image(user_id: &UserID, uri: &String) -> ResultBoxError {
// First, delete the previous account image
delete_account_image(user_id)?;
// Update database
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("account_image_path", uri)
.exec()
}
2021-01-19 18:48:56 +01:00
/// Set account image visibility level
pub fn set_account_image_visibility(user_id: &UserID, level: AccountImageVisibility) -> ResultBoxError {
database::UpdateInfo::new(USERS_TABLE)
.cond_user_id("ID", user_id)
.set_str("account_image_visibility", &level.to_db())
.exec()
}
2020-07-13 19:12:39 +02:00
/// Export an account's data
pub fn export(user_id: &UserID) -> ResultBoxError<AccountExport> {
2020-07-14 08:07:55 +02:00
let mut data = AccountExport {
2020-07-13 19:12:39 +02:00
user: user_helper::find_user_by_id(user_id)?,
posts: posts_helper::export_all_posts_user(user_id)?,
2020-07-13 19:26:19 +02:00
comments: comments_helper::export_all_user(user_id)?,
2020-07-13 19:38:51 +02:00
likes: likes_helper::export_all_user(user_id)?,
survey_responses: survey_helper::export_all_user_responses(user_id)?,
2020-07-14 07:48:05 +02:00
movies: movies_helper::get_list_user(user_id)?,
2020-07-14 07:54:40 +02:00
all_conversation_messages: conversations_helper::export_all_user_messages(user_id)?,
2020-07-14 08:07:55 +02:00
conversations: conversations_helper::get_list_user(user_id)?,
conversation_messages: Default::default(),
2020-07-14 08:30:24 +02:00
friends_list: friends_helper::GetFriendsQuery::new(user_id).exec()?,
groups: groups_helper::get_list_user(user_id, false)?,
2020-07-13 19:12:39 +02:00
};
2020-07-14 08:07:55 +02:00
// Process conversation messages
for conv in &data.conversations {
data.conversation_messages
.insert(conv.id, conversations_helper::get_all_messages(conv.id)?);
}
2020-07-13 19:12:39 +02:00
Ok(data)
2021-01-20 18:58:02 +01:00
}
/// Delete a user's account
2021-01-21 18:05:48 +01:00
pub fn delete(user_id: &UserID) -> ResultBoxError {
2021-01-20 18:58:02 +01:00
// TODO : close all websockets of user
2021-01-21 18:05:48 +01:00
// Delete all group membership
groups_helper::delete_all_user_groups(user_id)?;
2021-01-20 18:58:02 +01:00
2021-01-21 18:13:01 +01:00
// Delete all user comments
comments_helper::delete_all_user(user_id)?;
2021-01-21 18:17:38 +01:00
// Delete all user posts
posts_helper::delete_all_user(user_id)?;
// Delete all responses of user to surveys
survey_helper::delete_all_user_responses(user_id)?;
2021-01-21 18:26:18 +01:00
// Delete all the likes created by the user
likes_helper::delete_all_user(user_id)?;
2021-01-20 18:58:02 +01:00
// TODO : continue work here
Ok(())
2020-05-23 19:17:48 +02:00
}