pub struct Identity {
pub user_id: String,
pub is_staff: bool,
pub is_superuser: bool,
pub extras: HashMap<String, Value>,
}Expand description
Who the request belongs to, after authentication.
The shape is intentionally narrow: user_id, is_staff, and
is_superuser cover most permission checks. An extras map carries
app-specific bits (role names, organisation id, scope strings) for
custom permission impls.
Fields§
§user_id: StringThe authenticated user’s primary key, stringified so the same Identity shape
works whether the active user model has an i64, String, or UUID primary key.
The framework’s own permissions plugin and session store speak strings.
To get the typed key back, use Identity::pk — not .parse(). This
doc-comment used to say “parse on demand (identity.user_id.parse::<i64>())”,
and a live consumer duly wrote that expression ~19 times, each with its own
bespoke error branch for a failure that cannot happen. Documentation that hands
you a snippet is documentation that decides your code; this one was teaching the
boilerplate it should have been replacing. (gaps3 #57.)
In a handler, prefer not to touch this field at all — the
RequireAuth<T> / RequireStaff extractors hand you the typed key in the
signature, so a handler that forgot to authenticate cannot be written.
is_staff: boolStaff flag. Used by the
built-in IsStaff permission class in umbral-rest.
is_superuser: boolSuperuser flag. A superuser bypasses all permission checks in the built-in permission classes; custom permission impls can consult this field to grant unconditional access.
extras: HashMap<String, Value>App-specific extras a permission check might want to consult.
umbral-auth doesn’t populate this; user-defined auth backends
can stuff role names, organisation ids, etc. here.
Implementations§
Source§impl Identity
impl Identity
Sourcepub fn pk<T: FromStr>(&self) -> Result<T, IdentityPkError>
pub fn pk<T: FromStr>(&self) -> Result<T, IdentityPkError>
The user’s primary key, typed (gaps3 #57).
user_id is a String because the framework supports i64, String and UUID
primary keys behind one Identity shape. This converts it back:
let uid: i64 = identity.pk()?;Err carries the unparseable value, which is the only useful thing to say about
it — but note that in a correctly-configured app this cannot fail: the string was
produced by Display on that very key type. That is precisely why hand-writing
.parse::<i64>().map_err(|_| some_500())? at every call site is waste: it is an
error branch for an impossible state, repeated once per handler.
Sourcepub fn user(user_id: impl ToString) -> Self
pub fn user(user_id: impl ToString) -> Self
Convenience constructor for a non-staff user. Accepts any
stringifiable PK — Identity::user(42), Identity::user("42"),
or Identity::user(uuid.to_string()) all work because the
argument is impl ToString.
Sourcepub fn with_staff(self, is_staff: bool) -> Self
pub fn with_staff(self, is_staff: bool) -> Self
Set the staff flag explicitly. Chainable.
Sourcepub fn with_superuser(self, is_superuser: bool) -> Self
pub fn with_superuser(self, is_superuser: bool) -> Self
Set the superuser flag explicitly. Chainable.
Sourcepub fn with_extra(self, key: impl Into<String>, value: Value) -> Self
pub fn with_extra(self, key: impl Into<String>, value: Value) -> Self
Insert an extras entry. Chainable.
Sourcepub fn user_pk<T: FromStr>(&self) -> Result<T, T::Err>
pub fn user_pk<T: FromStr>(&self) -> Result<T, T::Err>
Parse the stringified user_id back into the caller’s
primary-key type.
Identity::user_id is a String — the lowest common denominator across
i64 / String / UUID user models. Rather than hand-roll
identity.user_id.parse::<i64>().map_err(|_| /* 401 */)? in every scoped
handler (the pattern a live consumer repeated ~8×), call
identity.user_pk::<i64>()?. Generic over any T: FromStr, so it works
for numeric, string, and UUID keys alike; a parse mismatch is returned
as T::Err so the caller owns the HTTP shape (usually a 401/400).
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Identity
impl<'de> Deserialize<'de> for Identity
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Identity
impl RefUnwindSafe for Identity
impl Send for Identity
impl Sync for Identity
impl Unpin for Identity
impl UnsafeUnpin for Identity
impl UnwindSafe for Identity
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);