From 49d307a3b6dbd747ac8c26ec1186346c635123d1 Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Thu, 22 Jul 2021 17:17:55 -0400 Subject: [PATCH] Add user roles --- Cargo.lock | 28 ++++++++++++++-------------- README.md | 4 ++-- src/auth.rs | 22 +++++++++++++++++----- src/chat.rs | 3 +-- src/file_io.rs | 22 ---------------------- src/user.rs | 9 ++++++++- 6 files changed, 42 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0845d70..9aef55b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,9 +228,9 @@ dependencies = [ [[package]] name = "crypto-mac" -version = "0.10.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4857fd85a0c34b3c3297875b747c1e02e06b6a0ea32dd892d8192b9ce0813ea6" +checksum = "bff07008ec701e8028e2ceb8f83f0e4274ee62bd2dbdc4fefff2e9a91824081a" dependencies = [ "generic-array", "subtle", @@ -301,9 +301,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "1.4.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77b705829d1e87f762c2df6da140b26af5839e1033aa84aa5f56bb688e4e1bdb" +checksum = "b394ed3d285a429378d3b384b9eb1285267e7df4b166df24b7a6939a04dc392e" dependencies = [ "instant", ] @@ -572,9 +572,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.97" +version = "0.2.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" +checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" [[package]] name = "lock_api" @@ -807,7 +807,7 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pogchat" -version = "0.5.0" +version = "0.5.1" dependencies = [ "bincode", "chrono", @@ -1062,7 +1062,7 @@ checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" dependencies = [ "proc-macro2 1.0.27", "quote 1.0.9", - "syn 1.0.73", + "syn 1.0.74", ] [[package]] @@ -1148,9 +1148,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.73" +version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" dependencies = [ "proc-macro2 1.0.27", "quote 1.0.9", @@ -1178,9 +1178,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.2.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342" +checksum = "848a1e1181b9f6753b5e96a092749e29b11d19ede67dfbbd6c7dc7e0f49b5338" dependencies = [ "tinyvec_macros", ] @@ -1259,9 +1259,9 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "universal-hash" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b2c654932e3e4f9196e69d08fdf7cfd718e1dc6f66b347e6024a0c961402" +checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05" dependencies = [ "generic-array", "subtle", diff --git a/README.md b/README.md index d8f40de..fd2b5cb 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,10 @@ Whenever user sends a message, client will send message & token and backend will - [x] Fail on NULL token - [x] Pronouns - [x] Set pronouns - - [ ] Change pronouns - - [ ] Multiple sets of pronouns + - [x] Change pronouns - [ ] Some form of plural support? - [ ] User management (banning, etc.) + - [ ] User roles (admin, mod, etc.) - [ ] Blacklist words from chat/names - [ ] More advanced chat features - [x] Different types of message events? eg. default, announcement, command diff --git a/src/auth.rs b/src/auth.rs index 5a11db3..37fefee 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,7 +1,7 @@ extern crate log; use crate::file_io::{db_add, db_write, db_read}; -use rocket::http::{Cookie, Cookies, SameSite}; -use crate::user::User; +use rocket::http::{Cookie, Cookies}; +use crate::user::{User, UserType}; use rocket_contrib::json::{Json, JsonValue}; use random_string::generate; extern crate sha1; @@ -40,13 +40,14 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue { } let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string(); // hash the pin - + let new_user: User = User { name: name.to_string().to_lowercase(), - pin_hashed: pin_hashed, + pin_hashed, pronouns: pronouns.to_string().to_lowercase(), session_token: "NULL".to_string(), - }; // append the user to the vec + role: UserType::Normal, + }; /* // append to the json file @@ -409,6 +410,7 @@ pub fn get_user(name: String) -> JsonValue { "user": { "name": user.name, "pronouns": user.pronouns, + "role": user.role, }, }), None => json!({ @@ -417,3 +419,13 @@ pub fn get_user(name: String) -> JsonValue { }), } } +/* +#[derive(Deserialize, Debug)] +pub struct ModerationAction { +} + +/* User Management */ +#[post("/mod", format = "json", data = "")] +pub fn moderation_actions(data: Json>, mut cookies: Cookies) -> JsonValue { + +}*/ diff --git a/src/chat.rs b/src/chat.rs index fcc2d9c..7912a65 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -9,7 +9,6 @@ use rocket_contrib::json::{Json, JsonValue}; use chrono::prelude::*; use uuid::Uuid; use crate::user::User; -use std::time::{Duration, SystemTime}; static MESSAGES: Lazy>> = Lazy::new(|| Mutex::new(Vec::new())); @@ -33,9 +32,9 @@ fn create_message(message: Json, file: &str, user: &User) -> JsonV // create full message object let message_obj: Message = Message { id: Uuid::new_v4(), + event_type, user: user.name.to_owned(), body: message.body.to_string(), - event_type: event_type, created_at: Utc::now(), }; info!("created mesage: {:?}", message_obj); diff --git a/src/file_io.rs b/src/file_io.rs index d173717..ab5161b 100644 --- a/src/file_io.rs +++ b/src/file_io.rs @@ -97,28 +97,6 @@ pub fn write_json(users_list: &Vec) -> Result<()> { Ok(()) } -// test sled funtion -pub fn test_sled() { - // create test user - let user = User { - name: "erin".to_string(), - pin_hashed: "nyaa".to_string(), - pronouns: "she/her".to_string(), - session_token: "NULL".to_string(), - }; - // open database - let db: sled::Db = sled::open("my_db").unwrap(); - let bytes = bincode::serialize(&user).unwrap(); - db.insert(&user.name, bytes).unwrap(); - match db.get(user.name).unwrap() { - Some(bytes) => { - let read_user: User = bincode::deserialize(&bytes).unwrap(); - println!("username: {}, pronouns: {}", read_user.name, read_user.pronouns); - }, - None => (), - } -} - // add a user to the database pub fn db_add(user: &User) { let db: sled::Db = sled::open("users_db").unwrap(); diff --git a/src/user.rs b/src/user.rs index 915bd4a..c886fef 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,11 +1,18 @@ use serde::{Deserialize, Serialize}; +#[derive(Clone, Serialize, Deserialize, Debug)] +pub enum UserType { + Normal, + Moderator, + Admin, +} + // Struct to store basic user data #[derive(Clone, Serialize, Deserialize, Debug)] pub struct User { pub name: String, pub pin_hashed: String, pub pronouns: String, - #[serde(rename = "sessionToken")] pub session_token: String, + pub role: UserType, }