pub struct CustomCondition {
pub name_str: &'static str,
pub generator: ConditionGenerator,
}Expand description
自定义条件规则 — 通过闭包动态生成 WHERE 子句
适用于业务特定的权限逻辑(如“只能查看自己创建的草稿状态订单“)。
§安全警告
闭包返回的 String 会直接拼接到 SQL 中,调用方必须确保内容来源可信,
严禁将用户输入直接拼接到返回的字符串中(否则会引入 SQL 注入风险)。
若需要使用用户输入,应使用参数化查询(? 占位符 + bind 参数)。
§示例
use sz_orm_core::data_permission::{CustomCondition, PermissionRule, PermissionContext};
let rule = CustomCondition::new("status_filter", |ctx| {
if ctx.is_admin() {
None
} else {
Some("status != 'draft'".to_string())
}
});
let ctx = PermissionContext::new().with_user_id(1);
let clause = rule.apply(&ctx).unwrap().unwrap();
assert_eq!(clause, "status != 'draft'");Fields§
§name_str: &'static str规则名称
generator: ConditionGenerator条件生成闭包
Implementations§
Trait Implementations§
Source§impl PermissionRule for CustomCondition
impl PermissionRule for CustomCondition
Auto Trait Implementations§
impl !RefUnwindSafe for CustomCondition
impl !UnwindSafe for CustomCondition
impl Freeze for CustomCondition
impl Send for CustomCondition
impl Sync for CustomCondition
impl Unpin for CustomCondition
impl UnsafeUnpin for CustomCondition
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