-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* finish effector stream * simplify code * bump version * add assert to make sure that when we call current it's already finished * Fix: clippy warnings
- Loading branch information
Showing
5 changed files
with
103 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "casbin" | ||
version = "0.7.6" | ||
version = "0.8.1" | ||
authors = ["Joey <[email protected]>", "Cheng JIANG <[email protected]>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,88 @@ | ||
pub trait Effector: Send + Sync { | ||
fn merge_effects(&self, expr: &str, effects: Vec<EffectKind>) -> bool; | ||
} | ||
|
||
#[derive(PartialEq, Clone)] | ||
#[derive(PartialEq, Clone, Copy)] | ||
pub enum EffectKind { | ||
Allow = 0, | ||
Indeterminate = 1, | ||
Deny = 2, | ||
} | ||
|
||
pub trait Effector: Send + Sync { | ||
fn new_stream(&self, expr: &str, cap: usize) -> Box<dyn EffectorStream>; | ||
} | ||
|
||
pub trait EffectorStream: Send + Sync { | ||
fn current(&self) -> bool; | ||
fn push_effect(&mut self, eft: EffectKind) -> (bool, bool); | ||
} | ||
|
||
pub struct DefaultEffectStream { | ||
done: bool, | ||
res: bool, | ||
expr: String, | ||
effects: Vec<EffectKind>, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct DefaultEffector; | ||
|
||
impl Effector for DefaultEffector { | ||
fn merge_effects(&self, expr: &str, effects: Vec<EffectKind>) -> bool { | ||
if expr == "some(where (p_eft == allow))" { | ||
let mut result = false; | ||
for eft in effects { | ||
if eft == EffectKind::Allow { | ||
result = true; | ||
break; | ||
} | ||
} | ||
fn new_stream(&self, expr: &str, cap: usize) -> Box<dyn EffectorStream> { | ||
let res = match &*expr { | ||
"some(where (p_eft == allow))" | ||
| "some(where (p_eft == allow)) && !some(where (p_eft == deny))" | ||
| "priority(p_eft) || deny" => false, | ||
"!some(where (p_eft == deny))" => true, | ||
_ => panic!("unsupported effect: `{}`", expr), | ||
}; | ||
|
||
result | ||
} else if expr == "!some(where (p_eft == deny))" { | ||
let mut result = true; | ||
for eft in effects { | ||
if eft == EffectKind::Deny { | ||
result = false; | ||
break; | ||
} | ||
} | ||
Box::new(DefaultEffectStream { | ||
done: false, | ||
res, | ||
expr: expr.to_owned(), | ||
effects: Vec::with_capacity(cap), | ||
}) | ||
} | ||
} | ||
|
||
result | ||
} else if expr == "some(where (p_eft == allow)) && !some(where (p_eft == deny))" { | ||
let mut result = false; | ||
for eft in effects { | ||
if eft == EffectKind::Allow { | ||
result = true; | ||
} else if eft == EffectKind::Deny { | ||
result = false; | ||
break; | ||
} | ||
} | ||
impl EffectorStream for DefaultEffectStream { | ||
fn current(&self) -> bool { | ||
assert!(self.done); | ||
self.res | ||
} | ||
|
||
fn push_effect(&mut self, eft: EffectKind) -> (bool, bool) { | ||
let cap = self.effects.capacity(); | ||
self.effects.push(eft); | ||
|
||
result | ||
} else if expr == "priority(p_eft) || deny" { | ||
let mut result = false; | ||
for eft in effects { | ||
if eft != EffectKind::Indeterminate { | ||
if eft == EffectKind::Allow { | ||
result = true | ||
} else { | ||
result = false | ||
} | ||
break; | ||
} | ||
if self.expr == "some(where (p_eft == allow))" { | ||
if eft == EffectKind::Allow { | ||
self.done = true; | ||
self.res = true; | ||
} | ||
} else if self.expr == "some(where (p_eft == allow)) && !some(where (p_eft == deny))" { | ||
if eft == EffectKind::Allow { | ||
self.res = true; | ||
} else if eft == EffectKind::Deny { | ||
self.done = true; | ||
self.res = false; | ||
} | ||
} else if self.expr == "!some(where (p_eft == deny))" { | ||
if eft == EffectKind::Deny { | ||
self.done = true; | ||
self.res = false; | ||
} | ||
} else if self.expr == "priority(p_eft) || deny" && eft != EffectKind::Indeterminate { | ||
if eft == EffectKind::Allow { | ||
self.res = true; | ||
} else { | ||
self.res = false; | ||
} | ||
self.done = true; | ||
} | ||
|
||
result | ||
} else { | ||
panic!("unsupported effect: `{}`", expr); | ||
if cap == self.effects.len() { | ||
self.done = true; | ||
} | ||
|
||
(self.done, self.res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e99cfa3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust Benchmark
b_benchmark_abac_model
7430
ns/iter (± 505
)53309
ns/iter (± 22316
)0.14
b_benchmark_basic_model
7599
ns/iter (± 494
)78163
ns/iter (± 8585
)0.09721991223468905
b_benchmark_key_match
25752
ns/iter (± 1693
)120899
ns/iter (± 9597
)0.21
b_benchmark_priority_model
9230
ns/iter (± 865
)89611
ns/iter (± 11510
)0.10
b_benchmark_raw
8
ns/iter (± 0
)8
ns/iter (± 0
)1
b_benchmark_rbac_model
22119
ns/iter (± 2127
)71003
ns/iter (± 5172
)0.31
b_benchmark_rbac_model_large
66038660
ns/iter (± 3265971
)66814948
ns/iter (± 1921795
)0.99
b_benchmark_rbac_model_medium
6290208
ns/iter (± 822761
)6420130
ns/iter (± 360340
)0.98
b_benchmark_rbac_model_small
634671
ns/iter (± 16311
)683168
ns/iter (± 62901
)0.93
b_benchmark_rbac_model_with_domains
12935
ns/iter (± 1399
)85420
ns/iter (± 17004
)0.15
b_benchmark_rbac_with_deny
37612
ns/iter (± 3101
)79068
ns/iter (± 9563
)0.48
b_benchmark_rbac_with_resource_roles
10113
ns/iter (± 556
)67024
ns/iter (± 14352
)0.15
This comment was automatically generated by workflow using github-action-benchmark.