Skip to content

Commit

Permalink
feat: add key_get series built-in operator (#338)
Browse files Browse the repository at this point in the history
* feat: add key_get series built-in operator

* fix: pass test

* fix: pass test

* fix: cargo fmt
  • Loading branch information
MuZhou233 authored Sep 2, 2024
1 parent f4c3350 commit b5d3765
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 45 deletions.
10 changes: 3 additions & 7 deletions src/cached_enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
emitter::{clear_cache, Event, EventData, EventEmitter},
enforcer::EnforceContext,
enforcer::Enforcer,
model::Model,
model::{Model, OperatorFunction},
rbac::RoleManager,
Result,
};
Expand All @@ -27,7 +27,7 @@ use crate::{error::ModelError, get_or_err};

use async_trait::async_trait;
use parking_lot::RwLock;
use rhai::{Dynamic, ImmutableString};
use rhai::Dynamic;

use std::{collections::HashMap, sync::Arc};

Expand Down Expand Up @@ -123,11 +123,7 @@ impl CoreApi for CachedEnforcer {
}

#[inline]
fn add_function(
&mut self,
fname: &str,
f: fn(ImmutableString, ImmutableString) -> bool,
) {
fn add_function(&mut self, fname: &str, f: OperatorFunction) {
self.enforcer.add_function(fname, f);
}

Expand Down
13 changes: 4 additions & 9 deletions src/core_api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
enforcer::EnforceContext, Adapter, Effector, EnforceArgs, Event,
EventEmitter, Filter, Model, Result, RoleManager, TryIntoAdapter,
TryIntoModel,
enforcer::EnforceContext, model::OperatorFunction, Adapter, Effector,
EnforceArgs, Event, EventEmitter, Filter, Model, Result, RoleManager,
TryIntoAdapter, TryIntoModel,
};

#[cfg(feature = "watcher")]
Expand All @@ -15,7 +15,6 @@ use crate::emitter::EventData;

use async_trait::async_trait;
use parking_lot::RwLock;
use rhai::ImmutableString;

use std::sync::Arc;

Expand All @@ -33,11 +32,7 @@ pub trait CoreApi: Send + Sync {
) -> Result<Self>
where
Self: Sized;
fn add_function(
&mut self,
fname: &str,
f: fn(ImmutableString, ImmutableString) -> bool,
);
fn add_function(&mut self, fname: &str, f: OperatorFunction);
fn get_model(&self) -> &dyn Model;
fn get_mut_model(&mut self) -> &mut dyn Model;
fn get_adapter(&self) -> &dyn Adapter;
Expand Down
35 changes: 26 additions & 9 deletions src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
error::{ModelError, PolicyError, RequestError},
get_or_err, get_or_err_with_context,
management_api::MgmtApi,
model::{FunctionMap, Model},
model::{FunctionMap, Model, OperatorFunction},
rbac::{DefaultRoleManager, RoleManager},
register_g_function,
util::{escape_assertion, escape_eval},
Expand Down Expand Up @@ -351,6 +351,23 @@ impl Enforcer {
}))
}

fn register_function(engine: &mut Engine, key: &str, f: OperatorFunction) {
match f {
OperatorFunction::Arg0(func) => {
engine.register_fn(key, func);
}
OperatorFunction::Arg1(func) => {
engine.register_fn(key, func);
}
OperatorFunction::Arg2(func) => {
engine.register_fn(key, func);
}
OperatorFunction::Arg3(func) => {
engine.register_fn(key, func);
}
}
}

pub(crate) fn register_g_functions(&mut self) -> Result<()> {
if let Some(ast_map) = self.model.get_model().get("g") {
for (fname, ast) in ast_map {
Expand Down Expand Up @@ -380,7 +397,7 @@ impl CoreApi for Enforcer {
engine.register_global_module(CASBIN_PACKAGE.as_shared_module());

for (key, &func) in fm.get_functions() {
engine.register_fn(key, func);
Self::register_function(&mut engine, key, func);
}

let mut e = Self {
Expand Down Expand Up @@ -425,13 +442,9 @@ impl CoreApi for Enforcer {
}

#[inline]
fn add_function(
&mut self,
fname: &str,
f: fn(ImmutableString, ImmutableString) -> bool,
) {
fn add_function(&mut self, fname: &str, f: OperatorFunction) {
self.fm.add_function(fname, f);
self.engine.register_fn(fname, f);
Self::register_function(&mut self.engine, fname, f);
}

#[inline]
Expand Down Expand Up @@ -1340,7 +1353,11 @@ mod tests {

e.add_function(
"keyMatchCustom",
|s1: ImmutableString, s2: ImmutableString| key_match(&s1, &s2),
OperatorFunction::Arg2(
|s1: ImmutableString, s2: ImmutableString| {
key_match(&s1, &s2).into()
},
),
);

assert_eq!(
Expand Down
Loading

0 comments on commit b5d3765

Please sign in to comment.