2020-01-21 07:02:03 +01:00
|
|
|
use crate::users::User;
|
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
|
|
|
|
|
|
|
/// 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>()?;
|
2019-09-13 12:28:36 +02:00
|
|
|
if user.is_admin() {
|
2018-07-27 19:05:36 +02:00
|
|
|
Outcome::Success(Admin(user))
|
|
|
|
} else {
|
|
|
|
Outcome::Failure((Status::Unauthorized, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 12:28:36 +02:00
|
|
|
|
|
|
|
/// Same as `Admin` but for moderators.
|
|
|
|
pub struct Moderator(pub User);
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Moderator {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Moderator, ()> {
|
|
|
|
let user = request.guard::<User>()?;
|
|
|
|
if user.is_moderator() {
|
|
|
|
Outcome::Success(Moderator(user))
|
|
|
|
} else {
|
|
|
|
Outcome::Failure((Status::Unauthorized, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|