aboutsummaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/Cargo.toml3
-rw-r--r--base/src/lib.rs4
-rw-r--r--base/src/permission.rs27
3 files changed, 32 insertions, 2 deletions
diff --git a/base/Cargo.toml b/base/Cargo.toml
index f49db80..3081262 100644
--- a/base/Cargo.toml
+++ b/base/Cargo.toml
@@ -5,9 +5,10 @@ edition = "2021"
[dependencies]
jellycommon = { path = "../common" }
+serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
log = { workspace = true }
sha2 = "0.10.8"
base64 = "0.21.4"
-tokio = {workspace=true}
+tokio = { workspace = true }
anyhow = "1.0.75"
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."
+ ))
+ }
+ }
+}