2018-11-24 12:44:17 +01:00
|
|
|
use rocket::{
|
|
|
|
http::Status,
|
|
|
|
request::{self, FromRequest, Request},
|
|
|
|
Outcome,
|
|
|
|
};
|
2018-07-27 19:05:36 +02:00
|
|
|
|
|
|
|
use users::User;
|
|
|
|
|
|
|
|
/// Wrapper around User to use as a request guard on pages reserved to admins.
|
|
|
|
pub struct Admin(pub User);
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Admin {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Admin, ()> {
|
|
|
|
let user = request.guard::<User>()?;
|
|
|
|
if user.is_admin {
|
|
|
|
Outcome::Success(Admin(user))
|
|
|
|
} else {
|
|
|
|
Outcome::Failure((Status::Unauthorized, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|