more modular + registration done

This commit is contained in:
Reid 2023-08-27 14:09:59 -07:00
parent 4e2318b852
commit f3908219cd
Signed by: reidlab
GPG key ID: 6C9EAA3364F962C8
11 changed files with 239 additions and 66 deletions

View file

@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli # see https://diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/server/db/schema.rs" file = "src/db/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"] custom_type_derives = ["diesel::query_builder::QueryId"]
[migrations_directory] [migrations_directory]

View file

@ -24,5 +24,5 @@ CREATE TABLE accounts (
twitter_url VARCHAR(20), twitter_url VARCHAR(20),
twitch_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'))
); );

View file

@ -0,0 +1 @@
DROP TABLE users;

View file

@ -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
);

View file

@ -28,12 +28,13 @@ _these features are implemented_
### testing ### testing
- run `cargo run <client|server>` - run `cargo run run`
### building ### building
- run `cargo build <client|server>` - run `cargo build`
## todo ## 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!! - 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)

View file

@ -1,6 +1,6 @@
use diesel::prelude::*; use diesel::prelude::*;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use super::schema::accounts; use super::schema::*;
#[derive(Queryable, Serialize)] #[derive(Queryable, Serialize)]
pub struct Account { pub struct Account {
@ -31,3 +31,54 @@ pub struct NewAccount {
pub gjp2: String, pub gjp2: String,
pub email: String pub email: String
} }
#[derive(Queryable, Serialize)]
pub struct User {
pub id: i32,
pub udid: Option<String>,
pub account_id: Option<i32>,
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
}

View file

@ -18,6 +18,49 @@ diesel::table! {
twitter_url -> Nullable<Varchar>, twitter_url -> Nullable<Varchar>,
#[max_length = 20] #[max_length = 20]
twitch_url -> Nullable<Varchar>, twitch_url -> Nullable<Varchar>,
created_at -> Timestamp, created_at -> Text,
} }
} }
diesel::table! {
users (id) {
id -> Int4,
udid -> Nullable<Text>,
account_id -> Nullable<Int4>,
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,
);

1
src/endpoints.rs Normal file
View file

@ -0,0 +1 @@
pub mod accounts;

View file

@ -0,0 +1 @@
pub mod register_account;

View file

@ -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 = "<input>")]
pub fn register_account(input: Form<FormRegisterAccount>) -> 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::<i64>(connection) as Result<i64, Error>;
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::<Account, >(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::<User, >(connection)
.expect("Fatal error saving the new user");
}
return status::Custom(Status::Ok, "1")
}

View file

@ -1,12 +1,6 @@
#![feature(decl_macro)] #![feature(decl_macro)]
#[macro_use] extern crate rocket; #[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; mod db;
use db::*; use db::*;
@ -14,64 +8,19 @@ use db::*;
mod helpers; mod helpers;
use helpers::*; use helpers::*;
mod endpoints;
use endpoints::*;
#[get("/")] #[get("/")]
fn index() -> String { fn index() -> String {
return String::from("index | coming soon to a localhost:8000 near u"); 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 = "<input>")]
fn register_gj_account(input: Form<FormRegisterGJAccount>) -> 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::<i64>(connection) as Result<i64, Error>;
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] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
rocket::build().mount("/", routes![index, register_gj_account]) rocket::build().mount("/", routes![
index,
endpoints::accounts::register_account::register_account
])
} }