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,
30 impl ToSafeSettings for LocalUser {
31 type SafeSettingsColumns = Columns;
33 /// Includes everything but the hashed password
34 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
45 send_notifications_to_email,
53 pub trait LocalUser_ {
54 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
57 local_user_id: LocalUserId,
59 ) -> Result<LocalUser, Error>;
62 impl LocalUser_ for LocalUser {
63 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)
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<LocalUserForm, LocalUserId> for LocalUser {
89 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
90 local_user.find(local_user_id).first::<Self>(conn)
92 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
93 diesel::delete(local_user.find(local_user_id)).execute(conn)
95 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
96 insert_into(local_user)
98 .get_result::<Self>(conn)
102 local_user_id: LocalUserId,
103 form: &LocalUserForm,
104 ) -> Result<Self, Error> {
105 diesel::update(local_user.find(local_user_id))
107 .get_result::<Self>(conn)