From: Ian Jackson Date: Sat, 24 Oct 2020 01:07:30 +0000 (+0100) Subject: wip new account etc., introducing AccountId X-Git-Tag: otter-0.2.0~599 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8d08b83c0234b0efe5ebabfb6c3b086b39b15fb2;p=otter.git wip new account etc., introducing AccountId Signed-off-by: Ian Jackson --- diff --git a/src/accounts.rs b/src/accounts.rs index 42399964..00cd76a9 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -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>> - = const_rwlock(None); +#[derive(Debug)] +#[derive(Serialize,Deserialize)] +struct Accounts { + names: HashMap, + records: DenseSlotMap, +} + +static ACCOUNTS : RwLock> = 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(account: &AccountName, auth: Authorisation, f: F) -> Result - 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), diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index f39fd308..197b9822 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -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::::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)?; diff --git a/src/global.rs b/src/global.rs index d378df30..912844c4 100644 --- a/src/global.rs +++ b/src/global.rs @@ -164,6 +164,7 @@ pub struct TokenRegistry { pub struct InstanceAccessDetails { 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 - ) { - // 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, @@ -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()?;