diff --git a/src/actions.rs b/src/actions.rs index 3bfb8c6..2fd448b 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1075,7 +1075,10 @@ fn normalize_casbin_rule(mut rule: Vec) -> Vec { fn normalize_casbin_rule_option(rule: Vec) -> Vec> { 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::>>(); rule_with_option.resize(6, None); rule_with_option diff --git a/src/adapter.rs b/src/adapter.rs index 81587d0..9287f93 100644 --- a/src/adapter.rs +++ b/src/adapter.rs @@ -251,7 +251,7 @@ impl Adapter for SqlxAdapter { field_index: usize, field_values: Vec, ) -> Result { - 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) @@ -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",