Skip to main content

v_authorization_impl/
az_context.rs

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