Add basic support for moderation actions, delete previous user before changing name

pull/5/head
~erin 2021-07-23 07:55:24 -04:00
parent 36846b2eab
commit 786a4100f2
Signed by: erin
GPG Key ID: DA70E064A8C70F44
5 changed files with 72 additions and 8 deletions

View File

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

View File

@ -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<ChangeEvent>, 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 = "<data>")]
pub fn moderation_actions(data: Json<ModerationAction<'_>>, mut cookies: Cookies) -> JsonValue {
pub fn moderation_actions(data: Json<ModerationAction>, 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<ModerationAction<'_>>, mut cookies: Cookies
},
Some(token) => token,
};
}*/
let mut user = db_read_user(&data.name.to_lowercase());
let mut users: Vec<User> = 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"
})
}

View File

@ -116,6 +116,12 @@ pub fn db_write(users_list: &Vec<User>) {
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<User> {
let db: sled::Db = sled::open("users_db").unwrap();
@ -127,3 +133,12 @@ pub fn db_read() -> Vec<User> {
}
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;
}

View File

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

View File

@ -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 */