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, Error> { ... }
fn require_role(&self, role: &str) -> Result<(), Error> { ... }
fn require_tenant(&self) -> Result<&str, Error> { ... }
}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§
Sourcefn current_user(&self) -> Option<&IdentityUser>
fn current_user(&self) -> Option<&IdentityUser>
The authenticated identity, or None for an anonymous request.
Provided Methods§
Sourcefn user_email(&self) -> Option<&str>
fn user_email(&self) -> Option<&str>
The email address, or None when anonymous.
Sourcefn tenant_id(&self) -> Option<&str>
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.
Sourcefn permissions(&self) -> &[String]
fn permissions(&self) -> &[String]
The permissions of the authenticated user (empty when anonymous).
Sourcefn is_in_role(&self, role: &str) -> bool
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.
Sourcefn has_permission(&self, permission: &str) -> bool
fn has_permission(&self, permission: &str) -> bool
Whether the authenticated user has permission (anonymous → false).
Sourcefn require_user(&self) -> Result<&IdentityUser, Error>
fn require_user(&self) -> Result<&IdentityUser, Error>
The identity, or 401 Unauthorized when the request is anonymous.
Sourcefn require_role(&self, role: &str) -> Result<(), Error>
fn require_role(&self, role: &str) -> Result<(), Error>
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.
Sourcefn require_tenant(&self) -> Result<&str, Error>
fn require_tenant(&self) -> Result<&str, Error>
Require a tenant claim, separating “not signed in” from “no tenant scope”.
- anonymous →
401 Unauthorized - authenticated without a tenant claim → delegates to
IdentityUser::require_tenant(403 Forbidden)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".