4 schema::local_user::dsl::*,
5 source::local_user::{LocalUser, LocalUserForm},
8 use bcrypt::{hash, DEFAULT_COST};
9 use diesel::{dsl::*, result::Error, *};
11 mod safe_settings_type {
13 schema::local_user::columns::*,
14 source::local_user::LocalUser,
15 traits::ToSafeSettings,
28 send_notifications_to_email,
38 impl ToSafeSettings for LocalUser {
39 type SafeSettingsColumns = Columns;
41 /// Includes everything but the hashed password
42 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
53 send_notifications_to_email,
67 pub fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
68 let mut edited_user = form.clone();
69 let password_hash = form
72 .map(|p| hash(p, DEFAULT_COST).expect("Couldn't hash password"));
73 edited_user.password_encrypted = password_hash;
75 Self::create(conn, &edited_user)
78 pub fn update_password(
80 local_user_id: LocalUserId,
82 ) -> Result<Self, Error> {
83 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
85 diesel::update(local_user.find(local_user_id))
87 password_encrypted.eq(password_hash),
88 validator_time.eq(naive_now()),
90 .get_result::<Self>(conn)
93 pub fn set_all_users_email_verified(conn: &PgConnection) -> Result<Vec<Self>, Error> {
94 diesel::update(local_user)
95 .set(email_verified.eq(true))
96 .get_results::<Self>(conn)
99 pub fn set_all_users_registration_applications_accepted(
101 ) -> Result<Vec<Self>, Error> {
102 diesel::update(local_user)
103 .set(accepted_application.eq(true))
104 .get_results::<Self>(conn)
108 impl Crud for LocalUser {
109 type Form = LocalUserForm;
110 type IdType = LocalUserId;
111 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
112 local_user.find(local_user_id).first::<Self>(conn)
114 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
115 diesel::delete(local_user.find(local_user_id)).execute(conn)
117 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
118 insert_into(local_user)
120 .get_result::<Self>(conn)
124 local_user_id: LocalUserId,
125 form: &LocalUserForm,
126 ) -> Result<Self, Error> {
127 diesel::update(local_user.find(local_user_id))
129 .get_result::<Self>(conn)