aboutsummaryrefslogtreecommitdiff
path: root/base/src/permission.rs
blob: 8993154543dcfa77644f389ae152f0003e1af25e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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."
            ))
        }
    }
}