diff --git a/Cargo.toml b/Cargo.toml index 5661be6..9736f4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-casbin-auth" -version = "0.4.0" +version = "0.4.1" authors = ["Eason Chai ","Cheng JIANG "] edition = "2018" license = "Apache-2.0" diff --git a/README.md b/README.md index 87b3f0b..c14b1e8 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Add it to `Cargo.toml` ```rust -actix-casbin-auth = "0.4.0" +actix-casbin-auth = "0.4.1" actix-rt = "1.1.1" actix-web = "2.0.0" ``` @@ -107,11 +107,21 @@ mod fake_auth; #[actix_rt::main] async fn main() -> Result<()> { - let m = DefaultModel::from_file("examples/rbac_restful_keymatch2_model.conf").await?; - let a = FileAdapter::new("examples/rbac_restful_keymatch2_policy.csv"); //You can also use diesel-adapter or sqlx-adapter + let m = DefaultModel::from_file("examples/rbac_with_pattern_model.conf") + .await + .unwrap(); + let a = FileAdapter::new("examples/rbac_with_pattern_policy.csv"); //You can also use diesel-adapter or sqlx-adapter let casbin_middleware = CasbinService::new(m, a).await; + casbin_middleware + .write() + .await + .get_role_manager() + .write() + .unwrap() + .matching_fn(Some(key_match2), None); + HttpServer::new(move || { App::new() .wrap(casbin_middleware.clone()) diff --git a/examples/rbac_restful_keymatch2_model.conf b/examples/rbac_restful_keymatch2_model.conf deleted file mode 100644 index aaed506..0000000 --- a/examples/rbac_restful_keymatch2_model.conf +++ /dev/null @@ -1,14 +0,0 @@ -[request_definition] -r = sub, obj, act - -[request_definition] -r = sub, obj, act - -[policy_definition] -p = sub, obj, act - -[policy_effect] -e = some(where (p.eft == allow)) - -[matchers] -m = r.sub == p.sub && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act) diff --git a/examples/rbac_restful_keymatch2_policy.csv b/examples/rbac_restful_keymatch2_policy.csv deleted file mode 100644 index 6ac24a5..0000000 --- a/examples/rbac_restful_keymatch2_policy.csv +++ /dev/null @@ -1,2 +0,0 @@ -p, alice, /pen/:id, GET -p, alice, /book/:id, GET diff --git a/examples/rbac_with_domains_model.conf b/examples/rbac_with_domains_model.conf index 46585e1..26b95a7 100644 --- a/examples/rbac_with_domains_model.conf +++ b/examples/rbac_with_domains_model.conf @@ -6,10 +6,9 @@ p = sub, dom, obj, act [role_definition] g = _, _, _ -g2 = _, _, _ [policy_effect] e = some(where (p.eft == allow)) [matchers] -m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act \ No newline at end of file +m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && regexMatch(r.act, p.act) \ No newline at end of file diff --git a/tests/test_middleware.rs b/tests/test_middleware.rs index bc0fcd8..2757682 100644 --- a/tests/test_middleware.rs +++ b/tests/test_middleware.rs @@ -10,7 +10,8 @@ use futures::future::{ok, Future, Ready}; use actix_casbin_auth::{CasbinService, CasbinVals}; use actix_web::{test, web, App}; -use casbin::{DefaultModel, FileAdapter}; +use casbin::function_map::key_match2; +use casbin::{CoreApi, DefaultModel, FileAdapter}; pub struct FakeAuth; @@ -69,13 +70,21 @@ where #[actix_rt::test] async fn test_middleware() { - let m = DefaultModel::from_file("examples/rbac_restful_keymatch2_model.conf") + let m = DefaultModel::from_file("examples/rbac_with_pattern_model.conf") .await .unwrap(); - let a = FileAdapter::new("examples/rbac_restful_keymatch2_policy.csv"); + let a = FileAdapter::new("examples/rbac_with_pattern_policy.csv"); let casbin_middleware = CasbinService::new(m, a).await; + casbin_middleware + .write() + .await + .get_role_manager() + .write() + .unwrap() + .matching_fn(Some(key_match2), None); + let mut app = test::init_service( App::new() .wrap(casbin_middleware.clone()) diff --git a/tests/test_set_enforcer.rs b/tests/test_set_enforcer.rs index a8b07d4..7ce9604 100644 --- a/tests/test_set_enforcer.rs +++ b/tests/test_set_enforcer.rs @@ -10,6 +10,7 @@ use futures::future::{ok, Future, Ready}; use actix_casbin_auth::{CasbinService, CasbinVals}; use actix_web::{test, web, App}; +use casbin::function_map::key_match2; use casbin::{CachedEnforcer, CoreApi, DefaultModel, FileAdapter}; use std::sync::Arc; @@ -77,18 +78,26 @@ where #[actix_rt::test] async fn test_set_enforcer() { - let m = DefaultModel::from_file("examples/rbac_restful_keymatch2_model.conf") + let m = DefaultModel::from_file("examples/rbac_with_pattern_model.conf") .await .unwrap(); - let a = FileAdapter::new("examples/rbac_restful_keymatch2_policy.csv"); + let a = FileAdapter::new("examples/rbac_with_pattern_policy.csv"); let enforcer = Arc::new(RwLock::new(CachedEnforcer::new(m, a).await.unwrap())); let casbin_middleware = CasbinService::set_enforcer(enforcer); + casbin_middleware + .write() + .await + .get_role_manager() + .write() + .unwrap() + .matching_fn(Some(key_match2), None); + let mut app = test::init_service( App::new() - .wrap(casbin_middleware) + .wrap(casbin_middleware.clone()) .wrap(FakeAuth) .route("/pen/1", web::get().to(|| HttpResponse::Ok())) .route("/book/{id}", web::get().to(|| HttpResponse::Ok())),