mirror of
https://git.joinplu.me/Plume/Plume.git
synced 2024-11-24 04:21:00 +00:00
Add an admin inclusive request guard #1156
This commit is contained in:
parent
a21ab5c2a8
commit
d004a7047b
2 changed files with 19 additions and 2 deletions
|
@ -5,7 +5,7 @@ use rocket::{
|
|||
Outcome,
|
||||
};
|
||||
|
||||
/// Wrapper around User to use as a request guard on pages reserved to admins.
|
||||
/// Wrapper around User to use as a request guard on pages exclusively reserved to admins.
|
||||
pub struct Admin(pub User);
|
||||
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Admin {
|
||||
|
@ -21,6 +21,23 @@ impl<'a, 'r> FromRequest<'a, 'r> for Admin {
|
|||
}
|
||||
}
|
||||
|
||||
/// Same as `Admin` but it forwards to next guard if the user is not an admin.
|
||||
/// It's useful when there are multiple implementations of routes for admin and moderator.
|
||||
pub struct InclusiveAdmin(pub User);
|
||||
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for InclusiveAdmin {
|
||||
type Error = ();
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<InclusiveAdmin, ()> {
|
||||
let user = request.guard::<User>()?;
|
||||
if user.is_admin() {
|
||||
Outcome::Success(InclusiveAdmin(user))
|
||||
} else {
|
||||
Outcome::Forward(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as `Admin` but for moderators.
|
||||
pub struct Moderator(pub User);
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ pub fn index(conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
|||
}
|
||||
|
||||
#[get("/admin")]
|
||||
pub fn admin(_admin: Admin, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
pub fn admin(_admin: InclusiveAdmin, conn: DbConn, rockets: PlumeRocket) -> Result<Ructe, ErrorPage> {
|
||||
let local_inst = Instance::get_local()?;
|
||||
Ok(render!(instance::admin(
|
||||
&(&conn, &rockets).to_context(),
|
||||
|
|
Loading…
Reference in a new issue