summaryrefslogtreecommitdiffstats
path: root/server/src/db
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2019-10-29 20:35:39 -0700
committerDessalines <tyhou13@gmx.com>2019-10-29 20:35:39 -0700
commit9f35b33dc7238f0d6748beafa79ca0af192b5ca6 (patch)
tree9199b38cc553822d2e43eed2f5d3d7975d2b702a /server/src/db
parent198b5f10dd18244744b6d82b93155a5c5b569bb9 (diff)
Halfway done with email, not fully working yet.
Diffstat (limited to 'server/src/db')
-rw-r--r--server/src/db/mod.rs1
-rw-r--r--server/src/db/password_reset_request.rs108
-rw-r--r--server/src/db/src/schema.rs345
-rw-r--r--server/src/db/user.rs23
4 files changed, 131 insertions, 346 deletions
diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs
index 2045692d..8070041c 100644
--- a/server/src/db/mod.rs
+++ b/server/src/db/mod.rs
@@ -17,6 +17,7 @@ pub mod user;
pub mod user_mention;
pub mod user_mention_view;
pub mod user_view;
+pub mod password_reset_request;
pub trait Crud<T> {
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
diff --git a/server/src/db/password_reset_request.rs b/server/src/db/password_reset_request.rs
new file mode 100644
index 00000000..e9968aa8
--- /dev/null
+++ b/server/src/db/password_reset_request.rs
@@ -0,0 +1,108 @@
+use super::*;
+use crate::schema::password_reset_request;
+use crate::schema::password_reset_request::dsl::*;
+
+use bcrypt::{hash, DEFAULT_COST};
+
+#[derive(Queryable, Identifiable, PartialEq, Debug)]
+#[table_name = "password_reset_request"]
+pub struct PasswordResetRequest {
+ pub id: i32,
+ pub user_id: i32,
+ pub token_encrypted: String,
+ pub published: chrono::NaiveDateTime,
+}
+
+#[derive(Insertable, AsChangeset, Clone)]
+#[table_name = "password_reset_request"]
+pub struct PasswordResetRequestForm {
+ pub user_id: i32,
+ pub token_encrypted: String,
+}
+
+impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
+ fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
+ use crate::schema::password_reset_request::dsl::*;
+ password_reset_request.find(password_reset_request_id).first::<Self>(conn)
+ }
+ fn delete(conn: &PgConnection, password_reset_request_id: i32) -> Result<usize, Error> {
+ diesel::delete(password_reset_request.find(password_reset_request_id)).execute(conn)
+ }
+ fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
+ insert_into(password_reset_request).values(form).get_result::<Self>(conn)
+ }
+ fn update(conn: &PgConnection, password_reset_request_id: i32, form: &PasswordResetRequestForm) -> Result<Self, Error> {
+ diesel::update(password_reset_request.find(password_reset_request_id))
+ .set(form)
+ .get_result::<Self>(conn)
+ }
+}
+
+impl PasswordResetRequest {
+ pub fn create_token(conn: &PgConnection, from_user_id: i32, token: &str) -> Result<Self, Error> {
+ let token_hash =
+ hash(token, DEFAULT_COST).expect("Couldn't hash token");
+
+ let form = PasswordResetRequestForm {
+ user_id: from_user_id,
+ token_encrypted: token_hash,
+ };
+
+ Self::create(&conn, &form)
+ }
+ pub fn read_from_token(conn: &PgConnection, token: &str) -> Result<Self, Error> {
+ let token_hash =
+ hash(token, DEFAULT_COST).expect("Couldn't hash token");
+
+ password_reset_request.filter(token_encrypted.eq(token_hash)).first::<Self>(conn)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use super::super::user::*;
+
+ #[test]
+ fn test_crud() {
+ let conn = establish_connection();
+
+ let new_user = UserForm {
+ name: "thommy prw".into(),
+ fedi_name: "rrf".into(),
+ preferred_username: None,
+ password_encrypted: "nope".into(),
+ email: None,
+ admin: false,
+ banned: false,
+ updated: None,
+ show_nsfw: false,
+ theme: "darkly".into(),
+ default_sort_type: SortType::Hot as i16,
+ default_listing_type: ListingType::Subscribed as i16,
+ };
+
+ let inserted_user = User_::create(&conn, &new_user).unwrap();
+
+ let new_password_reset_request = PasswordResetRequestForm {
+ user_id: inserted_user.id,
+ token_encrypted: "no".into(),
+ };
+
+ let inserted_password_reset_request = PasswordResetRequest::create(&conn, &new_password_reset_request).unwrap();
+
+ let expected_password_reset_request = PasswordResetRequest {
+ id: inserted_password_reset_request.id,
+ user_id: inserted_user.id,
+ token_encrypted: "no".into(),
+ published: inserted_password_reset_request.published,
+ };
+
+ let read_password_reset_request = PasswordResetRequest::read(&conn, inserted_password_reset_request.id).unwrap();
+ let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
+
+ assert_eq!(expected_password_reset_request, read_password_reset_request);
+ assert_eq!(expected_password_reset_request, inserted_password_reset_request);
+ assert_eq!(1, num_deleted);
+ }
+}
diff --git a/server/src/db/src/schema.rs b/server/src/db/src/schema.rs
deleted file mode 100644
index 8693db25..00000000
--- a/server/src/db/src/schema.rs
+++ /dev/null
@@ -1,345 +0,0 @@
-table! {
- category (id) {
- id -> Int4,
- name -> Varchar,
- }
-}
-
-table! {
- comment (id) {
- id -> Int4,
- creator_id -> Int4,
- post_id -> Int4,
- parent_id -> Nullable<Int4>,
- content -> Text,
- removed -> Bool,
- read -> Bool,
- published -> Timestamp,
- updated -> Nullable<Timestamp>,
- deleted -> Bool,
- }
-}
-
-table! {
- comment_like (id) {
- id -> Int4,
- user_id -> Int4,
- comment_id -> Int4,
- post_id -> Int4,
- score -> Int2,
- published -> Timestamp,
- }
-}
-
-table! {
- comment_saved (id) {
- id -> Int4,
- comment_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- community (id) {
- id -> Int4,
- name -> Varchar,
- title -> Varchar,
- description -> Nullable<Text>,
- category_id -> Int4,
- creator_id -> Int4,
- removed -> Bool,
- published -> Timestamp,
- updated -> Nullable<Timestamp>,
- deleted -> Bool,
- nsfw -> Bool,
- }
-}
-
-table! {
- community_follower (id) {
- id -> Int4,
- community_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- community_moderator (id) {
- id -> Int4,
- community_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- community_user_ban (id) {
- id -> Int4,
- community_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- mod_add (id) {
- id -> Int4,
- mod_user_id -> Int4,
- other_user_id -> Int4,
- removed -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_add_community (id) {
- id -> Int4,
- mod_user_id -> Int4,
- other_user_id -> Int4,
- community_id -> Int4,
- removed -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_ban (id) {
- id -> Int4,
- mod_user_id -> Int4,
- other_user_id -> Int4,
- reason -> Nullable<Text>,
- banned -> Nullable<Bool>,
- expires -> Nullable<Timestamp>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_ban_from_community (id) {
- id -> Int4,
- mod_user_id -> Int4,
- other_user_id -> Int4,
- community_id -> Int4,
- reason -> Nullable<Text>,
- banned -> Nullable<Bool>,
- expires -> Nullable<Timestamp>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_lock_post (id) {
- id -> Int4,
- mod_user_id -> Int4,
- post_id -> Int4,
- locked -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_remove_comment (id) {
- id -> Int4,
- mod_user_id -> Int4,
- comment_id -> Int4,
- reason -> Nullable<Text>,
- removed -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_remove_community (id) {
- id -> Int4,
- mod_user_id -> Int4,
- community_id -> Int4,
- reason -> Nullable<Text>,
- removed -> Nullable<Bool>,
- expires -> Nullable<Timestamp>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_remove_post (id) {
- id -> Int4,
- mod_user_id -> Int4,
- post_id -> Int4,
- reason -> Nullable<Text>,
- removed -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- mod_sticky_post (id) {
- id -> Int4,
- mod_user_id -> Int4,
- post_id -> Int4,
- stickied -> Nullable<Bool>,
- when_ -> Timestamp,
- }
-}
-
-table! {
- post (id) {
- id -> Int4,
- name -> Varchar,
- url -> Nullable<Text>,
- body -> Nullable<Text>,
- creator_id -> Int4,
- community_id -> Int4,
- removed -> Bool,
- locked -> Bool,
- published -> Timestamp,
- updated -> Nullable<Timestamp>,
- deleted -> Bool,
- nsfw -> Bool,
- stickied -> Bool,
- }
-}
-
-table! {
- post_like (id) {
- id -> Int4,
- post_id -> Int4,
- user_id -> Int4,
- score -> Int2,
- published -> Timestamp,
- }
-}
-
-table! {
- post_read (id) {
- id -> Int4,
- post_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- post_saved (id) {
- id -> Int4,
- post_id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- site (id) {
- id -> Int4,
- name -> Varchar,
- description -> Nullable<Text>,
- creator_id -> Int4,
- published -> Timestamp,
- updated -> Nullable<Timestamp>,
- }
-}
-
-table! {
- user_ (id) {
- id -> Int4,
- name -> Varchar,
- fedi_name -> Varchar,
- preferred_username -> Nullable<Varchar>,
- password_encrypted -> Text,
- email -> Nullable<Text>,
- icon -> Nullable<Bytea>,
- admin -> Bool,
- banned -> Bool,
- published -> Timestamp,
- updated -> Nullable<Timestamp>,
- show_nsfw -> Bool,
- theme -> Varchar,
- }
-}
-
-table! {
- user_ban (id) {
- id -> Int4,
- user_id -> Int4,
- published -> Timestamp,
- }
-}
-
-table! {
- user_mention (id) {
- id -> Int4,
- recipient_id -> Int4,
- comment_id -> Int4,
- read -> Bool,
- published -> Timestamp,
- }
-}
-
-joinable!(comment -> post (post_id));
-joinable!(comment -> user_ (creator_id));
-joinable!(comment_like -> comment (comment_id));
-joinable!(comment_like -> post (post_id));
-joinable!(comment_like -> user_ (user_id));
-joinable!(comment_saved -> comment (comment_id));
-joinable!(comment_saved -> user_ (user_id));
-joinable!(community -> category (category_id));
-joinable!(community -> user_ (creator_id));
-joinable!(community_follower -> community (community_id));
-joinable!(community_follower -> user_ (user_id));
-joinable!(community_moderator -> community (community_id));
-joinable!(community_moderator -> user_ (user_id));
-joinable!(community_user_ban -> community (community_id));
-joinable!(community_user_ban -> user_ (user_id));
-joinable!(mod_add_community -> community (community_id));
-joinable!(mod_ban_from_community -> community (community_id));
-joinable!(mod_lock_post -> post (post_id));
-joinable!(mod_lock_post -> user_ (mod_user_id));
-joinable!(mod_remove_comment -> comment (comment_id));
-joinable!(mod_remove_comment -> user_ (mod_user_id));
-joinable!(mod_remove_community -> community (community_id));
-joinable!(mod_remove_community -> user_ (mod_user_id));
-joinable!(mod_remove_post -> post (post_id));
-joinable!(mod_remove_post -> user_ (mod_user_id));
-joinable!(mod_sticky_post -> post (post_id));
-joinable!(mod_sticky_post -> user_ (mod_user_id));
-joinable!(post -> community (community_id));
-joinable!(post -> user_ (creator_id));
-joinable!(post_like -> post (post_id));
-joinable!(post_like -> user_ (user_id));
-joinable!(post_read -> post (post_id));
-joinable!(post_read -> user_ (user_id));
-joinable!(post_saved -> post (post_id));
-joinable!(post_saved -> user_ (user_id));
-joinable!(site -> user_ (creator_id));
-joinable!(user_ban -> user_ (user_id));
-joinable!(user_mention -> comment (comment_id));
-joinable!(user_mention -> user_ (recipient_id));
-
-allow_tables_to_appear_in_same_query!(
- category,
- comment,
- comment_like,
- comment_saved,
- community,
- community_follower,
- community_moderator,
- community_user_ban,
- mod_add,
- mod_add_community,
- mod_ban,
- mod_ban_from_community,
- mod_lock_post,
- mod_remove_comment,
- mod_remove_community,
- mod_remove_post,
- mod_sticky_post,
- post,
- post_like,
- post_read,
- post_saved,
- site,
- user_,
- user_ban,
- user_mention,
-);
diff --git a/server/src/db/user.rs b/server/src/db/user.rs
index a378d3c2..da8e5dc2 100644
--- a/server/src/db/user.rs
+++ b/server/src/db/user.rs
@@ -44,7 +44,6 @@ pub struct UserForm {
impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
- use crate::schema::user_::dsl::*;
user_.find(user_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
@@ -69,6 +68,16 @@ impl User_ {
Self::create(&conn, &edited_user)
}
+
+ pub fn update_password(conn: &PgConnection, user_id: i32, form: &UserForm) -> 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::update(&conn, user_id, &edited_user)
+ }
+
pub fn read_from_name(conn: &PgConnection, from_user_name: String) -> Result<Self, Error> {
user_.filter(name.eq(from_user_name)).first::<Self>(conn)
}
@@ -129,6 +138,16 @@ impl User_ {
.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)
+ }
+
pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result<Self, Error> {
let claims: Claims = Claims::decode(&jwt).expect("Invalid token").claims;
@@ -139,6 +158,8 @@ impl User_ {
#[cfg(test)]
mod tests {
use super::*;
+ use super::User_;
+
#[test]
fn test_crud() {
let conn = establish_connection();