Add structs and enums in src/auth.rs into seperate file

pull/5/head
~erin 2021-07-22 17:31:18 -04:00
parent d13e5cf8ae
commit 36846b2eab
Signed by: erin
GPG Key ID: DA70E064A8C70F44
2 changed files with 52 additions and 25 deletions

View File

@ -1,7 +1,7 @@
extern crate log;
use crate::file_io::{db_add, db_write, db_read};
use rocket::http::{Cookie, Cookies};
use crate::user::{User, UserType};
use crate::user::*;
use rocket_contrib::json::{Json, JsonValue};
use random_string::generate;
extern crate sha1;
@ -133,12 +133,6 @@ pub fn check_token(name: String, mut cookies: Cookies) -> JsonValue {
});
}
// logout event struct
#[derive(Deserialize, Debug)]
pub struct LogoutEvent {
pub name: String,
}
// Logout API
#[post("/logout", format = "json", data = "<info>")]
pub fn logout(info: Json<LogoutEvent>, mut cookies: Cookies) -> JsonValue {
@ -232,14 +226,6 @@ pub fn check_pin(mut cookies: Cookies, name: String, pin: i32) -> JsonValue {
});
}
#[derive(Deserialize, Debug)]
pub struct ChangeEvent {
pub name: String,
pub pin: String,
pub changed_event: String,
pub new_event: String,
}
// Change info about a user
#[post("/users/change", format = "json", data = "<input>")]
pub fn change_info(input: Json<ChangeEvent>, mut cookies: Cookies) -> JsonValue {
@ -420,12 +406,17 @@ pub fn get_user(name: String) -> JsonValue {
}
}
/*
#[derive(Deserialize, Debug)]
pub struct ModerationAction {
}
/* User Management */
#[post("/mod", format = "json", data = "<data>")]
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!");
return json!({
"status": "fail",
"reason": "could not read cookie",
});
},
Some(token) => token,
};
}*/

View File

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
/* User Data */
// enum of different user types
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum UserType {
Normal,
@ -10,9 +12,43 @@ pub enum UserType {
// Struct to store basic user data
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct User {
pub name: String,
pub pin_hashed: String,
pub pronouns: String,
pub session_token: String,
pub role: UserType,
pub name: String, // unique username
pub pin_hashed: String, // sha1 hash of the pin
pub pronouns: String, // user's pronouns
pub session_token: String, // generated session token
pub role: UserType, // type/role of user
}
/* Moderation Data */
// enum of different moderator actions
#[derive(Deserialize, Debug)]
pub enum ModActions {
Kick, // Log the user out of their current session
Ban, // Remove the user
Demote, // Demote a user to a lower role
Premote, // Premote a user to a higher role
}
// struct to use for json input
#[derive(Deserialize, Debug)]
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
}
/* Miscellaneous Events */
// logout event struct
#[derive(Deserialize, Debug)]
pub struct LogoutEvent {
pub name: String,
}
// change info event struct
#[derive(Deserialize, Debug)]
pub struct ChangeEvent {
pub name: String, // name of the user
pub pin: String, // user's pin
pub changed_event: String, // which event to change
pub new_event: String, // the new value for the event
}