3 schema::local_user::dsl::*,
5 actor_language::{LocalUserLanguage, SiteLanguage},
6 local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
9 utils::{get_conn, naive_now, DbPool},
11 use bcrypt::{hash, DEFAULT_COST};
12 use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
13 use diesel_async::RunQueryDsl;
15 mod safe_settings_type {
17 schema::local_user::columns::*,
18 source::local_user::LocalUser,
19 traits::ToSafeSettings,
32 send_notifications_to_email,
42 impl ToSafeSettings for LocalUser {
43 type SafeSettingsColumns = Columns;
45 /// Includes everything but the hashed password
46 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
57 send_notifications_to_email,
71 pub async fn update_password(
73 local_user_id: LocalUserId,
75 ) -> Result<Self, Error> {
76 let conn = &mut get_conn(pool).await?;
77 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
79 diesel::update(local_user.find(local_user_id))
81 password_encrypted.eq(password_hash),
82 validator_time.eq(naive_now()),
84 .get_result::<Self>(conn)
88 pub async fn set_all_users_email_verified(pool: &DbPool) -> Result<Vec<Self>, Error> {
89 let conn = &mut get_conn(pool).await?;
90 diesel::update(local_user)
91 .set(email_verified.eq(true))
92 .get_results::<Self>(conn)
96 pub async fn set_all_users_registration_applications_accepted(
98 ) -> Result<Vec<Self>, Error> {
99 let conn = &mut get_conn(pool).await?;
100 diesel::update(local_user)
101 .set(accepted_application.eq(true))
102 .get_results::<Self>(conn)
108 impl Crud for LocalUser {
109 type InsertForm = LocalUserInsertForm;
110 type UpdateForm = LocalUserUpdateForm;
111 type IdType = LocalUserId;
112 async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result<Self, Error> {
113 let conn = &mut get_conn(pool).await?;
114 local_user.find(local_user_id).first::<Self>(conn).await
116 async fn delete(pool: &DbPool, local_user_id: LocalUserId) -> Result<usize, Error> {
117 let conn = &mut get_conn(pool).await?;
118 diesel::delete(local_user.find(local_user_id))
122 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
123 let conn = &mut get_conn(pool).await?;
124 let mut form_with_encrypted_password = form.clone();
126 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
127 form_with_encrypted_password.password_encrypted = password_hash;
129 let local_user_ = insert_into(local_user)
130 .values(form_with_encrypted_password)
131 .get_result::<Self>(conn)
133 .expect("couldnt create local user");
135 let site_languages = SiteLanguage::read_local(pool).await;
136 if let Ok(langs) = site_languages {
137 // if site exists, init user with site languages
138 LocalUserLanguage::update(pool, langs, local_user_.id).await?;
140 // otherwise, init with all languages (this only happens during tests and
141 // for first admin user, which is created before site)
142 LocalUserLanguage::update(pool, vec![], local_user_.id).await?;
149 local_user_id: LocalUserId,
150 form: &Self::UpdateForm,
151 ) -> Result<Self, Error> {
152 let conn = &mut get_conn(pool).await?;
153 diesel::update(local_user.find(local_user_id))
155 .get_result::<Self>(conn)