casbin / casbin-rs

An authorization library that supports access control models like ACL, RBAC, ABAC in Rust.
https://casbin.org
Apache License 2.0
861 stars 74 forks source link

Question: how to preload policy from file, then use db-backed adapter? #280

Closed bk138 closed 2 years ago

bk138 commented 2 years ago

My use case is that I'd like to define some access rules for roles beforehand but later on my app assigns users to roles, this should be kept in the database. What I'm doing right now is and what works:

What I'd somehow like to do is:

I know there's casbin::CoreApi::set_adapter, but this seems to discard any policies loaded before.

Any hints on this highly appreciated!

casbin-bot commented 2 years ago

@smrpn @hackerchai @PsiACE @GopherJ

hsluoyz commented 2 years ago

@bk138 see: https://casbin.org/docs/en/adapters#migrateconvert-between-different-adapter

bk138 commented 2 years ago

@bk138 see: https://casbin.org/docs/en/adapters#migrateconvert-between-different-adapter

@hsluoyz This does not work for the use case outlined above as it discards the policy loaded from adapter A completely. Here's some of my code:

let model = DefaultModel::from_file("casbin_model.conf").await?;
// CSV file has initial policy, e.g. rules for groups
let adapter = FileAdapter::new("casbin_policy.csv");

// enforcer uses initial policy from CSV file
let mut enforcer = Enforcer::new(model, adapter).await?;

// need to later assign user to groups etc, using db-backed adapter for this
let adapter = SqlxAdapter::new(std::env::var("DATABASE_URL")?, 10).await?;

// as per https://casbin.org/docs/en/adapters#migrateconvert-between-different-adapter,
// but policy from CSV file completely discarded
enforcer.set_adapter(adapter).await?;
enforcer.load_policy().await?;

Should I file an issue with sqlx-adapter?

hsluoyz commented 2 years ago

@hackerchai @PsiACE

bk138 commented 2 years ago

Found https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 - is this the way to achieve a policy preload?

hackerchai commented 2 years ago

Found https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 - is this the way to achieve a policy preload?

yes, you can use this way in prodcution

bk138 commented 2 years ago

@hackerchai What about adding this to the README?

hsluoyz commented 2 years ago

@bk138 sounds good. Can you make a PR?

bk138 commented 2 years ago

@bk138 sounds good. Can you make a PR?

I would, but I have some difficulties comprehending the logic in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 - @hackerchai can you maybe elaborate on the logic? In particular, why is the enforcer cloned in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L69 and the policies added to the cloned one instead of "main" enforcer?

hackerchai commented 2 years ago

@bk138 sounds good. Can you make a PR?

I would, but I have some difficulties comprehending the logic in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 - @hackerchai can you maybe elaborate on the logic? In particular, why is the enforcer cloned in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L69 and the policies added to the cloned one instead of "main" enforcer? @bk138 https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L69 Why we use clone_enforcer? Because we have casbin_actor as well as casbin_middleware, and they both use casbin enforcer. In order to make these two component use the sane enforcer, we clone the enforcer first.

hackerchai commented 2 years ago

@bk138 sounds good. Can you make a PR?

I would, but I have some difficulties comprehending the logic in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 - @hackerchai can you maybe elaborate on the logic? In particular, why is the enforcer cloned in https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L69 and the policies added to the cloned one instead of "main" enforcer?

@bk138 https://github.com/casbin-rs/examples/blob/master/actix-middleware-example/src/main.rs#L73 Here is the logic to import preset casbin rules into the database via casbin_adapter. The rules in csv file explains g2 group declaration, roles and role-group combination, default admin rules.

bk138 commented 2 years ago

@bk138 sounds good. Can you make a PR?

Just did so, see #284