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 register(&mut self, rule: Box<dyn PermissionRule>)
pub fn register(&mut self, rule: Box<dyn PermissionRule>)
注册规则
Sourcepub fn collect_clauses(
&self,
ctx: &PermissionContext,
) -> PermissionResult<Vec<String>>
pub fn collect_clauses( &self, ctx: &PermissionContext, ) -> PermissionResult<Vec<String>>
收集所有规则的 WHERE 子句(按注册顺序,跳过 None)
Sourcepub fn apply_to_select(
&self,
sql: &str,
ctx: &PermissionContext,
) -> PermissionResult<String>
pub fn apply_to_select( &self, sql: &str, ctx: &PermissionContext, ) -> PermissionResult<String>
将规则应用到 SELECT 语句
- 原 SQL 无 WHERE 子句:追加
WHERE clause1 AND clause2 ... - 原 SQL 有 WHERE 子句:在 WHERE 后追加
(原条件) AND (clause1 AND clause2 ...)
Sourcepub fn apply_to_update(
&self,
sql: &str,
ctx: &PermissionContext,
) -> PermissionResult<String>
pub fn apply_to_update( &self, sql: &str, ctx: &PermissionContext, ) -> PermissionResult<String>
将规则应用到 UPDATE 语句
Sourcepub fn apply_to_delete(
&self,
sql: &str,
ctx: &PermissionContext,
) -> PermissionResult<String>
pub fn apply_to_delete( &self, sql: &str, ctx: &PermissionContext, ) -> PermissionResult<String>
将规则应用到 DELETE 语句
Trait Implementations§
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