fabaccess-bffh/src/access.rs

47 lines
1.0 KiB
Rust
Raw Normal View History

2020-02-14 12:20:17 +01:00
//! Access control logic
//!
use casbin::prelude::*;
use super::config::Config;
2020-02-17 14:56:43 +01:00
use futures_signals::signal::Mutable;
2020-02-17 03:44:02 +01:00
use crate::api::api;
2020-02-17 14:56:43 +01:00
use crate::auth::Authentication;
use crate::error::Result;
2020-02-17 03:44:02 +01:00
#[derive(Clone)]
2020-02-17 14:56:43 +01:00
pub struct Permissions {
pdb: Mutable<Enforcer>,
auth: Authentication,
}
impl Permissions {
pub fn new(pdb: Mutable<Enforcer>, auth: Authentication) -> Self {
Self { pdb, auth }
}
pub fn enforce(&self, object: &str, action: &str) -> bool {
if let Some(actor) = self.auth.get_authzid() {
self.pdb.lock_ref().enforce(vec![&actor,object,action]).unwrap()
} else {
false
}
}
}
2020-02-17 03:44:02 +01:00
impl api::permissions::Server for Permissions {
}
/// This line documents init
2020-02-17 14:56:43 +01:00
pub async fn init(config: &Config) -> std::result::Result<Enforcer, Box<dyn std::error::Error>> {
2020-02-14 12:20:17 +01:00
let model = Model::from_file(config.access.model.clone()).await?;
let adapter = Box::new(FileAdapter::new(config.access.policy.clone()));
let e = Enforcer::new(model, adapter).await?;
return Ok(e);
}