lemmy/crates/db_queries/src/source/local_user.rs

147 lines
4.1 KiB
Rust
Raw Normal View History

2021-03-11 04:43:11 +00:00
use crate::{Crud, ToSafeSettings};
2021-03-10 22:33:55 +00:00
use bcrypt::{hash, DEFAULT_COST};
2021-03-11 04:43:11 +00:00
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
schema::local_user::dsl::*,
source::local_user::{LocalUser, LocalUserForm, LocalUserSettings},
};
2021-03-10 22:33:55 +00:00
mod safe_type {
use crate::ToSafe;
use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
2021-03-11 04:43:11 +00:00
type Columns = (id, person_id, admin, matrix_user_id);
2021-03-10 22:33:55 +00:00
impl ToSafe for LocalUser {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
2021-03-11 04:43:11 +00:00
(id, person_id, admin, matrix_user_id)
2021-03-10 22:33:55 +00:00
}
}
}
2021-02-26 13:49:58 +00:00
mod safe_settings_type {
use crate::ToSafeSettings;
2021-03-10 22:33:55 +00:00
use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
2021-02-26 13:49:58 +00:00
type Columns = (
id,
2021-03-10 22:33:55 +00:00
person_id,
2021-02-26 13:49:58 +00:00
email,
admin,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
);
2021-03-10 22:33:55 +00:00
impl ToSafeSettings for LocalUser {
2021-02-26 13:49:58 +00:00
type SafeSettingsColumns = Columns;
2021-03-11 04:43:11 +00:00
2021-03-10 22:33:55 +00:00
/// Includes everything but the hashed password
2021-02-26 13:49:58 +00:00
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
(
id,
2021-03-10 22:33:55 +00:00
person_id,
2021-02-26 13:49:58 +00:00
email,
admin,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
)
}
}
}
2021-03-10 22:33:55 +00:00
pub trait LocalUser_ {
fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
2021-03-11 04:43:11 +00:00
fn update_password(
conn: &PgConnection,
local_user_id: i32,
new_password: &str,
) -> Result<LocalUser, Error>;
2021-03-10 22:33:55 +00:00
fn add_admin(conn: &PgConnection, local_user_id: i32, added: bool) -> Result<LocalUser, Error>;
fn find_by_person(conn: &PgConnection, from_person_id: i32) -> Result<LocalUser, Error>;
}
impl LocalUser_ for LocalUser {
fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
let mut edited_user = form.clone();
let password_hash =
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
edited_user.password_encrypted = password_hash;
Self::create(&conn, &edited_user)
}
// TODO do more individual updates like these
2021-03-11 04:43:11 +00:00
fn update_password(
conn: &PgConnection,
local_user_id: i32,
new_password: &str,
) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
diesel::update(local_user.find(local_user_id))
2021-03-11 04:43:11 +00:00
.set((password_encrypted.eq(password_hash),))
2021-03-10 22:33:55 +00:00
.get_result::<Self>(conn)
}
// TODO is this used?
fn add_admin(conn: &PgConnection, local_user_id: i32, added: bool) -> Result<Self, Error> {
diesel::update(local_user.find(local_user_id))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
// TODO is this used?
fn find_by_person(conn: &PgConnection, for_person_id: i32) -> Result<LocalUser, Error> {
local_user
.filter(person_id.eq(for_person_id))
.first::<LocalUser>(conn)
}
}
impl Crud<LocalUserForm> for LocalUser {
fn read(conn: &PgConnection, local_user_id: i32) -> Result<Self, Error> {
2021-03-11 04:43:11 +00:00
local_user.find(local_user_id).first::<Self>(conn)
2021-03-10 22:33:55 +00:00
}
fn delete(conn: &PgConnection, local_user_id: i32) -> Result<usize, Error> {
diesel::delete(local_user.find(local_user_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
2021-03-11 04:43:11 +00:00
insert_into(local_user)
.values(form)
.get_result::<Self>(conn)
2021-03-10 22:33:55 +00:00
}
fn update(conn: &PgConnection, local_user_id: i32, form: &LocalUserForm) -> Result<Self, Error> {
diesel::update(local_user.find(local_user_id))
.set(form)
.get_result::<Self>(conn)
}
}
// TODO is this used?
pub trait LocalUserSettings_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<LocalUserSettings, Error>;
2021-02-26 13:49:58 +00:00
}
2021-03-10 22:33:55 +00:00
// TODO is this used?
impl LocalUserSettings_ for LocalUserSettings {
2021-02-26 13:49:58 +00:00
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
2021-03-10 22:33:55 +00:00
local_user
.select(LocalUser::safe_settings_columns_tuple())
2021-02-26 13:49:58 +00:00
.find(user_id)
.first::<Self>(conn)
}
}