Skip to content

Commit

Permalink
Merge pull request #14 from hackerchai/master
Browse files Browse the repository at this point in the history
Feat: bump version 0.3.2
  • Loading branch information
hackerchai authored Aug 30, 2020
2 parents 31e6808 + 07c741a commit cc8677f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
.env
.idea/
Cargo.lock
Cargo.lock
.vscode/
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-casbin-auth"
version = "0.3.1"
version = "0.3.2"
authors = ["Eason Chai <[email protected]>","Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -13,12 +13,12 @@ name = "actix_casbin_auth"
path = "src/lib.rs"

[dependencies]
casbin = { version = "1.1.2", default-features = false, features = ["incremental", "cached"] }
tokio = { version = "0.2.21", default-features = false, optional = true }
async-std = { version = "1.6.2", default-features = false, optional = true }
casbin = { version = "1.1.3", default-features = false, features = ["incremental", "cached"] }
tokio = { version = "0.2.22", default-features = false, optional = true }
async-std = { version = "1.6.3", default-features = false, optional = true }
actix-web = "2.0.0"
actix-service = "1.0.5"
futures = "0.3.5"
actix-service = "1.0.6"
futures = "0.3"

[features]
default = ["runtime-async-std"]
Expand All @@ -27,6 +27,6 @@ runtime-tokio = ["casbin/runtime-tokio", "tokio/sync"]
runtime-async-std = ["casbin/runtime-async-std", "async-std/std"]

[dev-dependencies]
tokio = { version = "0.2.21", features = [ "full" ] }
async-std = { version = "1.6.2", features = [ "attributes" ] }
tokio = { version = "0.2.22", features = [ "full" ] }
async-std = { version = "1.6.3", features = [ "attributes" ] }
actix-rt = "1.1.1"
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Add it to `Cargo.toml`

```rust
actix-casbin-auth = "0.3.1"
actix-casbin-auth = "0.3.2"
actix-rt = "1.1.1"
actix-web = "2.0.0"
```
Expand Down Expand Up @@ -112,8 +112,6 @@ async fn main() -> Result<()> {

let casbin_middleware = CasbinService::new(m, a).await;

casbin_middleware.write().await;

HttpServer::new(move || {
App::new()
.wrap(casbin_middleware.clone())
Expand Down
16 changes: 10 additions & 6 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ impl CasbinService {
}
}

pub async fn get_enforcer(&mut self) -> Arc<RwLock<CachedEnforcer>> {
pub fn get_enforcer(&mut self) -> Arc<RwLock<CachedEnforcer>> {
self.enforcer.clone()
}

pub async fn set_enforcer(e: Arc<RwLock<CachedEnforcer>>) -> Self {
pub fn set_enforcer(e: Arc<RwLock<CachedEnforcer>>) -> CasbinService {
CasbinService { enforcer: e }
}
}
Expand Down Expand Up @@ -126,27 +126,31 @@ where
let lock = cloned_enforcer.write().await;
match lock.enforce(&[subject, domain, &path, &action]) {
Ok(true) => {
let fut_res = srv.call(req);
fut_res.await
drop(lock);
srv.call(req).await
}
Ok(false) => {
drop(lock);
Ok(req.into_response(HttpResponse::Forbidden().finish().into_body()))
}
Err(_) => {
drop(lock);
Ok(req.into_response(HttpResponse::BadGateway().finish().into_body()))
}
}
} else {
let lock = cloned_enforcer.write().await;
match lock.enforce(&[subject, &path, &action]) {
Ok(true) => {
let fut_res = srv.call(req);
fut_res.await
drop(lock);
srv.call(req).await
}
Ok(false) => {
drop(lock);
Ok(req.into_response(HttpResponse::Forbidden().finish().into_body()))
}
Err(_) => {
drop(lock);
Ok(req.into_response(HttpResponse::BadGateway().finish().into_body()))
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/test_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ async fn test_middleware() {

let casbin_middleware = CasbinService::new(m, a).await;

casbin_middleware.write().await;

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())),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_middleware_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn test_middleware() {

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/1", web::get().to(|| HttpResponse::Ok())),
Expand Down
6 changes: 2 additions & 4 deletions tests/test_set_enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,15 @@ where
}

#[actix_rt::test]
async fn test_middleware() {
async fn test_set_enforcer() {
let m = DefaultModel::from_file("examples/rbac_restful_keymatch2_model.conf")
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_restful_keymatch2_policy.csv");

let enforcer = Arc::new(RwLock::new(CachedEnforcer::new(m, a).await.unwrap()));

let casbin_middleware = CasbinService::set_enforcer(enforcer).await;

casbin_middleware.write().await;
let casbin_middleware = CasbinService::set_enforcer(enforcer);

let mut app = test::init_service(
App::new()
Expand Down

0 comments on commit cc8677f

Please sign in to comment.