pub struct GateBuilder { /* private fields */ }Expand description
Builds a Gate by registering checks in evaluation order.
Implementations§
Source§impl GateBuilder
impl GateBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
A builder with no checks, in Mode::Open.
Sourcepub fn check<C: Check + 'static>(self, check: C) -> Self
pub fn check<C: Check + 'static>(self, check: C) -> Self
Register a check. Checks evaluate in the order they are registered.
Sourcepub fn check_boxed(self, check: Box<dyn Check>) -> Self
pub fn check_boxed(self, check: Box<dyn Check>) -> Self
Register a pre-boxed check (for dynamically-assembled check sets).
Sourcepub fn closed(self) -> Self
pub fn closed(self) -> Self
Evaluate in Mode::Closed: deny when no check decides.
use action_gate_core::{closed, ActionContext, Check, Decision, Gate};
struct AllowRead;
impl Check for AllowRead {
fn id(&self) -> &str { "allow-read" }
fn evaluate(&self, ctx: &ActionContext) -> Option<Decision> {
(ctx.action == "read").then(Decision::allow)
}
}
let gate = Gate::builder().check(AllowRead).closed().build();
assert!(gate.evaluate(&ActionContext::new("read")).is_allow());
let d = gate.evaluate(&ActionContext::new("write"));
assert_eq!(d.reason, closed::NO_CHECK_DECIDED);
assert!(d.blocking);Sourcepub fn require(self, id: impl Into<String>) -> Self
pub fn require(self, id: impl Into<String>) -> Self
Require the check with id: it must be registered and must return
Some on every evaluation, or the gate denies. Requiring a check puts
the gate in Mode::Closed; there is no open gate with requirements.
The requirement may be stated before or after the check is registered.
use action_gate_core::{closed, ActionContext, Check, Decision, Gate};
struct Scan;
impl Check for Scan {
fn id(&self) -> &str { "scan" }
fn evaluate(&self, _ctx: &ActionContext) -> Option<Decision> {
Some(Decision::allow())
}
}
// Required but never registered: every action is denied.
let gate = Gate::builder().require("scan").build();
let d = gate.evaluate(&ActionContext::new("write"));
assert_eq!(d.reason, closed::REQUIRED_UNREGISTERED);
assert_eq!(d.check_ids, vec!["scan"]);
let gate = Gate::builder().check(Scan).require("scan").build();
assert!(gate.evaluate(&ActionContext::new("write")).is_allow());Sourcepub fn require_all(
self,
ids: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn require_all( self, ids: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Require every id in ids; see require.
Sourcepub fn build_deny_by_default(self) -> Gate
pub fn build_deny_by_default(self) -> Gate
Register DenyByDefault as the terminal check and finish building:
the gate denies any action no earlier check decides. An open-mode
shorthand; see DenyByDefault on combining it with closed.