pub struct UserContext {
pub user_id: i64,
pub is_super: bool,
pub roles: Vec<String>,
pub permissions: Vec<String>,
}Expand description
用户上下文 — 扩展 AuthenticatedUser,提供权限信息
由业务层在登录时构建并注入到 request extensions(在 Auth 中间件之后、Guard 之前)。
Guard 通过 UserContext 进行权限判断。
§PHP 对齐
对齐 PHP szoa RBAC 模型:
is_super=true绕过所有 RBAC(对齐 PHPis_super=1)roles对齐 PHProle表(通过user_role关联)permissions对齐 PHPaccess表(格式:controller/action,如user/list)
§注入时机
Auth 中间件(注入 AuthenticatedUser)
↓
业务中间件(查 DB 获取 roles/permissions,注入 UserContext)
↓
Guard(基于 UserContext 进行鉴权决策)
↓
Handler如果业务代码未注入 UserContext,AdminGuard / PermissionGuard / RoleGuard
将返回 Forbidden(无权限访问)。
Fields§
§user_id: i64用户 ID(对齐 AuthenticatedUser::user_id)
is_super: bool是否超级管理员(对齐 PHP is_super=1,绕过所有 RBAC)
roles: Vec<String>角色列表(对齐 PHP role 表)
permissions: Vec<String>权限列表(对齐 PHP access 表,格式:controller/action)
Implementations§
Source§impl UserContext
impl UserContext
Sourcepub fn new(user_id: i64) -> UserContext
pub fn new(user_id: i64) -> UserContext
创建 UserContext(默认非超级管理员,无角色,无权限)
Sourcepub fn with_super(self, is_super: bool) -> UserContext
pub fn with_super(self, is_super: bool) -> UserContext
设置是否超级管理员
Sourcepub fn with_roles(self, roles: Vec<String>) -> UserContext
pub fn with_roles(self, roles: Vec<String>) -> UserContext
设置角色列表
Sourcepub fn with_permissions(self, permissions: Vec<String>) -> UserContext
pub fn with_permissions(self, permissions: Vec<String>) -> UserContext
设置权限列表
Sourcepub fn has_permission(&self, permission: &str) -> bool
pub fn has_permission(&self, permission: &str) -> bool
检查是否具有指定权限
对齐 PHP AuthService::check() 权限检查:
- 精确匹配:
user/list==user/list→ true - 通配符匹配:
user/*匹配user/list→ true(对齐 PHP 通配符权限)
注意:is_super=true 的绕过逻辑由 Guard 负责,本方法仅检查权限列表。
Trait Implementations§
Source§impl Clone for UserContext
impl Clone for UserContext
Source§fn clone(&self) -> UserContext
fn clone(&self) -> UserContext
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for UserContext
impl Debug for UserContext
Source§impl Default for UserContext
impl Default for UserContext
Source§fn default() -> UserContext
fn default() -> UserContext
Returns the “default value” for a type. Read more
Source§impl From<AuthenticatedUser> for UserContext
impl From<AuthenticatedUser> for UserContext
Source§fn from(user: AuthenticatedUser) -> UserContext
fn from(user: AuthenticatedUser) -> UserContext
从 AuthenticatedUser 创建 UserContext
默认非超级管理员,无角色,无权限(业务层需后续调用 with_super / with_roles
/ with_permissions 补充权限信息)。
Auto Trait Implementations§
impl Freeze for UserContext
impl RefUnwindSafe for UserContext
impl Send for UserContext
impl Sync for UserContext
impl Unpin for UserContext
impl UnsafeUnpin for UserContext
impl UnwindSafe for UserContext
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
Mutably borrows from an owned value. Read more
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,
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> ⓘ
Converts
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> ⓘ
Converts
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> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.