Skip to main content

DataPermissionInterceptor

Struct DataPermissionInterceptor 

Source
pub struct DataPermissionInterceptor { /* private fields */ }
Expand description

数据权限拦截器 — 注册多个规则并应用到 SQL

§工作流程

  1. 调用方注册多个 PermissionRule
  2. 在执行 SQL 前,调用 apply_to_select / apply_to_update / apply_to_delete
  3. 拦截器按注册顺序依次调用每个 PermissionRule.apply()
  4. 将所有非 None 的子句用 AND 连接,追加到原 SQL 的 WHERE 子句

§示例

use sz_orm_query::data_permission::{
    DataPermissionInterceptor, PermissionContext, TenantIsolation, OwnerOnly,
};

let mut interceptor = DataPermissionInterceptor::new();
interceptor.register(Box::new(TenantIsolation::default_field()));
interceptor.register(Box::new(OwnerOnly::default_field()));

let ctx = PermissionContext::new().with_user_id(100).with_tenant_id(5);
let sql = interceptor.apply_to_select("SELECT * FROM orders", &ctx).unwrap();
assert!(sql.contains("WHERE"));
assert!(sql.contains("tenant_id = 5"));
assert!(sql.contains("user_id = 100"));

Implementations§

Source§

impl DataPermissionInterceptor

Source

pub fn new() -> DataPermissionInterceptor

创建空拦截器

Source

pub fn register(&mut self, rule: Box<dyn PermissionRule>)

注册规则

Source

pub fn count(&self) -> usize

已注册规则数量

Source

pub fn names(&self) -> Vec<&'static str>

列出所有规则名称

Source

pub fn collect_clauses( &self, ctx: &PermissionContext, ) -> Result<Vec<String>, PermissionError>

收集所有规则的 WHERE 子句(按注册顺序,跳过 None)

Source

pub fn apply_to_select( &self, sql: &str, ctx: &PermissionContext, ) -> Result<String, PermissionError>

将规则应用到 SELECT 语句

  • 原 SQL 无 WHERE 子句:追加 WHERE clause1 AND clause2 ...
  • 原 SQL 有 WHERE 子句:在 WHERE 后追加 (原条件) AND (clause1 AND clause2 ...)
Source

pub fn apply_to_update( &self, sql: &str, ctx: &PermissionContext, ) -> Result<String, PermissionError>

将规则应用到 UPDATE 语句

Source

pub fn apply_to_delete( &self, sql: &str, ctx: &PermissionContext, ) -> Result<String, PermissionError>

将规则应用到 DELETE 语句

Trait Implementations§

Source§

impl Default for DataPermissionInterceptor

Source§

fn default() -> DataPermissionInterceptor

Returns the “default value” for a type. 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<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, 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<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