chiark / gitweb /
wip new account etc., introducing AccountId
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Oct 2020 01:07:30 +0000 (02:07 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 24 Oct 2020 01:07:30 +0000 (02:07 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/accounts.rs
src/cmdlistener.rs
src/global.rs

index 42399964be50b2607441c3a3ef7ef56a9fa88f76..00cd76a9423c165530211e2a2752dd4223967422 100644 (file)
@@ -8,6 +8,11 @@ use parking_lot::{RwLock, const_rwlock,
                   RwLockReadGuard, RwLockWriteGuard,
                   MappedRwLockReadGuard, MappedRwLockWriteGuard};
 
+slotmap::new_key_type!{
+//  #[derive(Serialize,Deserialize,Debug)] xxx
+  pub struct AccountId;
+}
+
 #[derive(Debug,Clone,Deserialize,Serialize)]
 #[derive(Eq,PartialEq,Ord,PartialOrd,Hash)]
 pub enum AccountScope {
@@ -147,8 +152,14 @@ pub struct TokenRevelation {
   pub earliest: Timestamp,
 }
 
-static ACCOUNTS : RwLock<Option<HashMap<AccountName, AccountRecord>>>
-  = const_rwlock(None);
+#[derive(Debug)]
+#[derive(Serialize,Deserialize)]
+struct Accounts {
+  names: HashMap<AccountName, AccountId>,
+  records: DenseSlotMap<AccountId, AccountRecord>,
+}
+
+static ACCOUNTS : RwLock<Option<Accounts>> = const_rwlock(None);
 
 // xxx load, incl reveleation expiry
 // xxx periodic token reveleation expiry
@@ -173,14 +184,24 @@ impl AccountRecord {
       |accounts| accounts.as_mut()?.get_mut(account)
     ).ok()
   }
+
+  #[throws(MgmtError)]
   pub fn with_entry_mut<T, F>(account: &AccountName,
                               auth: Authorisation<AccountName>,
                               f: F)
                               -> Result<T, (InternalError, T)>
-  where F: FnOnce(Option<&mut AccountRecord>) -> T
+  where F: FnOnce(&mut AccountRecord) -> T
   {
-    let entry = AccountRecord::lookup_mut_caller_must_save(account, auth);
-    let output = f(entry.as_deref_mut());
+    let entry = AccountRecord::lookup_mut_caller_must_save(account, auth)
+      .ok_or(MgmtError::AccountNotFound)?;
+    let old_access = entry.access.clone();
+    let output = f(entry);
+    let mut ok = true;
+    if ! Arc::ptr_eq(old_access, entry.access) {
+// xxx actually do this
+//      invalidate_all_tokens_for_account(accid)
+//        .dont_just_questionmark
+    }
     let ok = if entry.is_some() { save_accounts_now() } else { Ok(()) };
     match ok {
       Ok(()) => Ok(output),
index f39fd308d467bd76cd6cf750fe070f967a95e570..197b9822cdc6c66d7891eba83f606278ca35807d 100644 (file)
@@ -102,6 +102,20 @@ fn execute(cs: &mut CommandStream, cmd: MgmtCommand) -> MgmtResponse {
       AccountRecord::insert_entry(account, auth, record)?;
       Fine
     }
+/*
+    UpdateAccont(AccountDetails { account, nick, timezone, access }) => {
+      let auth = authorise_for_account(cs, &account)?;
+      let access = access.map(Into::into);
+      
+        .unwrap_or_else(|| Arc::new(PlayerAccessUnset) as Arc<_>);
+      let record = AccountRecord {
+        nick, access,
+        timezone: timezone.unwrap_or_default(),
+        tokens_revealed: default(),
+      };
+      AccountRecord::insert_entry(account, auth, record)?;
+      Fine
+    }*/
 
     SetAccount(wanted_account) => {
       let auth = authorise_scope_direct(cs, &wanted_account.scope)?;
@@ -355,23 +369,6 @@ fn execute_game_insn<'ig>(
           raw: None },
        PlayerAccessToken(token), ig)
     },
-/*
-    SetFixedPlayerAccess { player, token } => {
-      let authorised : AuthorisedSatisfactory =
-        authorise_scope(cs, &AS::Server)?;
-      let authorised = match authorised.into_inner() {
-        AS::Server => Authorisation::<RawToken>::authorise(),
-        _ => panic!(),
-      };
-      ig.player_access_register_fixed(
-        player, token, authorised
-      )?;
-      (U{ pcs: vec![],
-          log: vec![],
-          raw: None},
-       Fine)
-    }
-*/
 
     DeletePiece(piece) => {
       let (ig, modperm, _) = cs.check_acl_modify_pieces(ig)?;
index d378df3074cb3700dd6045ce8b902f7ec44ffed4..912844c4fa7892c7259aeedb1cddebb4468faeb3 100644 (file)
@@ -164,6 +164,7 @@ pub struct TokenRegistry<Id: AccessId> {
 pub struct InstanceAccessDetails<Id> {
   pub gref : InstanceRef,
   pub ident : Id,
+  pub accid : AccountId,
 }
 
 #[derive(Clone,Debug)]
@@ -561,21 +562,6 @@ impl InstanceGuard<'_> {
     Ok((old_account, old_pst))
   }
 
-  #[throws(MgmtError)]
-  pub fn player_access_register_fixed(&mut self,
-                                      player: PlayerId, token: RawToken,
-                                      _safe: Authorisation<RawToken>
-  ) {
-    // xxx get rid of this or something ?
-    self.tokens_deregister_for_id(|id:PlayerId| id==player);
-    let iad = InstanceAccessDetails {
-      gref : self.gref.clone(),
-      ident : player
-    };
-    self.token_register(token, iad);
-    self.save_access_now()?;
-  }
-
   #[throws(MgmtError)]
   fn player_access_reset_redeliver(&mut self, player: PlayerId,
                                    authorised: Authorisation<AccountName>,
@@ -587,9 +573,8 @@ impl InstanceGuard<'_> {
 
     let access = AccountRecord::with_entry_mut(
       &pst.account, authorised,
-      |acct|
+      |acct, acctid|
     {
-      let acct = acct.ok_or(MgmtError::AccountNotFound)?;
       let access = acct.access;
       let desc = access.describe_html();
       let now = Timestamp::now();
@@ -601,7 +586,7 @@ impl InstanceGuard<'_> {
         .latest = now;
       acct.expire_tokens_revealed();
       Ok::<_,MgmtError>(access.clone())
-    }).map_err(|(e,_)|e)??;
+    })?.map_err(|(e,_)|e)??;
 
     if reset {
       self.save_access_now()?;