pub struct PolicyContext {
pub session: Session,
pub user_id: Option<String>,
pub roles: Vec<String>,
pub pool: Option<Pool<AsyncPgConnection>>,
pub policy_registry: PolicyRegistry,
}Expand description
Per-request context handed to every policy and scope check.
Carries the resolved Session, the authenticated user id (when
present), the active role set, the PolicyRegistry (so
Post::scope(&ctx) can resolve a registered scope without
re-threading state), and a clone of the database pool so
policies can consult related rows. Clone + Send + Sync — flows
freely across .await points.
Fields§
§session: SessionThe full per-request Session. Read raw values via
Session::get when a policy needs data beyond the
canonical user-id and role keys.
user_id: Option<String>The authenticated user id, if any. Mirrors the configured
session auth key (default: "user_id").
roles: Vec<String>Active role set for the current user. Empty when the user has no role or is anonymous.
pool: Option<Pool<AsyncPgConnection>>Database connection pool, cloned from AppState. Policies
that need to consult related rows (e.g. group membership)
can borrow a connection here.
policy_registry: PolicyRegistryRegistered Policy / Scope map, cloned from
AppState. Lets the Scoped blanket trait resolve a
registered scope from &ctx alone — the
Post::scope(&ctx).load(&mut db).await? ergonomic the
authorization guide documents.
Implementations§
Source§impl PolicyContext
impl PolicyContext
Sourcepub async fn from_session(session: &Session, auth_session_key: &str) -> Self
pub async fn from_session(session: &Session, auth_session_key: &str) -> Self
Build a PolicyContext from a session alone.
The resulting context has an empty PolicyRegistry and no
pool — sufficient for hand-rolled policy unit tests that
don’t go through AppState. Production code paths construct
a PolicyContext via from_request
instead.
Sourcepub async fn from_request(state: &AppState, session: &Session) -> Self
pub async fn from_request(state: &AppState, session: &Session) -> Self
Build a fully-populated PolicyContext from AppState +
Session. Used by the #[authorize] macro and
#[repository(policy = ...)]-generated handlers.
Sourcepub const fn is_authenticated(&self) -> bool
pub const fn is_authenticated(&self) -> bool
Returns true when the request has a resolved authenticated user.
Sourcepub fn user_id_i64(&self) -> Option<i64>
pub fn user_id_i64(&self) -> Option<i64>
Returns the user id parsed as an i64, when the session
stored it as a numeric string. Convenient for the common
case of BIGSERIAL primary keys.
Sourcepub fn has_role(&self, role: &str) -> bool
pub fn has_role(&self, role: &str) -> bool
Returns true when the active role set contains role.
Sourcepub fn has_any_role<I, S>(&self, candidates: I) -> bool
pub fn has_any_role<I, S>(&self, candidates: I) -> bool
Returns true when the active role set contains any of the
supplied roles. Mirrors #[secured("a", "b")] semantics.
Sourcepub fn with_pool(self, pool: Pool<AsyncPgConnection>) -> Self
pub fn with_pool(self, pool: Pool<AsyncPgConnection>) -> Self
Attach a database pool to the context. Used by the framework when constructing the context inside extractors; tests can also call this to inject a pool by hand.
Trait Implementations§
Source§impl Clone for PolicyContext
impl Clone for PolicyContext
Source§fn clone(&self) -> PolicyContext
fn clone(&self) -> PolicyContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PolicyContext
impl !RefUnwindSafe for PolicyContext
impl Send for PolicyContext
impl Sync for PolicyContext
impl Unpin for PolicyContext
impl UnsafeUnpin for PolicyContext
impl !UnwindSafe for PolicyContext
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> RepositoryHooksClone for Twhere
T: Clone,
impl<T> RepositoryHooksClone for Twhere
T: Clone,
Source§fn autumn_clone(&self) -> T
fn autumn_clone(&self) -> T
Source§impl<T, Conn> RunQueryDsl<Conn> for T
impl<T, Conn> RunQueryDsl<Conn> for T
Source§fn execute<'conn, 'query>(
self,
conn: &'conn mut Conn,
) -> <Conn as AsyncConnectionCore>::ExecuteFuture<'conn, 'query>
fn execute<'conn, 'query>( self, conn: &'conn mut Conn, ) -> <Conn as AsyncConnectionCore>::ExecuteFuture<'conn, 'query>
Source§fn load<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
fn load<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
Source§fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnectionCore,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
fn load_stream<'conn, 'query, U>(
self,
conn: &'conn mut Conn,
) -> Self::LoadFuture<'conn>where
Conn: AsyncConnectionCore,
U: 'conn,
Self: LoadQuery<'query, Conn, U> + 'query,
Stream] with the returned rows. Read moreSource§fn get_result<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, LoadNext<Pin<Box<Self::Stream<'conn>>>>>
fn get_result<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, LoadNext<Pin<Box<Self::Stream<'conn>>>>>
Source§fn get_results<'query, 'conn, U>(
self,
conn: &'conn mut Conn,
) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
fn get_results<'query, 'conn, U>( self, conn: &'conn mut Conn, ) -> AndThen<Self::LoadFuture<'conn>, TryCollect<Self::Stream<'conn>, Vec<U>>>
Vec with the affected rows. Read moreSource§impl<T> Scoped for T
impl<T> Scoped for T
Source§fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
ScopeQuery for this type. Resolves the
registered scope at .load() time, not here.