From f3908219cda0bcc41042444a8fbb6c0123670ff5 Mon Sep 17 00:00:00 2001 From: reidlab Date: Sun, 27 Aug 2023 14:09:59 -0700 Subject: [PATCH] more modular + registration done --- diesel.toml | 2 +- migrations/2023-08-26-071607_accounts/up.sql | 2 +- migrations/2023-08-27-090522_users/down.sql | 1 + migrations/2023-08-27-090522_users/up.sql | 41 ++++++++++ readme.md | 7 +- src/db/models.rs | 53 +++++++++++- src/db/schema.rs | 45 ++++++++++- src/endpoints.rs | 1 + src/endpoints/accounts.rs | 1 + src/endpoints/accounts/register_account.rs | 85 ++++++++++++++++++++ src/main.rs | 67 ++------------- 11 files changed, 239 insertions(+), 66 deletions(-) create mode 100644 migrations/2023-08-27-090522_users/down.sql create mode 100644 migrations/2023-08-27-090522_users/up.sql create mode 100644 src/endpoints.rs create mode 100644 src/endpoints/accounts.rs create mode 100644 src/endpoints/accounts/register_account.rs diff --git a/diesel.toml b/diesel.toml index 436f015..fa8cc69 100644 --- a/diesel.toml +++ b/diesel.toml @@ -2,7 +2,7 @@ # see https://diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/server/db/schema.rs" +file = "src/db/schema.rs" custom_type_derives = ["diesel::query_builder::QueryId"] [migrations_directory] diff --git a/migrations/2023-08-26-071607_accounts/up.sql b/migrations/2023-08-26-071607_accounts/up.sql index 4eb423d..f17d4e9 100644 --- a/migrations/2023-08-26-071607_accounts/up.sql +++ b/migrations/2023-08-26-071607_accounts/up.sql @@ -24,5 +24,5 @@ CREATE TABLE accounts ( twitter_url VARCHAR(20), twitch_url VARCHAR(20), - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + created_at TEXT NOT NULL DEFAULT (TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS')) ); \ No newline at end of file diff --git a/migrations/2023-08-27-090522_users/down.sql b/migrations/2023-08-27-090522_users/down.sql new file mode 100644 index 0000000..441087a --- /dev/null +++ b/migrations/2023-08-27-090522_users/down.sql @@ -0,0 +1 @@ +DROP TABLE users; \ No newline at end of file diff --git a/migrations/2023-08-27-090522_users/up.sql b/migrations/2023-08-27-090522_users/up.sql new file mode 100644 index 0000000..026bb93 --- /dev/null +++ b/migrations/2023-08-27-090522_users/up.sql @@ -0,0 +1,41 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + + -- if `registered`, use account_id, else, use udid + udid TEXT, + account_id INTEGER references accounts(id), + registered INTEGER NOT NULL, + + username TEXT NOT NULL COLLATE case_insensitive, + + stars INTEGER NOT NULL DEFAULT 0, + demons INTEGER NOT NULL DEFAULT 0, + coins INTEGER NOT NULL DEFAULT 0, + user_coins INTEGER NOT NULL DEFAULT 0, + diamonds INTEGER NOT NULL DEFAULT 0, + orbs INTEGER NOT NULL DEFAULT 0, + creator_points INTEGER NOT NULL DEFAULT 0, + + completed_levels INTEGER NOT NULL DEFAULT 0, + + icon_type INTEGER NOT NULL DEFAULT 0, -- icon to display in comments, etc + color1 INTEGER NOT NULL DEFAULT 0, + color2 INTEGER NOT NULL DEFAULT 3, + cube INTEGER NOT NULL DEFAULT 0, + ship INTEGER NOT NULL DEFAULT 0, + ball INTEGER NOT NULL DEFAULT 0, + ufo INTEGER NOT NULL DEFAULT 0, + wave INTEGER NOT NULL DEFAULT 0, + robot INTEGER NOT NULL DEFAULT 0, + spider INTEGER NOT NULL DEFAULT 0, + swing_copter INTEGER NOT NULL DEFAULT 0, + explosion INTEGER NOT NULL DEFAULT 0, + special INTEGER NOT NULL DEFAULT 0, + glow INTEGER NOT NULL DEFAULT 0, + + created_at TEXT NOT NULL DEFAULT (TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS')), + last_played TEXT NOT NULL DEFAULT (TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS.MS')), + + is_banned INTEGER NOT NULL DEFAULT 0, + is_banned_upload INTEGER NOT NULL DEFAULT 0 +); \ No newline at end of file diff --git a/readme.md b/readme.md index f7fd890..767df3b 100644 --- a/readme.md +++ b/readme.md @@ -28,12 +28,13 @@ _these features are implemented_ ### testing -- run `cargo run ` +- run `cargo run run` ### building -- run `cargo build ` +- run `cargo build` ## todo -- our passwords are a little insecure (`argon2(sha1(password + "mI29fmAnxgTs"))`) and there isnt anything we can do about this because gpj2 is forced like that!! thanks robtop!! \ No newline at end of file +- add login endpoint....... NOW! +- our passwords are a little insecure (`argon2(sha1(password + "mI29fmAnxgTs"))`) and there isnt anything we can do about this because gpj2 is forced like that!! thanks robtop!! (try and find a fix anyway lul) \ No newline at end of file diff --git a/src/db/models.rs b/src/db/models.rs index 296a8eb..239270a 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -1,6 +1,6 @@ use diesel::prelude::*; use serde::{Serialize, Deserialize}; -use super::schema::accounts; +use super::schema::*; #[derive(Queryable, Serialize)] pub struct Account { @@ -30,4 +30,55 @@ pub struct NewAccount { pub username: String, pub gjp2: String, pub email: String +} + +#[derive(Queryable, Serialize)] +pub struct User { + pub id: i32, + + pub udid: Option, + pub account_id: Option, + pub registered: i32, + + pub username: String, + + pub stars: i32, + pub demons: i32, + pub coins: i32, + pub user_coins: i32, + pub diamonds: i32, + pub orbs: i32, + pub creator_points: i32, + + pub completed_levels: i32, + + pub icon_type: i32, + pub color1: i32, + pub color2: i32, + pub cube: i32, + pub ship: i32, + pub ball: i32, + pub ufo: i32, + pub wave: i32, + pub robot: i32, + pub spider: i32, + pub swing_copter: i32, + pub explosion: i32, + pub special: i32, + pub glow: i32, + + pub created_at: String, + pub last_played: String, + + pub is_banned: i32, + pub is_banned_upload: i32 +} + +// TODO: err uhh we might need to make changes because green users 😀😀😀 im gonna commit suicide +#[derive(Insertable, Deserialize)] +#[diesel(table_name = users)] +pub struct NewUser { + pub account_id: i32, + pub username: String, + pub registered: i32 } \ No newline at end of file diff --git a/src/db/schema.rs b/src/db/schema.rs index 14a6569..232b06a 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -18,6 +18,49 @@ diesel::table! { twitter_url -> Nullable, #[max_length = 20] twitch_url -> Nullable, - created_at -> Timestamp, + created_at -> Text, } } + +diesel::table! { + users (id) { + id -> Int4, + udid -> Nullable, + account_id -> Nullable, + registered -> Int4, + username -> Text, + stars -> Int4, + demons -> Int4, + coins -> Int4, + user_coins -> Int4, + diamonds -> Int4, + orbs -> Int4, + creator_points -> Int4, + completed_levels -> Int4, + icon_type -> Int4, + color1 -> Int4, + color2 -> Int4, + cube -> Int4, + ship -> Int4, + ball -> Int4, + ufo -> Int4, + wave -> Int4, + robot -> Int4, + spider -> Int4, + swing_copter -> Int4, + explosion -> Int4, + special -> Int4, + glow -> Int4, + created_at -> Text, + last_played -> Text, + is_banned -> Int4, + is_banned_upload -> Int4, + } +} + +diesel::joinable!(users -> accounts (account_id)); + +diesel::allow_tables_to_appear_in_same_query!( + accounts, + users, +); diff --git a/src/endpoints.rs b/src/endpoints.rs new file mode 100644 index 0000000..5184f86 --- /dev/null +++ b/src/endpoints.rs @@ -0,0 +1 @@ +pub mod accounts; \ No newline at end of file diff --git a/src/endpoints/accounts.rs b/src/endpoints/accounts.rs new file mode 100644 index 0000000..a335f67 --- /dev/null +++ b/src/endpoints/accounts.rs @@ -0,0 +1 @@ +pub mod register_account; \ No newline at end of file diff --git a/src/endpoints/accounts/register_account.rs b/src/endpoints/accounts/register_account.rs new file mode 100644 index 0000000..e4e5898 --- /dev/null +++ b/src/endpoints/accounts/register_account.rs @@ -0,0 +1,85 @@ +use rocket::form::Form; +use rocket::http::Status; +use rocket::response::status; + +use diesel::prelude::*; +use diesel::result::Error; + +use crate::helpers; +use crate::db; + +#[derive(FromForm)] +pub struct FormRegisterAccount { + userName: String, + password: String, + email: String +} + +#[post("/memaddrefix/accounts/registerGJAccount.php", data = "")] +pub fn register_account(input: Form) -> status::Custom<&'static str> { + let connection = &mut db::establish_connection_pg(); + + if input.userName != helpers::clean::clean(input.userName.as_ref()) { + return status::Custom(Status::Ok, "-4") + } + + if input.password.len() < 6 { + return status::Custom(Status::Ok, "-8") + } + + if input.userName.len() < 3 { + return status::Custom(Status::Ok, "-9") + } + + if input.userName.len() > 20 { + return status::Custom(Status::Ok, "-4") + } + + if input.email.len() > 254 { + return status::Custom(Status::Ok, "-6") + } + + // account management + use crate::models::{Account, NewAccount}; + + let inserted_account: Account; + + { + use crate::schema::accounts::dsl::*; + + let account_name_usage = accounts.filter(username.eq(input.userName.clone())).count().get_result::(connection) as Result; + let account_name_used = account_name_usage.expect("Fatal database name query error") != 0; + if account_name_used { + return status::Custom(Status::Ok, "-2") + } + + let new_account = NewAccount { + username: input.userName.clone(), + gjp2: helpers::gjp2::get_gjp2_hashed(input.password.clone()), + email: input.email.clone() + }; + inserted_account = diesel::insert_into(accounts) + .values(&new_account) + .get_result::(connection) + .expect("Fatal error saving the new account"); + } + + // user management + use crate::models::{User, NewUser}; + + { + use crate::schema::users::dsl::*; + + let new_user = NewUser { + account_id: inserted_account.id, + username: input.userName.clone(), + registered: 1 + }; + diesel::insert_into(users) + .values(&new_user) + .get_result::(connection) + .expect("Fatal error saving the new user"); + } + + return status::Custom(Status::Ok, "1") +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 38dac15..fead183 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,6 @@ #![feature(decl_macro)] #[macro_use] extern crate rocket; -use rocket::form::Form; -use rocket::http::Status; -use rocket::response::status; - -use diesel::prelude::*; -use diesel::result::Error; mod db; use db::*; @@ -14,64 +8,19 @@ use db::*; mod helpers; use helpers::*; +mod endpoints; +use endpoints::*; + #[get("/")] fn index() -> String { return String::from("index | coming soon to a localhost:8000 near u"); } -#[derive(FromForm)] -struct FormRegisterGJAccount { - userName: String, - password: String, - email: String -} -#[post("/memaddrefix/accounts/registerGJAccount.php", data = "")] -fn register_gj_account(input: Form) -> status::Custom<&'static str> { - use crate::schema::accounts::dsl::*; - use crate::models::NewAccount; - - let connection = &mut establish_connection_pg(); - - if input.userName != clean::clean(input.userName.as_ref()) { - return status::Custom(Status::Ok, "-4") - } - - if input.password.len() < 6 { - return status::Custom(Status::Ok, "-8") - } - - if input.userName.len() < 3 { - return status::Custom(Status::Ok, "-9") - } - - if input.userName.len() > 20 { - return status::Custom(Status::Ok, "-4") - } - - if input.userName.len() > 254 { - return status::Custom(Status::Ok, "-6") - } - - let account_name_usage = accounts.filter(username.eq(input.userName.clone())).count().get_result::(connection) as Result; - let account_name_used = account_name_usage.expect("Fatal database name query error") != 0; - if account_name_used { - return status::Custom(Status::Ok, "-2") - } - - let new_account = NewAccount { - username: input.userName.clone(), - gjp2: helpers::gjp2::get_gjp2_hashed(input.password.clone()), - email: input.email.clone() - }; - diesel::insert_into(accounts) - .values(&new_account) - .execute(connection) - .expect("Fatal error saving the new account"); - - return status::Custom(Status::Ok, "1") -} - #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![index, register_gj_account]) + rocket::build().mount("/", routes![ + index, + + endpoints::accounts::register_account::register_account + ]) } \ No newline at end of file