3 schema::local_user::dsl::{
11 actor_language::{LocalUserLanguage, SiteLanguage},
12 local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
15 utils::{get_conn, naive_now, DbPool},
17 use bcrypt::{hash, DEFAULT_COST};
18 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
19 use diesel_async::RunQueryDsl;
21 mod safe_settings_type {
23 schema::local_user::columns::{
32 send_notifications_to_email,
42 source::local_user::LocalUser,
43 traits::ToSafeSettings,
56 send_notifications_to_email,
66 impl ToSafeSettings for LocalUser {
67 type SafeSettingsColumns = Columns;
69 /// Includes everything but the hashed password
70 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
81 send_notifications_to_email,
95 pub async fn update_password(
97 local_user_id: LocalUserId,
99 ) -> Result<Self, Error> {
100 let conn = &mut get_conn(pool).await?;
101 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
103 diesel::update(local_user.find(local_user_id))
105 password_encrypted.eq(password_hash),
106 validator_time.eq(naive_now()),
108 .get_result::<Self>(conn)
112 pub async fn set_all_users_email_verified(pool: &DbPool) -> Result<Vec<Self>, Error> {
113 let conn = &mut get_conn(pool).await?;
114 diesel::update(local_user)
115 .set(email_verified.eq(true))
116 .get_results::<Self>(conn)
120 pub async fn set_all_users_registration_applications_accepted(
122 ) -> Result<Vec<Self>, Error> {
123 let conn = &mut get_conn(pool).await?;
124 diesel::update(local_user)
125 .set(accepted_application.eq(true))
126 .get_results::<Self>(conn)
132 impl Crud for LocalUser {
133 type InsertForm = LocalUserInsertForm;
134 type UpdateForm = LocalUserUpdateForm;
135 type IdType = LocalUserId;
136 async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result<Self, Error> {
137 let conn = &mut get_conn(pool).await?;
138 local_user.find(local_user_id).first::<Self>(conn).await
140 async fn delete(pool: &DbPool, local_user_id: LocalUserId) -> Result<usize, Error> {
141 let conn = &mut get_conn(pool).await?;
142 diesel::delete(local_user.find(local_user_id))
146 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
147 let conn = &mut get_conn(pool).await?;
148 let mut form_with_encrypted_password = form.clone();
150 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
151 form_with_encrypted_password.password_encrypted = password_hash;
153 let local_user_ = insert_into(local_user)
154 .values(form_with_encrypted_password)
155 .get_result::<Self>(conn)
157 .expect("couldnt create local user");
159 let site_languages = SiteLanguage::read_local(pool).await;
160 if let Ok(langs) = site_languages {
161 // if site exists, init user with site languages
162 LocalUserLanguage::update(pool, langs, local_user_.id).await?;
164 // otherwise, init with all languages (this only happens during tests and
165 // for first admin user, which is created before site)
166 LocalUserLanguage::update(pool, vec![], local_user_.id).await?;
173 local_user_id: LocalUserId,
174 form: &Self::UpdateForm,
175 ) -> Result<Self, Error> {
176 let conn = &mut get_conn(pool).await?;
177 diesel::update(local_user.find(local_user_id))
179 .get_result::<Self>(conn)