Skip to main content

v_authorization_impl/
az_context.rs

1use std::io;
2use v_authorization::common::{AuthorizationContext, Trace};
3
4#[cfg(feature = "lmdb")]
5use crate::az_lmdb::LmdbAzContext;
6#[cfg(any(feature = "tt_2", feature = "tt_3"))]
7use crate::az_tarantool::TarantoolAzContext;
8
9/// Unified authorization context with backend selection at compile time
10pub enum AzContext {
11    #[cfg(feature = "lmdb")]
12    Lmdb(LmdbAzContext),
13    #[cfg(any(feature = "tt_2", feature = "tt_3"))]
14    Tarantool(TarantoolAzContext),
15}
16
17impl AzContext {
18    /// Create LMDB-backed context
19    #[cfg(feature = "lmdb")]
20    pub fn new(max_read_counter: u64) -> Self {
21        AzContext::Lmdb(LmdbAzContext::new(max_read_counter))
22    }
23    
24    /// Create LMDB-backed context with full configuration
25    #[cfg(feature = "lmdb")]
26    pub fn new_with_config(
27        max_read_counter: u64,
28        stat_url: Option<String>,
29        stat_mode: Option<String>,
30        use_cache: Option<bool>,
31    ) -> Self {
32        AzContext::Lmdb(LmdbAzContext::new_with_config(
33            max_read_counter, stat_url, stat_mode, use_cache
34        ))
35    }
36    
37    /// Create Tarantool-backed context
38    #[cfg(any(feature = "tt_2", feature = "tt_3"))]
39    pub fn new(uri: &str, login: &str, password: &str) -> Self {
40        AzContext::Tarantool(TarantoolAzContext::new(uri, login, password))
41    }
42    
43    /// Create Tarantool-backed context with stats
44    #[cfg(any(feature = "tt_2", feature = "tt_3"))]
45    pub fn new_with_config(
46        uri: &str, 
47        login: &str, 
48        password: &str,
49        stat_url: Option<String>,
50        stat_mode: Option<String>,
51    ) -> Self {
52        AzContext::Tarantool(TarantoolAzContext::new_with_stat(
53            uri, login, password, stat_url, stat_mode
54        ))
55    }
56}
57
58impl AuthorizationContext for AzContext {
59    fn authorize(
60        &mut self,
61        uri: &str,
62        user_uri: &str,
63        request_access: u8,
64        is_check_for_reload: bool,
65    ) -> Result<u8, io::Error> {
66        match self {
67            #[cfg(feature = "lmdb")]
68            AzContext::Lmdb(ctx) => ctx.authorize(uri, user_uri, request_access, is_check_for_reload),
69            #[cfg(any(feature = "tt_2", feature = "tt_3"))]
70            AzContext::Tarantool(ctx) => ctx.authorize(uri, user_uri, request_access, is_check_for_reload),
71        }
72    }
73
74    fn authorize_and_trace(
75        &mut self,
76        uri: &str,
77        user_uri: &str,
78        request_access: u8,
79        is_check_for_reload: bool,
80        trace: &mut Trace,
81    ) -> Result<u8, io::Error> {
82        match self {
83            #[cfg(feature = "lmdb")]
84            AzContext::Lmdb(ctx) => ctx.authorize_and_trace(uri, user_uri, request_access, is_check_for_reload, trace),
85            #[cfg(any(feature = "tt_2", feature = "tt_3"))]
86            AzContext::Tarantool(ctx) => ctx.authorize_and_trace(uri, user_uri, request_access, is_check_for_reload, trace),
87        }
88    }
89}
90
91#[cfg(feature = "lmdb")]
92impl Default for AzContext {
93    fn default() -> Self {
94        AzContext::new(u64::MAX)
95    }
96}