diff options
author | metamuffin <metamuffin@disroot.org> | 2023-10-01 00:38:29 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-10-01 00:38:29 +0200 |
commit | fc5e13ae525cb74e77a5bc51204f44476115cea9 (patch) | |
tree | a20b6d296d67735a2c8d42a0dc31b44c0bb53cb7 /base/src/permission.rs | |
parent | d546caa3f5053ade763430490911fefd6257af9f (diff) | |
download | jellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar jellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar.bz2 jellything-fc5e13ae525cb74e77a5bc51204f44476115cea9.tar.zst |
draft for permission framework
Diffstat (limited to 'base/src/permission.rs')
-rw-r--r-- | base/src/permission.rs | 27 |
1 files changed, 27 insertions, 0 deletions
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." + )) + } + } +} |