2 use bcrypt::{hash, DEFAULT_COST};
3 use diesel::{dsl::*, result::Error, *};
5 schema::local_user::dsl::*,
6 source::local_user::{LocalUser, LocalUserForm},
11 use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
13 type Columns = (id, person_id, admin, matrix_user_id);
15 impl ToSafe for LocalUser {
16 type SafeColumns = Columns;
17 fn safe_columns_tuple() -> Self::SafeColumns {
18 (id, person_id, admin, matrix_user_id)
23 mod safe_settings_type {
24 use crate::ToSafeSettings;
25 use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
38 send_notifications_to_email,
42 impl ToSafeSettings for LocalUser {
43 type SafeSettingsColumns = Columns;
45 /// Includes everything but the hashed password
46 fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
58 send_notifications_to_email,
65 pub trait LocalUser_ {
66 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
71 ) -> Result<LocalUser, Error>;
72 fn add_admin(conn: &PgConnection, person_id: i32, added: bool) -> Result<LocalUser, Error>;
75 impl LocalUser_ for LocalUser {
76 fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
77 let mut edited_user = form.clone();
79 hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
80 edited_user.password_encrypted = password_hash;
82 Self::create(&conn, &edited_user)
89 ) -> Result<Self, Error> {
90 let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
92 diesel::update(local_user.find(local_user_id))
93 .set((password_encrypted.eq(password_hash),))
94 .get_result::<Self>(conn)
97 fn add_admin(conn: &PgConnection, for_person_id: i32, added: bool) -> Result<Self, Error> {
98 diesel::update(local_user.filter(person_id.eq(for_person_id)))
100 .get_result::<Self>(conn)
104 impl Crud<LocalUserForm> for LocalUser {
105 fn read(conn: &PgConnection, local_user_id: i32) -> Result<Self, Error> {
106 local_user.find(local_user_id).first::<Self>(conn)
108 fn delete(conn: &PgConnection, local_user_id: i32) -> Result<usize, Error> {
109 diesel::delete(local_user.find(local_user_id)).execute(conn)
111 fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
112 insert_into(local_user)
114 .get_result::<Self>(conn)
116 fn update(conn: &PgConnection, local_user_id: i32, form: &LocalUserForm) -> Result<Self, Error> {
117 diesel::update(local_user.find(local_user_id))
119 .get_result::<Self>(conn)