use laravel::http::{Middleware, Next, Request, Response, StatusCode};
use crate::panel::PanelGate;
#[derive(Clone)]
pub(crate) struct AuthorizePanel {
gate: PanelGate,
}
impl AuthorizePanel {
pub(crate) fn new(gate: PanelGate) -> Self {
Self { gate }
}
}
impl Middleware for AuthorizePanel {
async fn handle(&self, request: Request, next: Next) -> laravel::Result<Response> {
let allowed = (self.gate)(&request).await?;
if !allowed {
return Err(laravel::abort_with(
StatusCode::FORBIDDEN,
"You do not have access to this admin panel.",
));
}
Ok(next.run(request).await)
}
}
pub(crate) const LOCKED_MESSAGE: &str = "This admin panel has no access control configured, so it only opens in the `local` and \
`testing` environments. Add `AdminPanel::authorize(..)` or `AdminPanel::middleware(..)`.";
#[derive(Clone, Copy)]
pub(crate) struct LockedPanel;
impl Middleware for LockedPanel {
async fn handle(&self, _request: Request, _next: Next) -> laravel::Result<Response> {
Err(laravel::abort_with(StatusCode::FORBIDDEN, LOCKED_MESSAGE))
}
}