Init
This commit is contained in:
@@ -149,3 +149,26 @@ pub async fn require_auth(req: Request<Body>, next: Next) -> Response {
|
||||
HTTPError::Unauthorized.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
/// Middleware de sécurité qui impose que l'utilisateur soit un Superuser (administrateur).
|
||||
pub async fn require_superuser(req: Request<Body>, next: Next) -> Response {
|
||||
let user_opt = req
|
||||
.extensions()
|
||||
.get::<RequestContext>()
|
||||
.and_then(|ctx| ctx.user.as_ref());
|
||||
|
||||
match user_opt {
|
||||
Some(user) if user.is_superuser => {
|
||||
// L'utilisateur est identifié ET est superuser, on continue
|
||||
next.run(req).await
|
||||
}
|
||||
Some(_) => {
|
||||
// Utilisateur connecté mais pas administrateur
|
||||
HTTPError::Forbidden.into_response()
|
||||
}
|
||||
None => {
|
||||
// Pas d'utilisateur authentifié du tout
|
||||
HTTPError::Unauthorized.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user