diff --git a/README.md b/README.md index 97d8e3a..2c7b2fb 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Whenever user sends a message, client will send message & token and backend will - [x] Basic messaging system - [x] Finish up `chat::create_message()` - [x] Create `chat::fetch_messages()` - - [ ] Use unix timestamp for date + - [x] Use unix timestamp for date - [ ] Create `chat::delete_message()` - [x] Switch to using sled database to store users - [ ] Error handling diff --git a/src/auth.rs b/src/auth.rs index eef27f3..4a648ee 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,5 +1,5 @@ extern crate log; -use crate::file_io::{db_add, db_write, db_read}; +use crate::file_io::{db_add, db_write, db_read, db_read_user, db_remove}; use rocket::http::{Cookie, Cookies}; use crate::user::*; use rocket_contrib::json::{Json, JsonValue}; @@ -256,6 +256,8 @@ pub fn change_info(input: Json, mut cookies: Cookies) -> JsonValue if input.name.to_lowercase() == users[i].name { // if user found... if token.value() == users[i].session_token { // & if token matches: if input.changed_event == "name" { + // remove the user first + db_remove(&users[i]); // change the name users[i].name = input.new_event.clone(); info!("changed name of {} to {}", input.name, input.new_event); @@ -405,10 +407,10 @@ pub fn get_user(name: String) -> JsonValue { }), } } -/* + /* User Management */ #[post("/mod", format = "json", data = "")] -pub fn moderation_actions(data: Json>, mut cookies: Cookies) -> JsonValue { +pub fn moderation_actions(data: Json, mut cookies: Cookies) -> JsonValue { let token = match cookies.get_private("token") { None => { warn!("couldn't get token cookie!"); @@ -419,4 +421,50 @@ pub fn moderation_actions(data: Json>, mut cookies: Cookies }, Some(token) => token, }; -}*/ + let mut user = db_read_user(&data.name.to_lowercase()); + + let mut users: Vec = Vec::new(); + // loop through vector + for i in &users { + if i.name == data.name.to_lowercase() { // found the user! + if token.value() == "NULL" { // fail if token is NULL + warn!("NULL token!"); + return json!({ + "status": "fail", + "reason": "NULL token", + }); + } else if i.session_token == token.value() { // if token matches + if i.role == UserType::Normal { + match data.action { + ModActions::Kick => { + info!("kicked user {}", data.target) + }, + ModActions::Ban => info!("banned user {}", data.target), + _ => info!("F"), + }; + return json!({ + "status": "ok", + "reason": "completed action", + }); + } else { + warn!("user does not have sufficient permissions to perform that action!"); + return json!({ + "status": "fail", + "reason": "insufficient permissions", + }); + }; + } else { + warn!("token does not match!"); + return json!({ + "status": "fail", + "reason": "token does not match", + }) + }; + }; + }; + warn!("user not found"); + json!({ + "status": "fail", + "reason": "user not found" + }) +} diff --git a/src/file_io.rs b/src/file_io.rs index ab5161b..1460e8e 100644 --- a/src/file_io.rs +++ b/src/file_io.rs @@ -116,6 +116,12 @@ pub fn db_write(users_list: &Vec) { info!("wrote all users to db"); } +// remove a user from the database +pub fn db_remove(user: &User) { + let db: sled::Db = sled::open("users_db").unwrap(); + db.remove(&user.name); +} + // read all users from the database pub fn db_read() -> Vec { let db: sled::Db = sled::open("users_db").unwrap(); @@ -127,3 +133,12 @@ pub fn db_read() -> Vec { } return users; } + +// read one user from the database +pub fn db_read_user(user: &str) -> User { + let db: sled::Db = sled::open("users_db").unwrap(); + let bytes = db.get(user).unwrap().unwrap(); + let read_user: User = bincode::deserialize(&bytes).unwrap(); + info!("read user {} from db", read_user.name); + return read_user; +} diff --git a/src/main.rs b/src/main.rs index 42a9a1c..dd729d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,8 @@ fn main() { chat::fetch_messages, auth::change_info, auth::check_token, - auth::logout + auth::logout, + auth::moderation_actions ], ) .mount("/", StaticFiles::from("frontend")) diff --git a/src/user.rs b/src/user.rs index 5c799c8..a40e6f3 100644 --- a/src/user.rs +++ b/src/user.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; /* User Data */ // enum of different user types -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)] pub enum UserType { Normal, Moderator, @@ -34,7 +34,7 @@ pub enum ModActions { pub struct ModerationAction { pub name: String, // name of the moderator pub action: ModActions, // what action to take - pub target: User, // who to take the action on + pub target: String, // who to take the action on } /* Miscellaneous Events */