laravel_rust_devtools/gate.rs
1//! The condition an application may put on who sees the developer tools.
2
3#[cfg(debug_assertions)]
4use std::pin::Pin;
5#[cfg(debug_assertions)]
6use std::sync::Arc;
7
8use laravel::http::Request;
9
10/// Who may see the developer tools, beyond what [`DevtoolsServiceProvider`] already checks, or
11/// instead of it.
12///
13/// The provider asks the gate in the `web` middleware group, after the session has started: a gate
14/// may ask who is logged in. A gate that says no, or fails, hides the toolbar and answers the
15/// developer tools' own pages with `404`. How the provider sets it decides whom it asks:
16///
17/// - Set with [`with_gate`], it narrows: it is asked only for those the provider lets in, on the
18/// application's machine or with the key. The CSRF check may answer before it with `419`, and the
19/// router, for a method a route doesn't have, with `405`, or `200` for `OPTIONS`, and `Allow`: it
20/// hides what the developer tools hold, not that they are there.
21/// - Set with [`with_admitting_gate`], it also lets in on its own: it is asked for everyone else,
22/// such as a browser behind a proxy without the key. Whom it turns away gets what the application
23/// answers a path that doesn't exist, as long as its fallback answers with `404` as
24/// `laravel::abort(404)` does.
25///
26/// ```
27/// use laravel::http::Request;
28/// use laravel_rust_devtools::{DevtoolsGate, DevtoolsServiceProvider};
29///
30/// /// Only those logged in.
31/// struct LoggedIn;
32///
33/// impl DevtoolsGate for LoggedIn {
34/// async fn allows(&self, request: &Request) -> laravel::Result<bool> {
35/// request.authenticated().await
36/// }
37/// }
38///
39/// laravel::Application::configure().with_providers(|providers| {
40/// providers.add(DevtoolsServiceProvider::new().with_gate(LoggedIn));
41/// });
42/// ```
43///
44/// [`DevtoolsServiceProvider`]: crate::DevtoolsServiceProvider
45/// [`with_gate`]: crate::DevtoolsServiceProvider::with_gate
46/// [`with_admitting_gate`]: crate::DevtoolsServiceProvider::with_admitting_gate
47pub trait DevtoolsGate: Send + Sync + 'static {
48 /// Whether the request may see the developer tools.
49 ///
50 /// # Errors
51 ///
52 /// An error, such as a database that can't be reached, counts as no.
53 fn allows(&self, request: &Request) -> impl Future<Output = laravel::Result<bool>> + Send;
54}
55
56/// A [`DevtoolsGate`] behind a pointer, whatever its type.
57#[cfg(debug_assertions)]
58pub(crate) trait AnyGate: Send + Sync {
59 fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>>;
60}
61
62#[cfg(debug_assertions)]
63impl<G: DevtoolsGate> AnyGate for G {
64 fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>> {
65 Box::pin(DevtoolsGate::allows(self, request))
66 }
67}
68
69/// The gate as the application set it, with whom the provider asks it.
70#[cfg(debug_assertions)]
71#[derive(Clone)]
72pub(crate) enum Gate {
73 /// Asked for whom the rules let in, and may turn them away.
74 Narrowing(Arc<dyn AnyGate>),
75 /// Asked for whom the rules keep out, and may let them in.
76 Admitting(Arc<dyn AnyGate>),
77}