pub struct DataPermissionInterceptor { /* private fields */ }Expand description
数据权限拦截器 — 注册多个规则并应用到 SQL
§工作流程
- 调用方注册多个
PermissionRule - 在执行 SQL 前,调用
apply_to_select/apply_to_update/apply_to_delete - 拦截器按注册顺序依次调用每个
PermissionRule.apply() - 将所有非 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
impl DataPermissionInterceptor
Sourcepub fn new() -> DataPermissionInterceptor
pub fn new() -> DataPermissionInterceptor
创建空拦截器
Sourcepub fn register(&mut self, rule: Box<dyn PermissionRule>)
pub fn register(&mut self, rule: Box<dyn PermissionRule>)
注册规则
Sourcepub fn collect_clauses(
&self,
ctx: &PermissionContext,
) -> Result<Vec<String>, PermissionError>
pub fn collect_clauses( &self, ctx: &PermissionContext, ) -> Result<Vec<String>, PermissionError>
收集所有规则的 WHERE 子句(按注册顺序,跳过 None)
Sourcepub fn apply_to_select(
&self,
sql: &str,
ctx: &PermissionContext,
) -> Result<String, PermissionError>
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 ...)
Sourcepub fn apply_to_update(
&self,
sql: &str,
ctx: &PermissionContext,
) -> Result<String, PermissionError>
pub fn apply_to_update( &self, sql: &str, ctx: &PermissionContext, ) -> Result<String, PermissionError>
将规则应用到 UPDATE 语句
Sourcepub fn apply_to_delete(
&self,
sql: &str,
ctx: &PermissionContext,
) -> Result<String, PermissionError>
pub fn apply_to_delete( &self, sql: &str, ctx: &PermissionContext, ) -> Result<String, PermissionError>
将规则应用到 DELETE 语句
Trait Implementations§
Source§impl Default for DataPermissionInterceptor
impl Default for DataPermissionInterceptor
Source§fn default() -> DataPermissionInterceptor
fn default() -> DataPermissionInterceptor
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for DataPermissionInterceptor
impl !UnwindSafe for DataPermissionInterceptor
impl Freeze for DataPermissionInterceptor
impl Send for DataPermissionInterceptor
impl Sync for DataPermissionInterceptor
impl Unpin for DataPermissionInterceptor
impl UnsafeUnpin for DataPermissionInterceptor
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
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