diff options
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | TODO | 8 | ||||
-rw-r--r-- | src/db.rs | 67 | ||||
-rw-r--r-- | src/main.rs | 2 |
4 files changed, 71 insertions, 11 deletions
@@ -112,6 +112,7 @@ - 500 Internal Server Error - JSON `"Not an admin"` - JSON `"Key already exists"` + - JSON `"Keys aren't used on this server"` - POST `/admin/regkey/del` - Request - JSON `{"id": "ID", "session": "SESSION_KEY", "key": "REGISTRATION_KEY"}` @@ -120,10 +121,12 @@ - 500 Internal Server Error - JSON `"Not an admin"` - JSON `"Could not find key"` + - JSON `"Keys aren't used on this server"` - POST `/admin/regkey/list` - Request - JSON `{"id": "ID", "session": "SESSION_KEY"}` - Response - 200 Ok - 500 Internal Server Error - - JSON `"Not an admin"`
\ No newline at end of file + - JSON `"Not an admin"` + - JSON `"Keys aren't used on this server"`
\ No newline at end of file @@ -3,8 +3,12 @@ Token Values Token Stock Prices (Values) Config Trade Requests +Bets/Requests Admin user recovery user recovery Email -Bets/Requests -Testing
\ No newline at end of file +Testing +Box Errors + +Features + - DisableRegistrations
\ No newline at end of file @@ -8,12 +8,18 @@ use std::{fs::File, io::Write}; use crate::uid::{self, UID}; +#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] +pub enum Features { + UseRegistrationKeys +} + #[derive(Serialize, Deserialize, Clone)] pub struct Config { dbsave: PathBuf, address: IpAddr, port: u16, base_tokens: Vec<Token>, + features: Vec<Features>, } impl Config { pub fn new() -> Config { @@ -28,6 +34,9 @@ impl Config { Token::new(Color::Blue, 5), Token::new(Color::Yellow, 5), ], + features: vec![ + Features::UseRegistrationKeys + ] } } @@ -61,6 +70,10 @@ impl Config { pub fn address(&self) -> IpAddr { self.address } + + pub fn useregistrationkeys(&self) -> bool { + self.features.contains(&Features::UseRegistrationKeys) + } } #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] @@ -331,6 +344,10 @@ impl DB { } pub async fn use_key(&mut self, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let mut result = Err("Could not find key".into()); let new_vec = self.registration_keys.clone().into_iter().filter_map(|k| { if *key == k { @@ -346,6 +363,10 @@ impl DB { } fn add_key(&mut self, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + if self.registration_keys.contains(key) { Err("Key already exists".into()) } else { @@ -355,6 +376,10 @@ impl DB { } pub async fn new_registration_key(&mut self, id: UID, session: &String, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { self.add_key(key)?; @@ -365,6 +390,10 @@ impl DB { } pub async fn del_registration_key(&mut self, id: UID, session: &String, key: &String) -> Result<(), String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { self.use_key(key).await?; @@ -375,6 +404,10 @@ impl DB { } pub async fn list_registration_keys(&self, id: UID, session: &String) -> Result<Vec<String>, String> { + if !self.config.useregistrationkeys() { + return Err("Keys aren't used on this server".into()); + } + let u = self.get_user_authenticated(id, session).await?; if u.is_admin() { Ok(self.registration_keys.clone()) @@ -383,26 +416,46 @@ impl DB { } } - pub async fn new_user(&mut self, name: String, password: String, id: UID, key: &String) -> Result<User, String> { - if self.use_key(key).await.is_ok() { - let mut is_admin = false; - if key == "ADMIN" { + pub async fn new_user(&mut self, name: String, password: String, id: UID, key: &Option<String>) -> Result<User, String> { + let ok: bool; + let mut is_admin = false; + + if self.config.useregistrationkeys() { + let key = match key.clone() { + Some(n) => n, + None => return Err("Key is required".into()), + }; + + if self.use_key(&key).await.is_ok() { + ok = true; + if key == "ADMIN" { + is_admin = true; + } + } else { + return Err("Invalid key".into()) + } + + } else { + ok = true; + if self.users.len() == 0 { is_admin = true; } + } + + if ok { let user = User::new(name, password, id, is_admin, self.config.base_tokens.clone()); self.users.push(user.clone()); match self.save().await { Ok(_) => Ok(user), Err(n) => { - self.add_key(key)?; + self.add_key(&key.clone().unwrap())?; Err(n) }, } } else { - Err("Invalid key".into()) + Err("Could not make new user".into()) } - } pub async fn login(&mut self, id: UID, password: &String, clientid: &String) -> Result<String, String> { diff --git a/src/main.rs b/src/main.rs index 88123df..9d7654b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,7 @@ struct RegisterForm { name: String, password: String, id: Option<UID>, - key: String, + key: Option<String>, } #[post("/register", data="<data>", format="json")] async fn new_user(data: Json<RegisterForm>, db: &State<Mutex<DB>>) -> (Status, Result<Json<UserOut>, Json<String>>) { |