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
use crate::access_control::{IncomingAccessControl, OutgoingAccessControl};
use crate::compat::sync::Arc;
use crate::compat::vec::Vec;
use crate::{async_trait, compat::boxed::Box, RelayMessage, Result};

/// Allows message that are allowed by any of [`IncomingAccessControl`]s
#[derive(Debug)]
pub struct AnyIncomingAccessControl(Vec<Arc<dyn IncomingAccessControl>>);

impl AnyIncomingAccessControl {
    /// Constructor
    pub fn new(access_controls: Vec<Arc<dyn IncomingAccessControl>>) -> Self {
        Self(access_controls)
    }
}

#[async_trait]
impl IncomingAccessControl for AnyIncomingAccessControl {
    async fn is_authorized(&self, relay_msg: &RelayMessage) -> Result<bool> {
        for ac in &self.0 {
            if ac.is_authorized(relay_msg).await? {
                return crate::allow();
            }
        }

        crate::deny()
    }
}

/// Allows message that are allowed by any of [`OutgoingAccessControl`]s
#[derive(Debug)]
pub struct AnyOutgoingAccessControl(Vec<Arc<dyn OutgoingAccessControl>>);

impl AnyOutgoingAccessControl {
    /// Constructor
    pub fn new(access_controls: Vec<Arc<dyn OutgoingAccessControl>>) -> Self {
        Self(access_controls)
    }
}

#[async_trait]
impl OutgoingAccessControl for AnyOutgoingAccessControl {
    async fn is_authorized(&self, relay_msg: &RelayMessage) -> Result<bool> {
        for ac in &self.0 {
            if ac.is_authorized(relay_msg).await? {
                return crate::allow();
            }
        }

        crate::deny()
    }
}

// TODO: Tests