pub struct TenantContext {
pub tenant_id: i64,
pub isolation_strategy: IsolationStrategy,
pub permissions: TenantPermissions,
}Expand description
租户上下文(运行时自动注入)
由可信路径(中间件/网关)设置,不可被客户端篡改。
tenant_id 必填 i64(禁止字符串避免注入)。
Fields§
§tenant_id: i64租户 ID(必填,与既有 with_tenant_id(tenant_id: i64) 类型一致)
isolation_strategy: IsolationStrategy隔离策略(行级 / Schema 隔离)
permissions: TenantPermissions权限(行级安全策略 + 列级脱敏规则 + 角色列表)
Implementations§
Source§impl TenantContext
impl TenantContext
Sourcepub fn new(tenant_id: i64, isolation_strategy: IsolationStrategy) -> Self
pub fn new(tenant_id: i64, isolation_strategy: IsolationStrategy) -> Self
创建新的租户上下文
Sourcepub fn with_permissions(self, permissions: TenantPermissions) -> Self
pub fn with_permissions(self, permissions: TenantPermissions) -> Self
设置权限
Sourcepub fn enter(self) -> TenantContextGuard
pub fn enter(self) -> TenantContextGuard
进入上下文作用域,返回 RAII 守卫(线程局部存储)
守卫在 Drop 时自动清理线程局部上下文。
注意:此方法使用 thread_local 存储,适用于同步上下文或
单线程 tokio 运行时。对于多线程 tokio 运行时中的异步任务,
请使用 TenantContext::scope 代替。
Sourcepub async fn scope<F, R>(self, f: F) -> Rwhere
F: Future<Output = R>,
pub async fn scope<F, R>(self, f: F) -> Rwhere
F: Future<Output = R>,
在指定异步块的作用域内设置上下文(task-local 存储)
这是异步安全的上下文设置方式,使用 tokio::task_local! 实现
异步任务边界隔离。在 scope 内部,TenantContext::current
返回 Some;scope 结束后返回 None。
§示例
ⓘ
let ctx = TenantContext::new(42, IsolationStrategy::RowLevel);
ctx.scope(async {
// 在此作用域内,TenantContext::current() 返回 Some
assert_eq!(TenantContext::current().unwrap().tenant_id, 42);
}).await;Sourcepub fn current() -> Option<TenantContext>
pub fn current() -> Option<TenantContext>
读取当前上下文
优先从 task-local(异步 scope)读取,其次从 thread-local(RAII guard)读取。
返回 None 表示未设置上下文。
Trait Implementations§
Source§impl Clone for TenantContext
impl Clone for TenantContext
Source§fn clone(&self) -> TenantContext
fn clone(&self) -> TenantContext
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 moreAuto Trait Implementations§
impl Freeze for TenantContext
impl RefUnwindSafe for TenantContext
impl Send for TenantContext
impl Sync for TenantContext
impl Unpin for TenantContext
impl UnsafeUnpin for TenantContext
impl UnwindSafe for TenantContext
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 more