Skip to content

Commit

Permalink
feat: fix bug in remove_filtered_policy() and add test cases (#90)
Browse files Browse the repository at this point in the history
* fix: normalize_casbin_rule_option to work with sql's coalesce

* fix: check for valid arguments in remove_filtered_policy

* test: add tests for remove_filtered_policy
  • Loading branch information
zbrox authored Feb 21, 2024
1 parent fa59fc8 commit 9550deb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,10 @@ fn normalize_casbin_rule(mut rule: Vec<String>) -> Vec<String> {
fn normalize_casbin_rule_option(rule: Vec<String>) -> Vec<Option<String>> {
let mut rule_with_option = rule
.iter()
.map(|x| Some(x.clone()))
.map(|x| match x.is_empty() {
true => None,
false => Some(x.clone()),
})
.collect::<Vec<Option<String>>>();
rule_with_option.resize(6, None);
rule_with_option
Expand Down
52 changes: 51 additions & 1 deletion src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Adapter for SqlxAdapter {
field_index: usize,
field_values: Vec<String>,
) -> Result<bool> {
if field_index <= 5 && !field_values.is_empty() && field_values.len() > field_index {
if field_index <= 5 && !field_values.is_empty() && field_values.len() + field_index <= 6 {
adapter::remove_filtered_policy(&self.pool, pt, field_index, field_values).await
} else {
Ok(false)
Expand Down Expand Up @@ -566,6 +566,56 @@ mod tests {
.unwrap());
assert_eq!(vec![String::new(); 0], e.get_roles_for_user("carol", None));

// GitHub issue: https://github.com/casbin-rs/sqlx-adapter/pull/90
// add policies:
// p, alice_rfp, book_rfp, read_rfp
// p, bob_rfp, book_rfp, read_rfp
// p, bob_rfp, book_rfp, write_rfp
// p, alice_rfp, pen_rfp, get_rfp
// p, bob_rfp, pen_rfp, get_rfp
// p, alice_rfp, pencil_rfp, get_rfp
assert!(adapter
.add_policy("", "p", to_owned(vec!["alice_rfp", "book_rfp", "read_rfp"]),)
.await
.is_ok());
assert!(adapter
.add_policy("", "p", to_owned(vec!["bob_rfp", "book_rfp", "read_rfp"]),)
.await
.is_ok());
assert!(adapter
.add_policy("", "p", to_owned(vec!["bob_rfp", "book_rfp", "write_rfp"]),)
.await
.is_ok());
assert!(adapter
.add_policy("", "p", to_owned(vec!["alice_rfp", "pen_rfp", "get_rfp"]),)
.await
.is_ok());
assert!(adapter
.add_policy("", "p", to_owned(vec!["bob_rfp", "pen_rfp", "get_rfp"]),)
.await
.is_ok());
assert!(adapter
.add_policy(
"",
"p",
to_owned(vec!["alice_rfp", "pencil_rfp", "get_rfp"]),
)
.await
.is_ok());

// should remove (return true) all policies where "book_rfp" is in the second position
assert!(adapter
.remove_filtered_policy("", "p", 1, to_owned(vec!["book_rfp"]),)
.await
.unwrap());

// should remove (return true) all policies which match "alice_rfp" on first position
// and "get_rfp" on third position
assert!(adapter
.remove_filtered_policy("", "p", 0, to_owned(vec!["alice_rfp", "", "get_rfp"]),)
.await
.unwrap());

// shadow the previous enforcer
let mut e = Enforcer::new(
"examples/rbac_with_domains_model.conf",
Expand Down

0 comments on commit 9550deb

Please sign in to comment.