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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use eva_common::acl::Acl;
use eva_common::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ACI {
    auth: AuthMode,
    acl: String,
    token_mode: Option<TokenMode>,
    u: Option<String>,
}

impl ACI {
    #[inline]
    pub fn acl_id(&self) -> &str {
        &self.acl
    }
    #[inline]
    pub fn token_mode(&self) -> Option<TokenMode> {
        self.token_mode
    }
    #[inline]
    pub fn user(&self) -> Option<&str> {
        self.u.as_deref()
    }
    #[inline]
    pub fn writable(&self) -> bool {
        if let Some(token_mode) = self.token_mode {
            token_mode == TokenMode::Normal
        } else {
            true
        }
    }
    #[inline]
    pub fn check_write(&self) -> EResult<()> {
        if self.writable() {
            Ok(())
        } else {
            Err(Error::access("Session is in the read-only mode"))
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TokenMode {
    Normal,
    Readonly,
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AuthMode {
    Token,
    Key,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct XParamsOwned {
    pub method: String,
    pub params: Value,
    pub aci: ACI,
    pub acl: Acl,
}

impl XParamsOwned {
    #[inline]
    pub fn method(&self) -> &str {
        &self.method
    }
}