Skip to main content

TenantContext

Struct TenantContext 

Source
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

Source

pub fn new(tenant_id: i64, isolation_strategy: IsolationStrategy) -> Self

创建新的租户上下文

Source

pub fn with_permissions(self, permissions: TenantPermissions) -> Self

设置权限

Source

pub fn enter(self) -> TenantContextGuard

进入上下文作用域,返回 RAII 守卫(线程局部存储)

守卫在 Drop 时自动清理线程局部上下文。 注意:此方法使用 thread_local 存储,适用于同步上下文或 单线程 tokio 运行时。对于多线程 tokio 运行时中的异步任务, 请使用 TenantContext::scope 代替。

Source

pub async fn scope<F, R>(self, f: F) -> R
where 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;
Source

pub fn current() -> Option<TenantContext>

读取当前上下文

优先从 task-local(异步 scope)读取,其次从 thread-local(RAII guard)读取。 返回 None 表示未设置上下文。

Source

pub fn is_set() -> bool

检查当前是否在上下文作用域内

Trait Implementations§

Source§

impl Clone for TenantContext

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for TenantContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more