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
6use laravel::http::Request;
7
8/// Who may see the developer tools, beyond what [`DevtoolsServiceProvider`] already checks.
9///
10/// The provider asks the gate for every request that would otherwise see them, in the `web`
11/// middleware group, after the session has started: a gate may ask who is logged in. A gate that
12/// says no, or fails, hides the toolbar and answers the developer tools' own pages with `404`; the
13/// CSRF check may answer before it with `419`, and the router, for a method a route doesn't have,
14/// with `405`, or `200` for `OPTIONS`, and `Allow`. It asks only those the provider lets in, on the application's
15/// machine or with the key, so it hides what the developer tools hold, not that they are there.
16///
17/// ```
18/// use laravel::http::Request;
19/// use laravel_rust_devtools::{DevtoolsGate, DevtoolsServiceProvider};
20///
21/// /// Only those logged in.
22/// struct LoggedIn;
23///
24/// impl DevtoolsGate for LoggedIn {
25/// async fn allows(&self, request: &Request) -> laravel::Result<bool> {
26/// request.authenticated().await
27/// }
28/// }
29///
30/// laravel::Application::configure().with_providers(|providers| {
31/// providers.add(DevtoolsServiceProvider::new().with_gate(LoggedIn));
32/// });
33/// ```
34///
35/// [`DevtoolsServiceProvider`]: crate::DevtoolsServiceProvider
36pub trait DevtoolsGate: Send + Sync + 'static {
37 /// Whether the request may see the developer tools.
38 ///
39 /// # Errors
40 ///
41 /// An error, such as a database that can't be reached, counts as no.
42 fn allows(&self, request: &Request) -> impl Future<Output = laravel::Result<bool>> + Send;
43}
44
45/// A [`DevtoolsGate`] behind a pointer, whatever its type.
46#[cfg(debug_assertions)]
47pub(crate) trait AnyGate: Send + Sync {
48 fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>>;
49}
50
51#[cfg(debug_assertions)]
52impl<G: DevtoolsGate> AnyGate for G {
53 fn allows<'a>(&'a self, request: &'a Request) -> Pin<Box<dyn Future<Output = laravel::Result<bool>> + Send + 'a>> {
54 Box::pin(DevtoolsGate::allows(self, request))
55 }
56}