aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs64
1 files changed, 25 insertions, 39 deletions
diff --git a/src/db.rs b/src/db.rs
index 43a9f9c..a6ca7b0 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -127,12 +127,12 @@ impl User {
pub fn get_name(&self) -> String {
self.name.clone()
}
- pub fn hex_id(&self) -> String {
- self.id.to_hex()
- }
pub fn get_tokovec(&self) -> Vec<Token> {
self.tokovec.clone()
}
+ pub fn get_id(&self) -> UID {
+ self.id
+ }
}
#[derive(Serialize, Deserialize)]
@@ -179,22 +179,18 @@ impl DB {
DB { uid_generator: uid::Generator::new(), users: vec![], config }
}
- pub async fn get_user(&self, id: &str) -> Result<&User, String> {
- match UID::from(id.to_string()) {
- Ok(n) => {
- for u in self.users.iter() {
- if u.id == n {
- return Ok(u)
- }
- }
- },
- Err(_) => (),
+ pub fn get_user(&self, id: UID) -> Result<&User, String> {
+ for u in self.users.iter() {
+ if u.id == id {
+ return Ok(u)
+ }
}
Err("Not Found".into())
}
- pub async fn get_user_authenticated(&self, id: &str, session: &String) -> Result<&User, String> {
- match self.get_user(id).await {
+
+ pub async fn get_user_authenticated(&self, id: UID, session: &String) -> Result<&User, String> {
+ match self.get_user(id) {
Ok(u) => {
if u.authenticate(session) {
Ok(u)
@@ -205,16 +201,11 @@ impl DB {
Err(n) => Err(n)
}
}
- pub async fn get_mut_user(&mut self, id: &str) -> Result<&mut User, String> {
- match UID::from(id.to_string()) {
- Ok(n) => {
- for u in self.users.iter_mut() {
- if u.id == n {
- return Ok(u)
- }
- }
- },
- Err(_) => (),
+ pub async fn get_mut_user(&mut self, id: UID) -> Result<&mut User, String> {
+ for u in self.users.iter_mut() {
+ if u.id == id {
+ return Ok(u)
+ }
}
Err("Not Found".into())
@@ -250,7 +241,7 @@ impl DB {
self.save().await
}
- pub async fn login(&mut self, id: &String, password: &String, clientid: &String) -> Result<String, String> {
+ pub async fn login(&mut self, id: UID, password: &String, clientid: &String) -> Result<String, String> {
let r = match self.get_mut_user(id).await {
Ok(n) => {
n.login(password, clientid)
@@ -261,7 +252,7 @@ impl DB {
let _ = self.save().await;
r
}
- pub async fn logout(&mut self, id: &String, session: &String) -> Result<(), String> {
+ pub async fn logout(&mut self, id: UID, session: &String) -> Result<(), String> {
let r = match self.get_mut_user(id).await {
Ok(n) => {
n.logout(session)
@@ -272,7 +263,7 @@ impl DB {
let _ = self.save().await;
r
}
- pub async fn logout_all(&mut self, id: &String, session: &String) -> Result<String, String> {
+ pub async fn logout_all(&mut self, id: UID, session: &String) -> Result<String, String> {
let r = match self.get_mut_user(id).await {
Ok(n) => {
n.clear_sessions(session);
@@ -285,8 +276,8 @@ impl DB {
r
}
- pub async fn get_sessions(&self, id: &String, session: &String) -> Result<Vec<(String, String)>, String> {
- let r = match self.get_user(id).await {
+ pub async fn get_sessions(&self, id: UID, session: &String) -> Result<Vec<(String, String)>, String> {
+ let r = match self.get_user(id) {
Ok(n) => {
n.get_sessions(session)
},
@@ -297,17 +288,12 @@ impl DB {
r
}
- pub async fn delete_user(&mut self, id: &String, session: &String) -> Result<String, String> {
- match UID::from(id.clone()) {
- Ok(n) => {
- self.users = self.users.clone().into_iter().filter(|u| !u.authenticate(session) && n != u.id).collect();
- self.uid_generator.delete_uid(n);
- },
- Err(n) => return Err(n)
- };
+ pub async fn delete_user(&mut self, id: UID, session: &String) -> Result<String, String> {
+ self.users = self.users.clone().into_iter().filter(|u| !u.authenticate(session) && id != u.id).collect();
+ self.uid_generator.delete_uid(id);
// Validate
- let r = match self.get_user(id).await {
+ let r = match self.get_user(id) {
Ok(_) => Err("Could not delete".into()),
Err(_) => {
Ok("Deleted".into())