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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Role-based database and command authorization.
use std::string::ToString;

use bson::Bson;

pub enum SingleDatabaseRole {
    Read,
    ReadWrite,
    DbAdmin,
    DbOwner,
    UserAdmin,
    ClusterAdmin,
    ClusterManager,
    ClusterMonitor,
    HostManager,
    Backup,
    Restore,
}

impl ToString for SingleDatabaseRole {
    fn to_string(&self) -> String {
        let string = match *self {
            SingleDatabaseRole::Read => "read",
            SingleDatabaseRole::ReadWrite => "readWrite",
            SingleDatabaseRole::DbAdmin => "dbAdmin",
            SingleDatabaseRole::DbOwner => "dbOwner",
            SingleDatabaseRole::UserAdmin => "userAdmin",
            SingleDatabaseRole::ClusterAdmin => "clusterAdmin",
            SingleDatabaseRole::ClusterManager => "clusterManager",
            SingleDatabaseRole::ClusterMonitor => "clusterMonitor",
            SingleDatabaseRole::HostManager => "hostManager",
            SingleDatabaseRole::Backup => "backup",
            SingleDatabaseRole::Restore => "restore",
        };

        string.to_owned()
    }
}

pub enum AllDatabaseRole {
    Read,
    ReadWrite,
    UserAdmin,
    DbAdmin,
}

impl ToString for AllDatabaseRole {
    fn to_string(&self) -> String {
        let string = match *self {
            AllDatabaseRole::Read => "read",
            AllDatabaseRole::ReadWrite => "readWrite",
            AllDatabaseRole::UserAdmin => "userAdmin",
            AllDatabaseRole::DbAdmin => "dbAdmin",
        };

        string.to_owned()
    }
}

pub enum Role {
    All(AllDatabaseRole),
    Single {
        role: SingleDatabaseRole,
        db: String,
    },
}

impl Role {
    fn to_bson(&self) -> Bson {
        match *self {
            Role::All(ref role) => Bson::String(role.to_string()),
            Role::Single { ref role, ref db } => Bson::Document(doc! {
                "role" => (Bson::String(role.to_string())),
                "db" => (Bson::String(db.to_owned()))
            })
        }
    }

    pub fn to_bson_array(vec: Vec<Role>) -> Bson {
        Bson::Array(vec.into_iter().map(|r| r.to_bson()).collect())
    }
}