aboutsummaryrefslogtreecommitdiff
path: root/src/users.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/users.rs')
-rw-r--r--src/users.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/users.rs b/src/users.rs
new file mode 100644
index 000000000000..edc3c13fe3b1
--- /dev/null
+++ b/src/users.rs
@@ -0,0 +1,61 @@
+//! A users abstraction module
+
+use crate::{
+ wire::{User, UserId},
+ Id,
+};
+use async_std::sync::{Arc, RwLock};
+use std::{
+ collections::BTreeMap,
+ sync::atomic::{AtomicUsize, Ordering},
+};
+
+pub struct MetaUser {
+ pub id: UserId,
+ pub name: String,
+ pub pw: String,
+ pub auth: User,
+}
+
+pub struct UserStore {
+ max: AtomicUsize,
+ users: RwLock<BTreeMap<UserId, Arc<MetaUser>>>,
+}
+
+impl UserStore {
+ /// Currently resuming a userstore isn't possible
+ pub fn new() -> Self {
+ UserStore {
+ max: 0.into(),
+ users: Default::default(),
+ }
+ }
+
+ pub async fn add<S: Into<Option<String>>>(
+ &self,
+ name: String,
+ pw: S,
+ registered: bool,
+ ) -> (UserId, User) {
+ let id = self.max.fetch_add(1, Ordering::Relaxed);
+ let token = Id::random();
+ let pw = pw.into().unwrap_or("".into());
+ let auth = User {
+ id,
+ token,
+ registered,
+ };
+
+ self.users.write().await.insert(
+ id,
+ MetaUser {
+ id,
+ name,
+ pw,
+ auth: auth.clone(),
+ }
+ .into(),
+ );
+ (id, auth.clone())
+ }
+}