chiark / gitweb /
wip new account etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 20 Oct 2020 21:19:09 +0000 (22:19 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 20 Oct 2020 21:19:09 +0000 (22:19 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/accounts.rs
src/cmdlistener.rs
src/global.rs
src/imports.rs
src/spec.rs

index 05416f2329e746621abba32f3449ec78ba50aa54..0676f5e55f6faab39871241cd95663f278b8e272 100644 (file)
@@ -140,7 +140,7 @@ pub mod loaded_acl {
   use crate::imports::*;
 
   pub trait Perm : FromPrimitive + ToPrimitive
-    + Copy + Eq + Hash + Sync + Send { }
+    + Copy + Eq + Hash + Sync + Send + 'static { }
 
   #[derive(Copy,Clone,Debug)]
   pub struct PermSet<P: Perm> (u64, PhantomData<&'static P>);
@@ -157,8 +157,8 @@ pub mod loaded_acl {
   #[derive(Debug,Clone)]
   struct LoadedAclEntry<P: Perm> {
     pat: glob::Pattern,
-    allow: Bitmap,
-    deny: Bitmap,
+    allow: PermSet<P>,
+    deny: PermSet<P>,
     ptype: PhantomData<&'static P>,
   }
 
@@ -170,14 +170,15 @@ pub mod loaded_acl {
     ptype: PhantomData<&'static P>,
   }
 
-  impl<P:Perm> LoadedAcl<P> {
-    fn entries(&'s self) -> impl Iterator<Item=AclEntryRef<'s>> {
-      self.owner_account.map(
+  impl<'e, P:Perm> EffectiveAcl<'e, P> {
+    fn entries(&self) -> impl Iterator<Item=AclEntryRef<'_, P>> {
+      self.owner_account.iter().map(
         |owner|
-        AclEntryRef { pat: Left(owner), allow: !0, deny: 0, ptype }
-      ).chain(self.entries.map(
-        |LoadedAclEntry { pat, allow, deny }|
-        AclEntryRef { pat: Left(pat), allow: allow.0, deny: deny.0 }
+        AclEntryRef { pat: Left(owner), allow: !0, deny: 0,
+                      ptype: PhantomData }
+      ).chain(self.acl.0.iter().map(
+        |&LoadedAclEntry { ref pat, ref allow, ref deny, ptype }|
+        AclEntryRef { pat: Right(pat), allow: allow.0, deny: deny.0, ptype }
       ))
     }
 
@@ -194,17 +195,19 @@ pub mod loaded_acl {
         needed &= !allow;
         if needed == 0 { return Ok(()) }
       }
-      Err(ME::PermissionDenied)
+      Err(MgmtError::PermissionDenied)
     }
   }
 
-  impl<P:Perm> From<I> for PermSet<P> where I: IntoIterator<Item=&P> {
+  impl<'i, P:Perm, I> From<I> for PermSet<P>
+  where I: IntoIterator<Item=&'i P>
+  {
     fn from(i: I) -> Self {
       i.into_iter().fold(0, |b, i| b | i.to_u64().unwrap())
     }
   }
 
-  fn unpack<P:Perm>(unpacked: Bitmap) -> HashSet<P> {
+  fn unpack<P:Perm>(unpacked: PermSet<P>) -> HashSet<P> {
     let mut s = HashSet::new();
     for n in 0.. {
       let v = match FromPrimitive::from_u64(n) { Some(v) => v, None => break };
index 190ee91ecbf73d22ed220571d6444592f6222383..0f1075d8ea3372fcafd2709f7ee7fa3f43f91766 100644 (file)
@@ -157,7 +157,7 @@ fn execute_game_insn(cs: &CommandStream,
 
   fn readonly<F: FnOnce(&mut InstanceGuard) -> ExecuteGameInsnResults>(
     cs: &CommandStream,
-    ig: &Unauthorised<InstanceGuard>,
+    ig: &Unauthorised<InstanceGuard, InstanceName>,
     p: PermSet<TablePermission>,
     f: F) -> ExecuteGameInsnResults
   {
@@ -571,6 +571,16 @@ impl CommandStream<'_> {
   }
 }
 
+#[throws(MgmtError)]
+fn authorise_by_account(cs: &CommandStream, wanted: &AccountScope)
+                        -> Authorisation<AccountScope> {
+  let currently = &cs.account.as_ref()?.account;
+  if currently == wanted {
+    return Authorisation::authorised(currently);
+  }
+  throw!(MgmtError::AuthorisationError)
+}
+
 #[throws(MgmtError)]
 fn authorise_scope_direct(cs: &CommandStream, wanted: &AccountScope)
                           -> Authorisation<AccountScope> {
index b597e0b25a320fbcd1c7c4c1bf1bd641f78f8fe6..b24c748adf341fe1d191dd9a16614efb17f0f33e 100644 (file)
@@ -254,7 +254,7 @@ impl InstanceRef {
 
 impl<A> Unauthorised<InstanceGuard<'_>, A> {
   #[throws(MgmtError)]
-  pub fn check_acl(&mut self, p: PermSet<TablePermissions>)
+  pub fn check_acl(&mut self, p: PermSet<TablePermission>)
                    -> &mut InstanceGuard {
     let auth = {
       let acl = self.by(Authorisation::authorise_any()).acl;
index 9c1876118ed65ce368661e964e5cdde6bacb5a14..e5e1093b3229ef248295475be90c88eae82de992 100644 (file)
@@ -105,6 +105,8 @@ pub use serde_with::serde_as;
 
 pub use ordered_float::OrderedFloat;
 
+pub use either::{Either,Left,Right};
+
 pub use crate::global::*;
 pub use crate::gamestate::*;
 pub use crate::pieces::*;
@@ -123,7 +125,7 @@ pub use crate::debugreader::DebugReader;
 pub use crate::shapelib;
 pub use crate::tz::*;
 pub use crate::accounts::*;
-pub use crate::accounts::loaded_acl::{self,LoadedAcl};
+pub use crate::accounts::loaded_acl::{self,LoadedAcl,PermSet};
 
 pub use zcoord::{self, ZCoord};
 
index 44772e854674d8641dd56d07524d2cb0097b126a..dd975760ac44375c7a4f8cbfc9b8c161a35ee832 100644 (file)
@@ -14,6 +14,7 @@ use thiserror::Error;
 use crate::error::display_as_debug;
 use crate::accounts::AccountName;
 use std::hash::Hash;
+use num_derive::{ToPrimitive, FromPrimitive};
 
 pub use implementation::PlayerAccessSpec;
 
@@ -74,7 +75,8 @@ pub struct AclEntry<Perm: Eq + Hash> {
 
 #[derive(Debug,Clone,Copy,Serialize,Deserialize)]
 #[derive(Hash,Eq,PartialEq,Ord,PartialOrd)]
-enum TablePermission {
+#[derive(FromPrimitive,ToPrimitive)]
+pub enum TablePermission {
   AddPlayer,
   ChangePieces,
   RemovePlayer,
@@ -223,6 +225,8 @@ pub mod implementation {
   use crate::imports::*;
   type Insn = crate::commands::MgmtGameInstruction;
 
+  impl loaded_acl::Perm for TablePermission { }
+
   type TDE = TokenDeliveryError;
 
   pub fn raw_token_debug_as_str(s: &str, f: &mut fmt::Formatter)