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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! This submodule describes routes for managing the data for a `User` entity,
//! particularly with respect to connecting to the `USER` gRPC service.

use super::response;
use crate::fairings::auth::SessionInfo;
use crate::utils;
use log::info;
use minerva_data as data;
use minerva_rpc as rpc;
use response::{ErrorResponse, RestResult};
use rocket::serde::json::Json;
use rocket::Route;
use rocket_okapi::{okapi::openapi3::OpenApi, openapi, openapi_get_routes_spec};
use std::env;
use tonic::Request;

/// Returns a tuple containing a vec of routes for this module, plus a structure
/// containing the OpenAPI specification for these routes.
pub fn routes() -> (Vec<Route>, OpenApi) {
    openapi_get_routes_spec![index, show, store, update, delete]
}

/// Retrieves the endpoint for the gRPC users service. Requires that the proper
/// environment variables are defined.
pub fn get_endpoint() -> String {
    let port = env::var("USER_SERVICE_PORT").expect("Unable to read USER_SERVICE_PORT");
    let srv = env::var("USER_SERVICE_SERVER").expect("Unable to read USER_SERVICE_SERVER");
    format!("http://{}:{}", srv, port)
}

/// Route for listing all users.
///
/// This route uses the concept of pages, starting with page index `0`. The
/// page number should be passed as a request parameter through the URL, under
/// a value named `page`. If omitted, it is assumed to be `0`.
///
/// Upon success, returns a list of users in JSON format, containing up to the
/// number of users per page as defined in the `USER` microservice.
#[allow(unused_variables)]
#[openapi(tag = "User")]
#[get("/<tenant>/user?<page>")]
async fn index(
    tenant: String,
    session: SessionInfo,
    page: Option<i64>,
) -> RestResult<Vec<data::user::User>> {
    let endpoint = get_endpoint();
    let tenant = session.info.tenant.clone();
    let requestor = session.info.login.clone();

    info!(
        "{}",
        data::log::format(
            utils::get_ip(),
            &requestor,
            &tenant,
            &format!("GET /user: request USER.index ({})", endpoint),
        )
    );

    let mut client = rpc::user::make_client(endpoint, tenant, requestor)
        .await
        .map_err(ErrorResponse::from)?;

    client
        .index(Request::new(rpc::messages::PageIndex { index: page }))
        .await
        .map(|msg| Json(data::user::message_to_vec(msg.into_inner())))
        .map_err(ErrorResponse::from)
}

/// Route for fetching data of a single user.
///
/// The numeric user ID should be passed through the route.
///
/// Upon success, retrieves data for a single user of the given ID in JSON
/// format.
#[allow(unused_variables)]
#[openapi(tag = "User")]
#[get("/<tenant>/user/<id>")]
async fn show(tenant: String, session: SessionInfo, id: i32) -> RestResult<data::user::User> {
    let endpoint = get_endpoint();
    let tenant = session.info.tenant.clone();
    let requestor = session.info.login.clone();

    info!(
        "{}",
        data::log::format(
            utils::get_ip(),
            &requestor,
            &tenant,
            &format!("GET /user/<id>: request USER.show ({})", endpoint),
        )
    );

    let mut client = rpc::user::make_client(endpoint, tenant, requestor)
        .await
        .map_err(ErrorResponse::from)?;

    let index = id;
    client
        .show(Request::new(rpc::messages::EntityIndex { index }))
        .await
        .map(|msg| Json(msg.into_inner().into()))
        .map_err(ErrorResponse::from)
}

/// Route for creating a new user.
///
/// To use this route, use a POST request, sending as body a JSON containing the
/// expected data for creating a new user.
///
/// Upon success, returns the data for the created user as if it were requested
/// through the `show` method.
#[allow(unused_variables)]
#[openapi(tag = "User")]
#[post("/<tenant>/user", data = "<body>")]
async fn store(
    tenant: String,
    session: SessionInfo,
    body: Json<data::user::RecvUser>,
) -> RestResult<data::user::User> {
    let endpoint = get_endpoint();
    let tenant = session.info.tenant.clone();
    let requestor = session.info.login.clone();

    let message: rpc::messages::User = body.0.into();

    if message.login == *"unknown" {
        return Err(ErrorResponse::BadRequest(
            crate::generic::Message::from("Username \"unknown\" is reserved").json(),
        ));
    }

    info!(
        "{}",
        data::log::format(
            utils::get_ip(),
            &requestor,
            &tenant,
            &format!("POST /user: request USER.store ({})", endpoint),
        )
    );

    let mut client = rpc::user::make_client(endpoint, tenant, requestor)
        .await
        .map_err(ErrorResponse::from)?;

    client
        .store(Request::new(message))
        .await
        .map(|msg| Json(msg.into_inner().into()))
        .map_err(ErrorResponse::from)
}

/// Route for updating data for a user.
///
/// To use this route, use a PUT request. The ID of the user to be updated
/// should also be passed through the URL.
///
/// Ignore `password` or pass it as an empty string if you wish to prevent
/// password updates.
///
/// Upon success, returns the data for the created user as if it were requested
/// through the `show` method.
#[allow(unused_variables)]
#[openapi(tag = "User")]
#[put("/<tenant>/user/<id>", data = "<body>")]
async fn update(
    tenant: String,
    session: SessionInfo,
    id: i32,
    body: Json<data::user::RecvUser>,
) -> RestResult<data::user::User> {
    let endpoint = get_endpoint();
    let tenant = session.info.tenant.clone();
    let requestor = session.info.login.clone();

    info!(
        "{}",
        data::log::format(
            utils::get_ip(),
            &requestor,
            &tenant,
            &format!("PUT /user/<id>: request USER.update ({})", endpoint),
        )
    );

    let mut client = rpc::user::make_client(endpoint, tenant, requestor)
        .await
        .map_err(ErrorResponse::from)?;

    let mut message: rpc::messages::User = body.0.into();
    message.id = Some(id);

    client
        .update(Request::new(message))
        .await
        .map(|msg| Json(msg.into_inner().into()))
        .map_err(ErrorResponse::from)
}

/// Route for removing a user altogether.
///
/// To use this route, use a DELETE request. The ID of the user to be updated
/// should also be passed through the URL.
///
/// Upon success, returns a success message.
#[allow(unused_variables)]
#[openapi(tag = "User")]
#[delete("/<tenant>/user/<index>")]
async fn delete(
    tenant: String,
    session: SessionInfo,
    index: i32,
) -> RestResult<crate::generic::Message> {
    let endpoint = get_endpoint();
    let tenant = session.info.tenant.clone();
    let requestor = session.info.login.clone();

    info!(
        "{}",
        data::log::format(
            utils::get_ip(),
            &requestor,
            &tenant,
            &format!("DELETE /user/<id>: request USER.delete ({})", endpoint),
        )
    );

    let mut client = rpc::user::make_client(endpoint, tenant, requestor)
        .await
        .map_err(ErrorResponse::from)?;

    client
        .delete(Request::new(rpc::messages::EntityIndex { index }))
        .await
        .map(|_| Json(crate::generic::Message::from("User removed successfully")))
        .map_err(ErrorResponse::from)
}