Skip to main content

v_common/storage/
async_storage.rs

1use v_individual_model::onto::individual::Individual;
2use v_individual_model::onto::parser::parse_raw;
3use v_storage::{Storage, StorageId, StorageResult};
4use v_storage::lmdb_storage::LMDBStorage;
5use crate::v_api::common_type::ResultCode;
6use crate::v_authorization::common::{Access, Trace};
7use futures::lock::Mutex;
8use std::io;
9use std::io::{Error, ErrorKind};
10use v_storage::tt_wrapper::{Client, IteratorType};
11
12pub use super::authorization_provider::AuthorizationProvider;
13
14pub const INDIVIDUALS_SPACE_ID: i32 = 512;
15pub const TICKETS_SPACE_ID: i32 = 513;
16
17pub struct AStorage {
18    pub tt: Option<Client>,
19    pub lmdb: Option<Mutex<LMDBStorage>>,
20}
21
22pub async fn check_indv_access_read(
23    mut indv: Individual,
24    uri: &str,
25    user_uri: &str,
26    az: Option<&AuthorizationProvider>,
27) -> io::Result<(Individual, ResultCode)> {
28    if indv.get_id().is_empty() {
29        return Ok((indv, ResultCode::NotFound));
30    }
31
32    if let Some(a) = az {
33        if a.authorize(uri, user_uri, Access::CanRead as u8, false).await.unwrap_or(0) != Access::CanRead as u8 {
34            return Ok((indv, ResultCode::NotAuthorized));
35        }
36    }
37
38    indv.parse_all();
39    Ok((indv, ResultCode::Ok))
40}
41
42pub async fn check_user_in_group(user_id: &str, group_id: &str, az: Option<&AuthorizationProvider>) -> io::Result<bool> {
43    if let Some(a) = az {
44        let mut tr = Trace {
45            acl: &mut "".to_string(),
46            is_acl: false,
47            group: &mut String::new(),
48            is_group: true,
49            info: &mut "".to_string(),
50            is_info: false,
51            str_num: 0,
52        };
53        if a.authorize_and_trace(user_id, user_id, 0xF, false, &mut tr).await.is_ok() {
54            for gr in tr.group.split('\n') {
55                if gr == group_id {
56                    return Ok(true);
57                }
58            }
59        } else {
60            return Err(Error::new(ErrorKind::Other, "fail authorize_and_trace"));
61        }
62    }
63
64    Ok(false)
65}
66
67pub async fn get_individual_from_db(uri: &str, user_uri: &str, db: &AStorage, az: Option<&AuthorizationProvider>) -> io::Result<(Individual, ResultCode)> {
68    get_individual_use_storage_id(StorageId::Individuals, uri, user_uri, db, az).await
69}
70
71pub async fn get_individual_use_storage_id(
72    storage_id: StorageId,
73    uri: &str,
74    user_uri: &str,
75    db: &AStorage,
76    az: Option<&AuthorizationProvider>,
77) -> io::Result<(Individual, ResultCode)> {
78    if let Some(tt) = &db.tt {
79        let space_id = match storage_id {
80            StorageId::Tickets => TICKETS_SPACE_ID,
81            StorageId::Individuals => INDIVIDUALS_SPACE_ID,
82            StorageId::Az => 514,
83        };
84
85        let response = tt.select(space_id, 0, &(uri,), 0, 100, IteratorType::EQ).await?;
86
87        let mut iraw = Individual::default();
88        iraw.set_raw(&response.data[5..]);
89        if parse_raw(&mut iraw).is_ok() {
90            return check_indv_access_read(iraw, uri, user_uri, az).await;
91        }
92        return Ok((iraw, ResultCode::UnprocessableEntity));
93    }
94    if let Some(lmdb) = &db.lmdb {
95        let mut iraw = Individual::default();
96        let res = lmdb.lock().await.get_individual(storage_id, uri, &mut iraw);
97        match res {
98            StorageResult::Ok(()) => {
99                return check_indv_access_read(iraw, uri, user_uri, az).await;
100            }
101            StorageResult::NotFound => {
102                return Ok((Individual::default(), ResultCode::NotFound));
103            }
104            _ => {
105                return Ok((Individual::default(), ResultCode::UnprocessableEntity));
106            }
107        }
108    }
109
110    Ok((Individual::default(), ResultCode::UnprocessableEntity))
111}