Repository navigation
Expand file tree
/
Copy pathclients_controller.rs
More file actions
258 lines (237 loc) · 6.19 KB
/
Copy pathclients_controller.rs
File metadata and controls
258 lines (237 loc) · 6.19 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use rocket::form::Form;
use rocket::http::Status;
use rocket::response::content::RawHtml;
use rocket::response::status;
use rocket::response::status::Custom;
use rocket::response::{Redirect, Responder};
use rocket::serde::json::Json;
use crate::DbConn;
use crate::ephemeral::from_api::Api;
use crate::ephemeral::from_api::SplitApi;
use crate::ephemeral::session::{AdminSession, ClientSession};
use crate::errors::Result;
use crate::models::client::*;
use crate::models::role::Role;
use crate::models::user::User;
use crate::views::accepter::Accepter;
// These structs need to be defined separately. Because we use a hidden field in
// the HTML to make sure we always get some value for `needs_grant`, we need
// Rocket to be lenient when parsing the form data. However, the `Lenient`
// struct does not play nice with any other libraries. (So it can't be
// deserialized by serde.)
#[derive(Debug, Deserialize)]
pub struct JsonClientChange {
pub name: Option<String>,
pub needs_grant: Option<bool>,
pub description: Option<String>,
pub redirect_uri_list: Option<String>,
}
#[derive(Debug, FromForm)]
pub struct FormClientChange {
pub name: Option<String>,
pub needs_grant: Vec<bool>,
pub description: Option<String>,
pub redirect_uri_list: Option<String>,
}
impl std::convert::From<JsonClientChange> for ClientChange {
fn from(val: JsonClientChange) -> Self {
ClientChange {
name: val.name,
needs_grant: val.needs_grant,
description: val.description,
redirect_uri_list: val.redirect_uri_list,
}
}
}
impl std::convert::From<FormClientChange> for ClientChange {
fn from(val: FormClientChange) -> Self {
ClientChange {
name: val.name,
needs_grant: val.needs_grant.last().cloned(),
description: val.description,
redirect_uri_list: val.redirect_uri_list,
}
}
}
#[get("/clients")]
pub async fn list_clients<'r>(
// from headers
session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let clients = Client::all(&db).await?;
Ok(Accepter {
html: RawHtml(template!("clients/index.html", {
clients: Vec<Client> = clients.clone(),
current_user: User = session.admin,
})),
json: Json(clients),
})
}
#[get("/clients/<id>/edit")]
pub async fn update_client_page<'r>(
// from url
id: i32,
// from headers
session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
let roles = Role::all(&db).await?;
Ok(RawHtml(template!("clients/edit_client.html", {
current_user: User = session.admin,
client: Client = client.clone(),
client_roles: Vec<Role> = client.roles(&db).await?,
roles: Vec<Role> = roles
})))
}
#[put("/clients/<id>", data = "<change>")]
pub async fn update_client<'r>(
// from url
id: i32,
// from body
change: SplitApi<FormClientChange, JsonClientChange, ClientChange>,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let mut client = Client::find(id, &db).await?;
client.change_with(change.into_inner())?;
client.update(&db).await?;
Ok(Accepter {
html: Redirect::to(uri!(list_clients)),
json: Custom(Status::NoContent, ()),
})
}
#[delete("/clients/<id>")]
pub async fn delete_client<'r>(
// from url
id: i32,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
client.delete(&db).await?;
Ok(Accepter {
html: Redirect::to(uri!(list_clients)),
json: Custom(Status::NoContent, ()),
})
}
#[post("/clients", data = "<client>")]
pub async fn create_client<'r>(
// from body
client: Api<NewClient>,
// from headers
_admin: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::create(client.into_inner(), &db).await?;
Ok(Accepter {
html: Redirect::to(uri!(update_client_page(client.id))),
json: status::Created::new(String::from("/client")).body(Json(client)),
})
}
#[get("/clients/<id>/generate_secret")]
pub async fn get_generate_secret<'r>(
// from url
id: i32,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
Ok(RawHtml(template!("clients/confirm_generate_secret.html", {
client: Client = client,
})))
}
#[post("/clients/<id>/generate_secret")]
pub async fn post_generate_secret<'r>(
// from url
id: i32,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
let client = client.generate_secret(&db).await?;
Ok(Accepter {
html: Redirect::to(uri!(update_client_page(client.id))),
json: Custom(Status::NoContent, ()),
})
}
#[derive(Serialize)]
pub struct ClientInfo {
id: i32,
name: String,
roles: Vec<String>,
}
impl ClientInfo {
async fn new(client: Client, db: &DbConn) -> Result<Self> {
let roles = client
.clone()
.roles(db)
.await?
.iter()
.map(|r| r.clone().name)
.collect();
Ok(ClientInfo {
id: client.id,
name: client.name,
roles,
})
}
}
#[get("/current_client")]
pub async fn current_client(
// from headers
session: ClientSession,
// injected
db: DbConn,
) -> Result<Json<ClientInfo>> {
Ok(Json(ClientInfo::new(session.client, &db).await?))
}
#[post("/clients/<id>/roles", data = "<role_id>")]
pub async fn add_role<'r>(
// from url
id: i32,
// from body
role_id: Form<i32>,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(*role_id, &db).await?;
let client = Client::find(id, &db).await?;
role.add_client(client.id, &db).await?;
Ok(Accepter {
html: Redirect::to(uri!(update_client_page(client.id))),
json: Custom(Status::NoContent, ()),
})
}
#[delete("/clients/<id>/roles/<role_id>")]
pub async fn delete_role<'r>(
// from url
id: i32,
role_id: i32,
// from headers
_session: AdminSession,
// injected
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let role = Role::find(role_id, &db).await?;
let client = Client::find(id, &db).await?;
role.remove_client(client.id, &db).await?;
Ok(Accepter {
html: Redirect::to(uri!(update_client_page(client.id))),
json: Custom(Status::NoContent, ()),
})
}