/
users_controller.rs
263 lines (244 loc) · 6.66 KB
/
users_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
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
259
260
261
262
263
use rocket::http::Status;
use rocket::response::status::Custom;
use rocket::response::{Redirect, Responder};
use rocket_contrib::json::Json;
use std::fmt::Debug;
use validator::ValidationErrors;
use crate::config::Config;
use crate::ephemeral::from_api::Api;
use crate::ephemeral::session::{AdminSession, UserSession};
use crate::errors::Either::{self, Left, Right};
use crate::errors::{InternalError, OneOf, Result, ZauthError};
use crate::mailer::Mailer;
use crate::models::user::*;
use crate::views::accepter::Accepter;
use crate::{util, DbConn};
use askama::Template;
use chrono::{Duration, Utc};
use rocket::request::Form;
use rocket::State;
#[get("/current_user")]
pub fn current_user(session: UserSession) -> Json<User> {
Json(session.user)
}
#[get("/users/<id>")]
pub fn show_user(
session: UserSession,
conn: DbConn,
id: i32,
) -> Result<impl Responder<'static>> {
let user = User::find(id, &conn)?;
// Check whether the current session is allowed to view this user
if session.user.admin || session.user.id == id {
Ok(Accepter {
html: template!("users/show.html";
user: User = user.clone(),
current_user: User = session.user,
),
json: Json(user),
})
} else {
Err(ZauthError::not_found(&format!(
"User with id {} not found",
id
)))
}
}
#[get("/users")]
pub fn list_users(
session: UserSession,
conn: DbConn,
) -> Result<impl Responder<'static>> {
let users = User::all(&conn)?;
Ok(Accepter {
html: template! {
"users/index.html";
users: Vec<User> = users.clone(),
current_user: User = session.user,
},
json: Json(users),
})
}
#[post("/users", data = "<user>")]
pub fn create_user(
_session: AdminSession,
user: Api<NewUser>,
conn: DbConn,
config: State<Config>,
) -> Result<impl Responder<'static>> {
let user = User::create(user.into_inner(), config.bcrypt_cost, &conn)
.map_err(ZauthError::from)?;
Ok(Accepter {
html: Redirect::to(uri!(show_user: user.id)),
json: Json(user),
})
}
#[get("/register")]
pub fn register_page() -> Result<impl Responder<'static>> {
Ok(
template! { "users/registration_form.html"; errors: Option<ValidationErrors> = None },
)
}
#[post("/register", data = "<user>")]
pub fn register(
user: Api<NewUser>,
conf: State<Config>,
conn: DbConn,
) -> Result<Either<impl Responder<'static>, impl Responder<'static>>> {
match User::create_pending(user.into_inner(), conf.bcrypt_cost, &conn) {
Ok(user) => Ok(Left(Accepter {
html: Custom(
Status::Created,
template!("users/registration_success.html"),
),
json: Custom(Status::Created, Json(user)),
})),
Err(ZauthError::ValidationError(errors)) => Ok(Right(Accepter {
html: Custom(
Status::UnprocessableEntity,
template! {
"users/registration_form.html";
errors: Option<ValidationErrors> = Some(errors.clone()),
},
),
json: Custom(Status::UnprocessableEntity, Json(errors)),
})),
Err(other) => Err(other),
}
}
#[put("/users/<id>", data = "<change>")]
pub fn update_user(
id: i32,
change: Api<UserChange>,
session: UserSession,
conf: State<Config>,
conn: DbConn,
) -> Result<
Either<impl Responder<'static>, Custom<impl Debug + Responder<'static>>>,
> {
let mut user = User::find(id, &conn)?;
if session.user.id == user.id || session.user.admin {
user.change_with(change.into_inner(), conf.bcrypt_cost)?;
let user = user.update(&conn)?;
Ok(Left(Accepter {
html: Redirect::to(uri!(show_user: user.id)),
json: Custom(Status::NoContent, ()),
}))
} else {
Ok(Right(Custom(Status::Forbidden, ())))
}
}
#[post("/users/<id>/admin", data = "<value>")]
pub fn set_admin(
id: i32,
value: Api<ChangeAdmin>,
_session: AdminSession,
conn: DbConn,
) -> Result<impl Responder<'static>> {
let mut user = User::find(id, &conn)?;
user.admin = value.into_inner().admin;
let user = user.update(&conn)?;
Ok(Accepter {
html: Redirect::to(uri!(show_user: user.id)),
json: Custom(Status::NoContent, ()),
})
}
#[get("/users/forgot_password")]
pub fn forgot_password_get() -> impl Responder<'static> {
template! { "users/forgot_password.html" }
}
#[derive(Debug, FromForm, Deserialize)]
pub struct ResetPassword {
for_email: String,
}
#[post("/users/forgot_password", data = "<value>")]
pub fn forgot_password_post(
value: Form<ResetPassword>,
conn: DbConn,
mailer: State<Mailer>,
) -> Result<impl Responder<'static>> {
let for_email = value.into_inner().for_email;
let user = match User::find_by_email(&for_email, &conn) {
Ok(user) if user.is_active() => Ok(Some(user)),
Ok(_user) => Ok(None),
Err(ZauthError::NotFound(_)) => Ok(None),
Err(other) => Err(other),
}?;
if let Some(mut user) = user {
user.password_reset_token = Some(util::random_token(32));
user.password_reset_expiry =
Some(Utc::now().naive_utc() + Duration::days(1));
let user = user.update(&conn)?;
let token = user.password_reset_token.as_ref().unwrap();
let reset_url = uri!(reset_password_get: token);
mailer.try_create(
&user,
String::from("[Zauth] You've requested a password reset"),
template!(
"mails/password_reset_token.txt";
name: String = user.username.to_string(),
reset_url: String = reset_url.to_string(),
)
.render()
.map_err(InternalError::from)?,
)?
};
Ok(template! {
"users/reset_link_sent.html";
email: String = for_email
})
}
#[get("/users/reset_password/<token>")]
pub fn reset_password_get(token: String) -> impl Responder<'static> {
template! {
"users/reset_password_form.html";
token: String = token,
errors: Option<String> = None,
}
}
#[derive(Debug, FromForm)]
pub struct PasswordReset {
token: String,
new_password: String,
}
#[post("/users/reset_password", data = "<form>")]
pub fn reset_password_post(
form: Form<PasswordReset>,
conn: DbConn,
conf: State<Config>,
mailer: State<Mailer>,
) -> Result<impl Responder<'static>> {
let form = form.into_inner();
if let Some(user) = User::find_by_token(&form.token, &conn)? {
match user.change_password(&form.new_password, conf.bcrypt_cost, &conn)
{
Ok(user) => {
let body = template!(
"mails/password_reset_success.txt";
name: String = user.username.to_string(),
)
.render()
.map_err(InternalError::from)?;
mailer.create(
&user,
String::from("[Zauth] Your password has been reset"),
body,
)?;
Ok(OneOf::One(
template! { "users/reset_password_success.html" },
))
},
Err(err) => {
let template = template! {
"users/reset_password_form.html";
token: String = form.token,
errors: Option<String> = Some(err.to_string()),
};
Ok(OneOf::Two(Custom(Status::UnprocessableEntity, template)))
},
}
} else {
let template = template! { "users/reset_password_invalid.html" };
Ok(OneOf::Three(Custom(Status::Forbidden, template)))
}
}