summaryrefslogtreecommitdiffstats
path: root/src/permissions.rs
blob: f181e218b4a93c79ada435cf610a3df3f5218f0e (plain)
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
use crate::data::Data;
use failure::Error;
use std::collections::HashSet;

#[macro_export]
macro_rules! permissions {
    ($vis:vis struct $name:ident { $($key:ident,)* }) => {
        #[derive(serde_derive::Deserialize, Debug)]
        #[serde(rename_all = "kebab-case", deny_unknown_fields)]
        $vis struct $name {
            $(
                #[serde(default)]
                $key: bool,
            )*
        }

        impl Default for $name {
            fn default() -> Self {
                $name {
                    $($key: false,)*
                }
            }
        }

        impl $name {
            $vis const AVAILABLE: &'static [&'static str] = &[$(stringify!($key),)*];

            $vis fn has(&self, permission: &str) -> bool {
                $(
                    if permission == stringify!($key) {
                        return self.$key;
                    }
                )*
                false
            }

            $vis fn has_any(&self) -> bool {
                false $(|| self.$key)*
            }
        }
    }
}

pub(crate) fn allowed_github_users(
    data: &Data,
    permission: &str,
) -> Result<HashSet<String>, Error> {
    let mut github_users = HashSet::new();
    for team in data.teams() {
        if team.permissions().has(permission) {
            for member in team.members(&data)? {
                github_users.insert(member.to_string());
            }
        }
    }
    for person in data.people() {
        if person.permissions().has(permission) {
            github_users.insert(person.github().to_string());
        }
    }
    Ok(github_users)
}