1
use std::collections::HashMap;
2

            
3
use axum::{http::Method, routing, Router};
4

            
5
use sylvia_iot_corelib::role::Role;
6

            
7
use super::super::{
8
    oauth2::middleware::{AuthService, RoleScopeType},
9
    State,
10
};
11

            
12
mod api;
13
mod request;
14
mod response;
15

            
16
974
pub fn new_service(scope_path: &str, state: &State) -> Router {
17
974
    let mut role_scopes_root: HashMap<Method, RoleScopeType> = HashMap::new();
18
974
    let mut role_scopes_count: HashMap<Method, RoleScopeType> = HashMap::new();
19
974
    let mut role_scopes_list: HashMap<Method, RoleScopeType> = HashMap::new();
20
974
    let mut role_scopes_param: HashMap<Method, RoleScopeType> = HashMap::new();
21
974

            
22
974
    match state.api_scopes.get("user.get") {
23
973
        None => {
24
973
            role_scopes_root.insert(Method::GET, (vec![], vec![]));
25
973
        }
26
1
        Some(scopes) => {
27
1
            role_scopes_root.insert(Method::GET, (vec![], scopes.clone()));
28
1
        }
29
    }
30
974
    match state.api_scopes.get("user.patch") {
31
973
        None => {
32
973
            role_scopes_root.insert(Method::PATCH, (vec![], vec![]));
33
973
        }
34
1
        Some(scopes) => {
35
1
            role_scopes_root.insert(Method::PATCH, (vec![], scopes.clone()));
36
1
        }
37
    }
38
974
    match state.api_scopes.get("user.post.admin") {
39
973
        None => {
40
973
            role_scopes_root.insert(Method::POST, (vec![Role::ADMIN], vec![]));
41
973
        }
42
1
        Some(scopes) => {
43
1
            role_scopes_root.insert(Method::POST, (vec![Role::ADMIN], scopes.clone()));
44
1
        }
45
    }
46
974
    match state.api_scopes.get("user.get.admin") {
47
973
        None => {
48
973
            role_scopes_count.insert(Method::GET, (vec![Role::ADMIN, Role::MANAGER], vec![]));
49
973
            role_scopes_list.insert(Method::GET, (vec![Role::ADMIN, Role::MANAGER], vec![]));
50
973
            role_scopes_param.insert(Method::GET, (vec![Role::ADMIN, Role::MANAGER], vec![]));
51
973
        }
52
1
        Some(scopes) => {
53
1
            role_scopes_count.insert(
54
1
                Method::GET,
55
1
                (vec![Role::ADMIN, Role::MANAGER], scopes.clone()),
56
1
            );
57
1
            role_scopes_list.insert(
58
1
                Method::GET,
59
1
                (vec![Role::ADMIN, Role::MANAGER], scopes.clone()),
60
1
            );
61
1
            role_scopes_param.insert(
62
1
                Method::GET,
63
1
                (vec![Role::ADMIN, Role::MANAGER], scopes.clone()),
64
1
            );
65
1
        }
66
    }
67
974
    match state.api_scopes.get("user.patch.admin") {
68
973
        None => {
69
973
            role_scopes_param.insert(Method::PATCH, (vec![Role::ADMIN, Role::MANAGER], vec![]));
70
973
        }
71
1
        Some(scopes) => {
72
1
            role_scopes_param.insert(
73
1
                Method::PATCH,
74
1
                (vec![Role::ADMIN, Role::MANAGER], scopes.clone()),
75
1
            );
76
1
        }
77
    }
78
974
    match state.api_scopes.get("user.delete.admin") {
79
973
        None => {
80
973
            role_scopes_param.insert(Method::DELETE, (vec![Role::ADMIN], vec![]));
81
973
        }
82
1
        Some(scopes) => {
83
1
            role_scopes_param.insert(Method::DELETE, (vec![Role::ADMIN], scopes.clone()));
84
1
        }
85
    }
86

            
87
974
    Router::new().nest(
88
974
        scope_path,
89
974
        Router::new()
90
974
            .route(
91
974
                "/",
92
974
                routing::get(api::get_user)
93
974
                    .patch(api::patch_user)
94
974
                    .post(api::post_admin_user)
95
974
                    .layer(AuthService::new(&state.model, role_scopes_root)),
96
974
            )
97
974
            .route(
98
974
                "/count",
99
974
                routing::get(api::get_admin_user_count)
100
974
                    .layer(AuthService::new(&state.model, role_scopes_count)),
101
974
            )
102
974
            .route(
103
974
                "/list",
104
974
                routing::get(api::get_admin_user_list)
105
974
                    .layer(AuthService::new(&state.model, role_scopes_list)),
106
974
            )
107
974
            .route(
108
974
                "/:user_id",
109
974
                routing::get(api::get_admin_user)
110
974
                    .patch(api::patch_admin_user)
111
974
                    .delete(api::delete_admin_user)
112
974
                    .layer(AuthService::new(&state.model, role_scopes_param)),
113
974
            )
114
974
            .with_state(state.clone()),
115
974
    )
116
974
}