2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::LocalUserId,
|
2021-03-11 04:43:11 +00:00
|
|
|
schema::local_user::dsl::*,
|
2022-08-23 19:38:51 +00:00
|
|
|
source::{
|
2022-10-06 18:27:58 +00:00
|
|
|
actor_language::{LocalUserLanguage, SiteLanguage},
|
2022-10-27 09:24:07 +00:00
|
|
|
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
|
2022-08-23 19:38:51 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, naive_now, DbPool},
|
2021-03-11 04:43:11 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use bcrypt::{hash, DEFAULT_COST};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
|
|
|
|
use diesel_async::RunQueryDsl;
|
2021-03-10 22:33:55 +00:00
|
|
|
|
2021-02-26 13:49:58 +00:00
|
|
|
mod safe_settings_type {
|
2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
schema::local_user::columns::*,
|
|
|
|
source::local_user::LocalUser,
|
|
|
|
traits::ToSafeSettings,
|
|
|
|
};
|
2021-02-26 13:49:58 +00:00
|
|
|
|
|
|
|
type Columns = (
|
|
|
|
id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id,
|
2021-02-26 13:49:58 +00:00
|
|
|
email,
|
|
|
|
show_nsfw,
|
|
|
|
theme,
|
|
|
|
default_sort_type,
|
|
|
|
default_listing_type,
|
2022-08-18 19:11:19 +00:00
|
|
|
interface_language,
|
2021-02-26 13:49:58 +00:00
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
2021-03-19 04:31:49 +00:00
|
|
|
validator_time,
|
2021-04-21 21:41:14 +00:00
|
|
|
show_bot_accounts,
|
2021-03-31 10:54:46 +00:00
|
|
|
show_scores,
|
2021-04-24 22:26:50 +00:00
|
|
|
show_read_posts,
|
2021-07-22 20:07:40 +00:00
|
|
|
show_new_post_notifs,
|
2021-12-15 19:49:59 +00:00
|
|
|
email_verified,
|
|
|
|
accepted_application,
|
2021-02-26 13:49:58 +00:00
|
|
|
);
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
impl ToSafeSettings for LocalUser {
|
2021-02-26 13:49:58 +00:00
|
|
|
type SafeSettingsColumns = Columns;
|
2021-03-11 04:43:11 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
/// Includes everything but the hashed password
|
2021-02-26 13:49:58 +00:00
|
|
|
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
|
|
|
|
(
|
|
|
|
id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id,
|
2021-02-26 13:49:58 +00:00
|
|
|
email,
|
|
|
|
show_nsfw,
|
|
|
|
theme,
|
|
|
|
default_sort_type,
|
|
|
|
default_listing_type,
|
2022-08-18 19:11:19 +00:00
|
|
|
interface_language,
|
2021-02-26 13:49:58 +00:00
|
|
|
show_avatars,
|
|
|
|
send_notifications_to_email,
|
2021-03-19 04:31:49 +00:00
|
|
|
validator_time,
|
2021-04-21 21:41:14 +00:00
|
|
|
show_bot_accounts,
|
2021-03-31 10:54:46 +00:00
|
|
|
show_scores,
|
2021-04-24 22:26:50 +00:00
|
|
|
show_read_posts,
|
2021-07-22 20:07:40 +00:00
|
|
|
show_new_post_notifs,
|
2021-12-15 19:49:59 +00:00
|
|
|
email_verified,
|
|
|
|
accepted_application,
|
2021-02-26 13:49:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl LocalUser {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn update_password(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
local_user_id: LocalUserId,
|
2021-03-11 04:43:11 +00:00
|
|
|
new_password: &str,
|
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
|
|
|
|
|
|
|
|
diesel::update(local_user.find(local_user_id))
|
2021-03-19 04:31:49 +00:00
|
|
|
.set((
|
|
|
|
password_encrypted.eq(password_hash),
|
|
|
|
validator_time.eq(naive_now()),
|
|
|
|
))
|
2021-03-10 22:33:55 +00:00
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2021-12-15 19:49:59 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn set_all_users_email_verified(pool: &DbPool) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
diesel::update(local_user)
|
|
|
|
.set(email_verified.eq(true))
|
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn set_all_users_registration_applications_accepted(
|
|
|
|
pool: &DbPool,
|
2021-12-15 19:49:59 +00:00
|
|
|
) -> Result<Vec<Self>, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
diesel::update(local_user)
|
|
|
|
.set(accepted_application.eq(true))
|
|
|
|
.get_results::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-08-17 18:04:58 +00:00
|
|
|
impl Crud for LocalUser {
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm = LocalUserInsertForm;
|
|
|
|
type UpdateForm = LocalUserUpdateForm;
|
2021-08-17 18:04:58 +00:00
|
|
|
type IdType = LocalUserId;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
local_user.find(local_user_id).first::<Self>(conn).await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn delete(pool: &DbPool, local_user_id: LocalUserId) -> Result<usize, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(local_user.find(local_user_id))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
let mut form_with_encrypted_password = form.clone();
|
|
|
|
let password_hash =
|
|
|
|
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
|
|
|
|
form_with_encrypted_password.password_encrypted = password_hash;
|
|
|
|
|
2022-08-23 19:38:51 +00:00
|
|
|
let local_user_ = insert_into(local_user)
|
2022-10-27 09:24:07 +00:00
|
|
|
.values(form_with_encrypted_password)
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_result::<Self>(conn)
|
|
|
|
.await
|
|
|
|
.expect("couldnt create local user");
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let site_languages = SiteLanguage::read_local(pool).await;
|
2022-10-06 18:27:58 +00:00
|
|
|
if let Ok(langs) = site_languages {
|
|
|
|
// if site exists, init user with site languages
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalUserLanguage::update(pool, langs, local_user_.id).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
} else {
|
|
|
|
// otherwise, init with all languages (this only happens during tests and
|
|
|
|
// for first admin user, which is created before site)
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalUserLanguage::update(pool, vec![], local_user_.id).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 19:38:51 +00:00
|
|
|
Ok(local_user_)
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn update(
|
|
|
|
pool: &DbPool,
|
2021-03-18 20:25:21 +00:00
|
|
|
local_user_id: LocalUserId,
|
2022-10-27 09:24:07 +00:00
|
|
|
form: &Self::UpdateForm,
|
2021-03-18 20:25:21 +00:00
|
|
|
) -> Result<Self, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-03-10 22:33:55 +00:00
|
|
|
diesel::update(local_user.find(local_user_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-03-10 22:33:55 +00:00
|
|
|
}
|
|
|
|
}
|