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
    let mut role_scopes_user_param: HashMap<Method, RoleScopeType> = HashMap::new();
22
974

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

            
74
974
    Router::new().nest(
75
974
        scope_path,
76
974
        Router::new()
77
974
            .route(
78
974
                "/",
79
974
                routing::post(api::post_client)
80
974
                    .layer(AuthService::new(&state.model, role_scopes_root)),
81
974
            )
82
974
            .route(
83
974
                "/count",
84
974
                routing::get(api::get_client_count)
85
974
                    .layer(AuthService::new(&state.model, role_scopes_count)),
86
974
            )
87
974
            .route(
88
974
                "/list",
89
974
                routing::get(api::get_client_list)
90
974
                    .layer(AuthService::new(&state.model, role_scopes_list)),
91
974
            )
92
974
            .route(
93
974
                "/:client_id",
94
974
                routing::get(api::get_client)
95
974
                    .patch(api::patch_client)
96
974
                    .delete(api::delete_client)
97
974
                    .layer(AuthService::new(&state.model, role_scopes_param)),
98
974
            )
99
974
            .route(
100
974
                "/user/:user_id",
101
974
                routing::delete(api::delete_client_user)
102
974
                    .layer(AuthService::new(&state.model, role_scopes_user_param)),
103
974
            )
104
974
            .with_state(state.clone()),
105
974
    )
106
974
}