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,
36 impl ToSafeSettings for LocalUser {
37 type SafeSettingsColumns = Columns;
39 /// Includes everything but the hashed password
40 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
51 send_notifications_to_email,
63 pub fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
64 let mut edited_user = form.clone();
66 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
67 edited_user.password_encrypted = password_hash;
69 Self::create(conn, &edited_user)
72 pub fn update_password(
74 local_user_id: LocalUserId,
76 ) -> Result<Self, Error> {
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 impl Crud for LocalUser {
89 type Form = LocalUserForm;
90 type IdType = LocalUserId;
91 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
92 local_user.find(local_user_id).first::<Self>(conn)
94 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
95 diesel::delete(local_user.find(local_user_id)).execute(conn)
97 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
98 insert_into(local_user)
100 .get_result::<Self>(conn)
104 local_user_id: LocalUserId,
105 form: &LocalUserForm,
106 ) -> Result<Self, Error> {
107 diesel::update(local_user.find(local_user_id))
109 .get_result::<Self>(conn)