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,
31 impl ToSafeSettings for LocalUser {
32 type SafeSettingsColumns = Columns;
34 /// Includes everything but the hashed password
35 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
46 send_notifications_to_email,
55 pub trait LocalUser_ {
56 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
59 local_user_id: LocalUserId,
61 ) -> Result<LocalUser, Error>;
64 impl LocalUser_ for LocalUser {
65 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
66 let mut edited_user = form.clone();
68 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
69 edited_user.password_encrypted = password_hash;
71 Self::create(&conn, &edited_user)
76 local_user_id: LocalUserId,
78 ) -> Result<Self, Error> {
79 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
81 diesel::update(local_user.find(local_user_id))
83 password_encrypted.eq(password_hash),
84 validator_time.eq(naive_now()),
86 .get_result::<Self>(conn)
90 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
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)