2 use bcrypt::{hash, DEFAULT_COST};
3 use diesel::{dsl::*, result::Error, *};
6 schema::local_user::dsl::*,
7 source::local_user::{LocalUser, LocalUserForm},
11 mod safe_settings_type {
12 use crate::ToSafeSettings;
13 use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
25 send_notifications_to_email,
32 impl ToSafeSettings for LocalUser {
33 type SafeSettingsColumns = Columns;
35 /// Includes everything but the hashed password
36 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
47 send_notifications_to_email,
57 pub trait LocalUser_ {
58 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
61 local_user_id: LocalUserId,
63 ) -> Result<LocalUser, Error>;
66 impl LocalUser_ for LocalUser {
67 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
68 let mut edited_user = form.clone();
70 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
71 edited_user.password_encrypted = password_hash;
73 Self::create(&conn, &edited_user)
78 local_user_id: LocalUserId,
80 ) -> Result<Self, Error> {
81 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
83 diesel::update(local_user.find(local_user_id))
85 password_encrypted.eq(password_hash),
86 validator_time.eq(naive_now()),
88 .get_result::<Self>(conn)
92 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
93 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
94 local_user.find(local_user_id).first::<Self>(conn)
96 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
97 diesel::delete(local_user.find(local_user_id)).execute(conn)
99 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
100 insert_into(local_user)
102 .get_result::<Self>(conn)
106 local_user_id: LocalUserId,
107 form: &LocalUserForm,
108 ) -> Result<Self, Error> {
109 diesel::update(local_user.find(local_user_id))
111 .get_result::<Self>(conn)