1
0
mirror of https://github.com/actix/examples synced 2025-06-29 02:10:36 +02:00

Restructure folders (#411)

This commit is contained in:
Daniel T. Rodrigues
2021-02-25 21:57:58 -03:00
committed by GitHub
parent 9db98162b2
commit c3407627d0
334 changed files with 127 additions and 120 deletions

View File

@ -0,0 +1,10 @@
[package]
name = "actix-casbin-example"
version = "0.1.0"
authors = ["Chojan Shang <psiace@outlook.com>"]
edition = "2018"
[dependencies]
actix-web = "3"
casbin = "2"
loge = {version = "0.4", default-features = false, features = ["colored", "chrono"]}

26
security/casbin/README.md Normal file
View File

@ -0,0 +1,26 @@
# Casbin
Basic integration of [Casbin-RS](https://github.com/casbin/casbin-rs) with [RBAC](https://en.wikipedia.org/wiki/Role-based_access_control) for Actix web.
## Usage
```sh
cd examples/casbin
```
Modify the files in the `rbac` directory and the code in the `src` directory as required.
## Running Server
```sh
cd examples/casbin
cargo run (or ``cargo watch -x run``)
# Started http server: 127.0.0.1:8080
```
In this example, you can get the the successful result at `http://localhost:8080/success` (accessible) and the failed result at `http://localhost:8080/fail` (inaccessible, `ERR_EMPTY_RESPONSE`).
## Others
- For more related examples of [Casbin-RS](https://github.com/casbin/casbin-rs): <https://github.com/casbin-rs/examples>

View File

@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

View File

@ -0,0 +1,5 @@
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
1 p, alice, data1, read
2 p, bob, data2, write
3 p, data2_admin, data2, read
4 p, data2_admin, data2, write
5 g, alice, data2_admin

View File

@ -0,0 +1,55 @@
use casbin::{CoreApi, DefaultModel, Enforcer, FileAdapter, RbacApi};
use std::io;
use std::sync::RwLock;
use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer};
/// simple handle
async fn success(
enforcer: web::Data<RwLock<Enforcer>>,
req: HttpRequest,
) -> HttpResponse {
let mut e = enforcer.write().unwrap();
println!("{:?}", req);
assert_eq!(vec!["data2_admin"], e.get_roles_for_user("alice", None));
HttpResponse::Ok().body("Success: alice is data2_admin.")
}
async fn fail(enforcer: web::Data<RwLock<Enforcer>>, req: HttpRequest) -> HttpResponse {
let mut e = enforcer.write().unwrap();
println!("{:?}", req);
assert_eq!(vec!["data1_admin"], e.get_roles_for_user("alice", None));
HttpResponse::Ok().body("Fail: alice is not data1_admin.") // In fact, it can't be displayed.
}
#[actix_web::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "info");
std::env::set_var("LOGE_FORMAT", "target");
loge::init();
let model = DefaultModel::from_file("rbac/rbac_model.conf")
.await
.unwrap();
let adapter = FileAdapter::new("rbac/rbac_policy.csv");
let e = Enforcer::new(model, adapter).await.unwrap();
let e = web::Data::new(RwLock::new(e)); // wrap enforcer into actix-state
//move is necessary to give closure below ownership of counter
HttpServer::new(move || {
App::new()
.app_data(e.clone()) // <- create app with shared state
// enable logger
.wrap(middleware::Logger::default())
// register simple handler, handle all methods
.service(web::resource("/success").to(success))
.service(web::resource("/fail").to(fail))
})
.bind("127.0.0.1:8080")?
.run()
.await
}