2019-09-07 15:35:05 +00:00
|
|
|
use super::*;
|
2019-06-03 17:47:12 +00:00
|
|
|
use crate::schema::user_;
|
|
|
|
use crate::schema::user_::dsl::*;
|
2019-09-07 15:35:05 +00:00
|
|
|
use crate::{is_email_regex, Settings};
|
|
|
|
use bcrypt::{hash, DEFAULT_COST};
|
|
|
|
use jsonwebtoken::{decode, encode, Header, TokenData, Validation};
|
2019-02-28 06:02:55 +00:00
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
#[derive(Queryable, Identifiable, PartialEq, Debug)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "user_"]
|
2019-02-28 06:02:55 +00:00
|
|
|
pub struct User_ {
|
|
|
|
pub id: i32,
|
|
|
|
pub name: String,
|
2019-04-03 06:49:32 +00:00
|
|
|
pub fedi_name: String,
|
2019-03-04 16:39:07 +00:00
|
|
|
pub preferred_username: Option<String>,
|
2019-02-28 06:02:55 +00:00
|
|
|
pub password_encrypted: String,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub icon: Option<Vec<u8>>,
|
2019-04-16 23:04:23 +00:00
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
2019-03-05 03:52:09 +00:00
|
|
|
pub published: chrono::NaiveDateTime,
|
2019-08-14 02:52:43 +00:00
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub show_nsfw: bool,
|
2019-10-15 19:21:27 +00:00
|
|
|
pub theme: String,
|
2019-10-21 04:21:54 +00:00
|
|
|
pub default_sort_type: i16,
|
|
|
|
pub default_listing_type: i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
pub lang: String,
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
#[derive(Insertable, AsChangeset, Clone)]
|
2019-09-07 15:35:05 +00:00
|
|
|
#[table_name = "user_"]
|
2019-03-23 01:42:57 +00:00
|
|
|
pub struct UserForm {
|
2019-09-07 15:35:05 +00:00
|
|
|
pub name: String,
|
|
|
|
pub fedi_name: String,
|
|
|
|
pub preferred_username: Option<String>,
|
|
|
|
pub password_encrypted: String,
|
|
|
|
pub admin: bool,
|
|
|
|
pub banned: bool,
|
|
|
|
pub email: Option<String>,
|
|
|
|
pub updated: Option<chrono::NaiveDateTime>,
|
|
|
|
pub show_nsfw: bool,
|
2019-10-15 19:21:27 +00:00
|
|
|
pub theme: String,
|
2019-10-21 04:21:54 +00:00
|
|
|
pub default_sort_type: i16,
|
|
|
|
pub default_listing_type: i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
pub lang: String,
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
impl Crud<UserForm> for User_ {
|
|
|
|
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
|
2019-09-07 15:35:05 +00:00
|
|
|
user_.find(user_id).first::<Self>(conn)
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
|
2019-09-07 15:35:05 +00:00
|
|
|
diesel::delete(user_.find(user_id)).execute(conn)
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
2019-09-07 15:35:05 +00:00
|
|
|
insert_into(user_).values(form).get_result::<Self>(conn)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
|
2019-04-16 23:04:23 +00:00
|
|
|
diesel::update(user_.find(user_id))
|
|
|
|
.set(form)
|
|
|
|
.get_result::<Self>(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User_ {
|
|
|
|
pub fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
|
2019-03-04 16:39:07 +00:00
|
|
|
let mut edited_user = form.clone();
|
2019-09-07 15:35:05 +00:00
|
|
|
let password_hash =
|
|
|
|
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
|
2019-03-23 01:42:57 +00:00
|
|
|
edited_user.password_encrypted = password_hash;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
Self::create(&conn, &edited_user)
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2019-11-02 06:43:21 +00:00
|
|
|
pub fn update_password(
|
|
|
|
conn: &PgConnection,
|
|
|
|
user_id: i32,
|
|
|
|
form: &UserForm,
|
|
|
|
) -> Result<Self, Error> {
|
2019-10-30 03:35:39 +00:00
|
|
|
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::update(&conn, user_id, &edited_user)
|
|
|
|
}
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
pub fn read_from_name(conn: &PgConnection, from_user_name: String) -> Result<Self, Error> {
|
2019-09-07 15:35:05 +00:00
|
|
|
user_.filter(name.eq(from_user_name)).first::<Self>(conn)
|
2019-04-25 21:52:18 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-03-25 03:51:27 +00:00
|
|
|
pub struct Claims {
|
|
|
|
pub id: i32,
|
|
|
|
pub username: String,
|
|
|
|
pub iss: String,
|
2019-08-14 02:52:43 +00:00
|
|
|
pub show_nsfw: bool,
|
2019-10-15 19:21:27 +00:00
|
|
|
pub theme: String,
|
2019-10-21 04:21:54 +00:00
|
|
|
pub default_sort_type: i16,
|
|
|
|
pub default_listing_type: i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
pub lang: String,
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Claims {
|
|
|
|
pub fn decode(jwt: &str) -> Result<TokenData<Claims>, jsonwebtoken::errors::Error> {
|
|
|
|
let v = Validation {
|
|
|
|
validate_exp: false,
|
|
|
|
..Validation::default()
|
|
|
|
};
|
2019-05-02 16:55:29 +00:00
|
|
|
decode::<Claims>(&jwt, Settings::get().jwt_secret.as_ref(), &v)
|
2019-03-25 03:51:27 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Jwt = String;
|
|
|
|
impl User_ {
|
|
|
|
pub fn jwt(&self) -> Jwt {
|
|
|
|
let my_claims = Claims {
|
|
|
|
id: self.id,
|
2019-03-25 03:51:27 +00:00
|
|
|
username: self.name.to_owned(),
|
2019-05-02 16:55:29 +00:00
|
|
|
iss: self.fedi_name.to_owned(),
|
2019-08-14 02:52:43 +00:00
|
|
|
show_nsfw: self.show_nsfw,
|
2019-10-15 19:21:27 +00:00
|
|
|
theme: self.theme.to_owned(),
|
2019-10-21 04:21:54 +00:00
|
|
|
default_sort_type: self.default_sort_type,
|
|
|
|
default_listing_type: self.default_listing_type,
|
2019-12-09 08:24:53 +00:00
|
|
|
lang: self.lang.to_owned(),
|
2019-03-23 01:42:57 +00:00
|
|
|
};
|
2019-09-07 15:35:05 +00:00
|
|
|
encode(
|
|
|
|
&Header::default(),
|
|
|
|
&my_claims,
|
|
|
|
Settings::get().jwt_secret.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 00:59:47 +00:00
|
|
|
pub fn find_by_username(conn: &PgConnection, username: &str) -> Result<Self, Error> {
|
|
|
|
user_.filter(name.eq(username)).first::<User_>(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<Self, Error> {
|
|
|
|
user_.filter(email.eq(from_email)).first::<User_>(conn)
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
pub fn find_by_email_or_username(
|
|
|
|
conn: &PgConnection,
|
|
|
|
username_or_email: &str,
|
|
|
|
) -> Result<Self, Error> {
|
2019-03-23 01:42:57 +00:00
|
|
|
if is_email_regex(username_or_email) {
|
2019-12-18 00:59:47 +00:00
|
|
|
User_::find_by_email(conn, username_or_email)
|
2019-03-23 01:42:57 +00:00
|
|
|
} else {
|
2019-12-18 00:59:47 +00:00
|
|
|
User_::find_by_username(conn, username_or_email)
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2019-12-18 00:59:47 +00:00
|
|
|
pub fn get_user_profile_url(username: &str) -> String {
|
|
|
|
format!("https://{}/u/{}", Settings::get().hostname, username)
|
2019-11-02 06:43:21 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result<Self, Error> {
|
2019-03-25 03:51:27 +00:00
|
|
|
let claims: Claims = Claims::decode(&jwt).expect("Invalid token").claims;
|
|
|
|
Self::read(&conn, claims.id)
|
2019-03-04 16:39:07 +00:00
|
|
|
}
|
2019-02-28 06:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-10-30 03:35:39 +00:00
|
|
|
use super::User_;
|
2019-11-02 06:43:21 +00:00
|
|
|
use super::*;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
#[test]
|
2019-02-28 06:02:55 +00:00
|
|
|
fn test_crud() {
|
|
|
|
let conn = establish_connection();
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2019-03-04 16:39:07 +00:00
|
|
|
let new_user = UserForm {
|
2019-04-03 23:01:20 +00:00
|
|
|
name: "thommy".into(),
|
2019-04-03 06:49:32 +00:00
|
|
|
fedi_name: "rrf".into(),
|
2019-03-04 16:39:07 +00:00
|
|
|
preferred_username: None,
|
2019-02-28 06:02:55 +00:00
|
|
|
password_encrypted: "nope".into(),
|
2019-03-05 03:52:09 +00:00
|
|
|
email: None,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin: false,
|
|
|
|
banned: false,
|
2019-08-14 02:52:43 +00:00
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
2019-10-15 19:21:27 +00:00
|
|
|
theme: "darkly".into(),
|
2019-10-21 04:21:54 +00:00
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
lang: "browser".into(),
|
2019-02-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let inserted_user = User_::create(&conn, &new_user).unwrap();
|
2019-02-28 06:02:55 +00:00
|
|
|
|
|
|
|
let expected_user = User_ {
|
|
|
|
id: inserted_user.id,
|
2019-04-03 23:01:20 +00:00
|
|
|
name: "thommy".into(),
|
2019-04-03 06:49:32 +00:00
|
|
|
fedi_name: "rrf".into(),
|
2019-03-04 16:39:07 +00:00
|
|
|
preferred_username: None,
|
2019-04-16 23:04:23 +00:00
|
|
|
password_encrypted: "nope".into(),
|
2019-02-28 06:02:55 +00:00
|
|
|
email: None,
|
|
|
|
icon: None,
|
2019-04-16 23:04:23 +00:00
|
|
|
admin: false,
|
|
|
|
banned: false,
|
2019-03-05 03:52:09 +00:00
|
|
|
published: inserted_user.published,
|
2019-08-14 02:52:43 +00:00
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
2019-10-15 19:21:27 +00:00
|
|
|
theme: "darkly".into(),
|
2019-10-21 04:21:54 +00:00
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
2019-12-09 08:24:53 +00:00
|
|
|
lang: "browser".into(),
|
2019-02-28 06:02:55 +00:00
|
|
|
};
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
let read_user = User_::read(&conn, inserted_user.id).unwrap();
|
|
|
|
let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
|
|
|
|
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
|
2019-03-04 16:39:07 +00:00
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
assert_eq!(expected_user, read_user);
|
|
|
|
assert_eq!(expected_user, inserted_user);
|
|
|
|
assert_eq!(expected_user, updated_user);
|
2019-02-28 06:02:55 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
|
|
|
}
|
|
|
|
}
|