2 use bcrypt::{hash, DEFAULT_COST};
3 use diesel::{dsl::*, result::Error, *};
6 schema::local_user::dsl::*,
7 source::local_user::{LocalUser, LocalUserForm},
12 mod safe_settings_type {
13 use crate::ToSafeSettings;
14 use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
27 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 {
47 send_notifications_to_email,
54 pub trait LocalUser_ {
55 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
58 local_user_id: LocalUserId,
60 ) -> Result<LocalUser, Error>;
61 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> 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)
89 fn add_admin(conn: &PgConnection, for_person_id: PersonId, added: bool) -> Result<Self, Error> {
90 diesel::update(local_user.filter(person_id.eq(for_person_id)))
92 .get_result::<Self>(conn)
96 impl Crud<LocalUserForm, LocalUserId> for LocalUser {
97 fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
98 local_user.find(local_user_id).first::<Self>(conn)
100 fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
101 diesel::delete(local_user.find(local_user_id)).execute(conn)
103 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
104 insert_into(local_user)
106 .get_result::<Self>(conn)
110 local_user_id: LocalUserId,
111 form: &LocalUserForm,
112 ) -> Result<Self, Error> {
113 diesel::update(local_user.find(local_user_id))
115 .get_result::<Self>(conn)