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,
29 impl ToSafeSettings for LocalUser {
30 type SafeSettingsColumns = Columns;
32 /// Includes everything but the hashed password
33 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
44 send_notifications_to_email,
51 pub trait LocalUser_ {
52 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
55 local_user_id: LocalUserId,
57 ) -> Result<LocalUser, Error>;
60 impl LocalUser_ for LocalUser {
61 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
62 let mut edited_user = form.clone();
64 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
65 edited_user.password_encrypted = password_hash;
67 Self::create(&conn, &edited_user)
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)
86 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
87 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
88 local_user.find(local_user_id).first::<Self>(conn)
90 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
91 diesel::delete(local_user.find(local_user_id)).execute(conn)
93 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
94 insert_into(local_user)
96 .get_result::<Self>(conn)
100 local_user_id: LocalUserId,
101 form: &LocalUserForm,
102 ) -> Result<Self, Error> {
103 diesel::update(local_user.find(local_user_id))
105 .get_result::<Self>(conn)