aboutsummaryrefslogtreecommitdiff
path: root/base/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-10-01 00:38:29 +0200
committermetamuffin <metamuffin@disroot.org>2023-10-01 00:38:29 +0200
commitfc5e13ae525cb74e77a5bc51204f44476115cea9 (patch)
treea20b6d296d67735a2c8d42a0dc31b44c0bb53cb7 /base/src
parentd546caa3f5053ade763430490911fefd6257af9f (diff)
downloadjellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar
jellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar.bz2
jellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar.zst
draft for permission framework
Diffstat (limited to 'base/src')
-rw-r--r--base/src/lib.rs4
-rw-r--r--base/src/permission.rs27
2 files changed, 30 insertions, 1 deletions
diff --git a/base/src/lib.rs b/base/src/lib.rs
index b97f924..5a98be5 100644
--- a/base/src/lib.rs
+++ b/base/src/lib.rs
@@ -4,8 +4,10 @@
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
#![feature(lazy_cell)]
+pub mod permission;
+
use base64::Engine;
-use jellycommon::{config::GlobalConfig, AssetLocation};
+use jellycommon::{AssetLocation, config::GlobalConfig};
use std::{fs::File, future::Future, path::PathBuf, sync::LazyLock};
use tokio::sync::Mutex;
diff --git a/base/src/permission.rs b/base/src/permission.rs
new file mode 100644
index 0000000..8993154
--- /dev/null
+++ b/base/src/permission.rs
@@ -0,0 +1,27 @@
+use crate::CONF;
+use anyhow::anyhow;
+use jellycommon::user::{PermissionSet, UserPermission};
+
+pub trait PermissionSetExt {
+ fn check(&self, perm: UserPermission) -> bool;
+ fn assert(&self, perm: UserPermission) -> Result<(), anyhow::Error>;
+}
+
+impl PermissionSetExt for PermissionSet {
+ fn check(&self, perm: UserPermission) -> bool {
+ *self
+ .0
+ .get(&perm)
+ .or(CONF.default_permission_set.0.get(&perm))
+ .unwrap_or(&perm.default_value())
+ }
+ fn assert(&self, perm: UserPermission) -> Result<(), anyhow::Error> {
+ if self.check(perm) {
+ Ok(())
+ } else {
+ Err(anyhow!(
+ "sorry, you need special permission {perm:?} for this action."
+ ))
+ }
+ }
+}