-
Notifications
You must be signed in to change notification settings - Fork 2
/
clients_controller.rs
158 lines (144 loc) · 4.24 KB
/
clients_controller.rs
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
use rocket::http::Status;
use rocket::response::status;
use rocket::response::status::Custom;
use rocket::response::{Redirect, Responder};
use rocket::serde::json::Json;
use std::fmt::Debug;
use crate::ephemeral::from_api::Api;
use crate::ephemeral::from_api::SplitApi;
use crate::ephemeral::session::AdminSession;
use crate::errors::Result;
use crate::models::client::*;
use crate::models::user::User;
use crate::views::accepter::Accepter;
use crate::DbConn;
// 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(Deserialize, Debug)]
pub struct JsonClientChange {
pub name: Option<String>,
pub needs_grant: Option<bool>,
pub description: Option<String>,
pub redirect_uri_list: Option<String>,
}
#[derive(FromForm, Debug)]
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>(
db: DbConn,
session: AdminSession,
) -> Result<impl Responder<'r, 'static>> {
let clients = Client::all(&db).await?;
Ok(Accepter {
html: 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>(
id: i32,
session: AdminSession,
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
Ok(template! { "clients/edit_client.html";
current_user: User = session.admin,
client: Client = client,
})
}
#[put("/clients/<id>", data = "<change>")]
pub async fn update_client<'r>(
id: i32,
change: SplitApi<FormClientChange, JsonClientChange, ClientChange>,
_session: AdminSession,
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>(
id: i32,
_session: AdminSession,
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>(
client: Api<NewClient>,
db: DbConn,
_admin: AdminSession,
) -> 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>(
id: i32,
_session: AdminSession,
db: DbConn,
) -> Result<impl Responder<'r, 'static>> {
let client = Client::find(id, &db).await?;
Ok(template! { "clients/confirm_generate_secret.html";
client: Client = client,
})
}
#[post("/clients/<id>/generate_secret")]
pub async fn post_generate_secret<'r>(
id: i32,
_session: AdminSession,
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, ()),
})
}