3 schema::local_user::dsl::*,
5 actor_language::{LocalUserLanguage, SiteLanguage},
6 local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
11 use bcrypt::{hash, DEFAULT_COST};
12 use diesel::{dsl::*, result::Error, *};
14 mod safe_settings_type {
16 schema::local_user::columns::*,
17 source::local_user::LocalUser,
18 traits::ToSafeSettings,
31 send_notifications_to_email,
41 impl ToSafeSettings for LocalUser {
42 type SafeSettingsColumns = Columns;
44 /// Includes everything but the hashed password
45 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
56 send_notifications_to_email,
70 pub fn update_password(
71 conn: &mut PgConnection,
72 local_user_id: LocalUserId,
74 ) -> Result<Self, Error> {
75 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
77 diesel::update(local_user.find(local_user_id))
79 password_encrypted.eq(password_hash),
80 validator_time.eq(naive_now()),
82 .get_result::<Self>(conn)
85 pub fn set_all_users_email_verified(conn: &mut PgConnection) -> Result<Vec<Self>, Error> {
86 diesel::update(local_user)
87 .set(email_verified.eq(true))
88 .get_results::<Self>(conn)
91 pub fn set_all_users_registration_applications_accepted(
92 conn: &mut PgConnection,
93 ) -> Result<Vec<Self>, Error> {
94 diesel::update(local_user)
95 .set(accepted_application.eq(true))
96 .get_results::<Self>(conn)
100 impl Crud for LocalUser {
101 type InsertForm = LocalUserInsertForm;
102 type UpdateForm = LocalUserUpdateForm;
103 type IdType = LocalUserId;
104 fn read(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
105 local_user.find(local_user_id).first::<Self>(conn)
107 fn delete(conn: &mut PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
108 diesel::delete(local_user.find(local_user_id)).execute(conn)
110 fn create(conn: &mut PgConnection, form: &Self::InsertForm) -> Result<Self, Error> {
111 let mut form_with_encrypted_password = form.clone();
113 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
114 form_with_encrypted_password.password_encrypted = password_hash;
116 let local_user_ = insert_into(local_user)
117 .values(form_with_encrypted_password)
118 .get_result::<Self>(conn)?;
120 let site_languages = SiteLanguage::read_local(conn);
121 if let Ok(langs) = site_languages {
122 // if site exists, init user with site languages
123 LocalUserLanguage::update(conn, langs, local_user_.id)?;
125 // otherwise, init with all languages (this only happens during tests and
126 // for first admin user, which is created before site)
127 LocalUserLanguage::update(conn, vec![], local_user_.id)?;
133 conn: &mut PgConnection,
134 local_user_id: LocalUserId,
135 form: &Self::UpdateForm,
136 ) -> Result<Self, Error> {
137 diesel::update(local_user.find(local_user_id))
139 .get_result::<Self>(conn)