Skip to main content

RequestUser

Trait RequestUser 

Source
pub trait RequestUser {
    // Required method
    fn current_user(&self) -> Option<&IdentityUser>;

    // Provided methods
    fn user_id(&self) -> Option<&str> { ... }
    fn user_name(&self) -> Option<&str> { ... }
    fn user_email(&self) -> Option<&str> { ... }
    fn tenant_id(&self) -> Option<&str> { ... }
    fn roles(&self) -> &[String] { ... }
    fn permissions(&self) -> &[String] { ... }
    fn is_in_role(&self, role: &str) -> bool { ... }
    fn has_permission(&self, permission: &str) -> bool { ... }
    fn require_user(&self) -> Result<&IdentityUser> { ... }
    fn require_role(&self, role: &str) -> Result<()> { ... }
    fn require_tenant(&self) -> Result<&str> { ... }
}
Expand description

Convenience accessors for a request that carries an IdentityUser.

#[derive(WebxRequestMeta)] implements this automatically when the struct has a #[fromclaim] field of type IdentityUser (or #[fromclaim(optional)] of type Option<IdentityUser>).

Required Methods§

Source

fn current_user(&self) -> Option<&IdentityUser>

The authenticated identity, or None for an anonymous request.

Provided Methods§

Source

fn user_id(&self) -> Option<&str>

The subject identifier, or None when anonymous.

Source

fn user_name(&self) -> Option<&str>

The display name, or None when anonymous.

Source

fn user_email(&self) -> Option<&str>

The email address, or None when anonymous.

Source

fn tenant_id(&self) -> Option<&str>

The tenant identifier, or None when anonymous.

Returns Some("") for an authenticated principal without a tenant claim (that is how IdentityUser::tenant_id represents “no claim”); use IdentityUser::tenant or RequestUser::require_tenant when the difference matters.

Source

fn roles(&self) -> &[String]

The roles of the authenticated user (empty when anonymous).

Source

fn permissions(&self) -> &[String]

The permissions of the authenticated user (empty when anonymous).

Source

fn is_in_role(&self, role: &str) -> bool

Whether the authenticated user has role.

Boolean branch only (anonymous → false); it cannot tell “not signed in” from “signed in without the role”. Use RequestUser::require_role for a 401/403 decision.

Source

fn has_permission(&self, permission: &str) -> bool

Whether the authenticated user has permission (anonymous → false).

Source

fn require_user(&self) -> Result<&IdentityUser>

The identity, or 401 Unauthorized when the request is anonymous.

Source

fn require_role(&self, role: &str) -> Result<()>

Require role, separating “not signed in” from “not allowed”.

  • anonymous → 401 Unauthorized (authentication required)
  • authenticated without the role → 403 Forbidden ({role} role required)

The 401/403 split lets clients react correctly: acquire a token, or stop retrying because the token lacks the role.

Source

fn require_tenant(&self) -> Result<&str>

Require a tenant claim, separating “not signed in” from “no tenant scope”.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§