Add basic support for moderation actions, delete previous user before changing name
parent
36846b2eab
commit
786a4100f2
|
@ -63,7 +63,7 @@ Whenever user sends a message, client will send message & token and backend will
|
||||||
- [x] Basic messaging system
|
- [x] Basic messaging system
|
||||||
- [x] Finish up `chat::create_message()`
|
- [x] Finish up `chat::create_message()`
|
||||||
- [x] Create `chat::fetch_messages()`
|
- [x] Create `chat::fetch_messages()`
|
||||||
- [ ] Use unix timestamp for date
|
- [x] Use unix timestamp for date
|
||||||
- [ ] Create `chat::delete_message()`
|
- [ ] Create `chat::delete_message()`
|
||||||
- [x] Switch to using sled database to store users
|
- [x] Switch to using sled database to store users
|
||||||
- [ ] Error handling
|
- [ ] Error handling
|
||||||
|
|
56
src/auth.rs
56
src/auth.rs
|
@ -1,5 +1,5 @@
|
||||||
extern crate log;
|
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 rocket::http::{Cookie, Cookies};
|
||||||
use crate::user::*;
|
use crate::user::*;
|
||||||
use rocket_contrib::json::{Json, JsonValue};
|
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 input.name.to_lowercase() == users[i].name { // if user found...
|
||||||
if token.value() == users[i].session_token { // & if token matches:
|
if token.value() == users[i].session_token { // & if token matches:
|
||||||
if input.changed_event == "name" {
|
if input.changed_event == "name" {
|
||||||
|
// remove the user first
|
||||||
|
db_remove(&users[i]);
|
||||||
// change the name
|
// change the name
|
||||||
users[i].name = input.new_event.clone();
|
users[i].name = input.new_event.clone();
|
||||||
info!("changed name of {} to {}", input.name, input.new_event);
|
info!("changed name of {} to {}", input.name, input.new_event);
|
||||||
|
@ -405,10 +407,10 @@ pub fn get_user(name: String) -> JsonValue {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
/* User Management */
|
/* User Management */
|
||||||
#[post("/mod", format = "json", data = "<data>")]
|
#[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") {
|
let token = match cookies.get_private("token") {
|
||||||
None => {
|
None => {
|
||||||
warn!("couldn't get token cookie!");
|
warn!("couldn't get token cookie!");
|
||||||
|
@ -419,4 +421,50 @@ pub fn moderation_actions(data: Json<ModerationAction<'_>>, mut cookies: Cookies
|
||||||
},
|
},
|
||||||
Some(token) => token,
|
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"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -116,6 +116,12 @@ pub fn db_write(users_list: &Vec<User>) {
|
||||||
info!("wrote all users to db");
|
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
|
// read all users from the database
|
||||||
pub fn db_read() -> Vec<User> {
|
pub fn db_read() -> Vec<User> {
|
||||||
let db: sled::Db = sled::open("users_db").unwrap();
|
let db: sled::Db = sled::open("users_db").unwrap();
|
||||||
|
@ -127,3 +133,12 @@ pub fn db_read() -> Vec<User> {
|
||||||
}
|
}
|
||||||
return users;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -37,7 +37,8 @@ fn main() {
|
||||||
chat::fetch_messages,
|
chat::fetch_messages,
|
||||||
auth::change_info,
|
auth::change_info,
|
||||||
auth::check_token,
|
auth::check_token,
|
||||||
auth::logout
|
auth::logout,
|
||||||
|
auth::moderation_actions
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount("/", StaticFiles::from("frontend"))
|
.mount("/", StaticFiles::from("frontend"))
|
||||||
|
|
|
@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/* User Data */
|
/* User Data */
|
||||||
// enum of different user types
|
// enum of different user types
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
|
||||||
pub enum UserType {
|
pub enum UserType {
|
||||||
Normal,
|
Normal,
|
||||||
Moderator,
|
Moderator,
|
||||||
|
@ -34,7 +34,7 @@ pub enum ModActions {
|
||||||
pub struct ModerationAction {
|
pub struct ModerationAction {
|
||||||
pub name: String, // name of the moderator
|
pub name: String, // name of the moderator
|
||||||
pub action: ModActions, // what action to take
|
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 */
|
/* Miscellaneous Events */
|
||||||
|
|
Loading…
Reference in New Issue