Skip to content

Commit

Permalink
feat: add new_filtered_adapter() to FileAdapter to make it act as a…
Browse files Browse the repository at this point in the history
… FilterAdapter. (#319)

* feat: add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter.

fix: #304

add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter.

And does not automatically load the policy when using the filter adapter.

* feat: add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter.

fix: #304

add new_filtered_adapter() to FileAdapter to make it act as a FilterAdapter.

And does not automatically load the policy when using the filter adapter.
  • Loading branch information
HGZ-20 authored Dec 1, 2023
1 parent 49f090e commit 7d909e8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/adapter/file_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ where
}
}

pub fn new_filtered_adapter(p: P) -> FileAdapter<P> {
FileAdapter {
file_path: p,
is_filtered: true,
}
}

async fn load_policy_file(
&self,
&mut self,
m: &mut dyn Model,
handler: LoadPolicyFileHandler,
) -> Result<()> {
Expand Down Expand Up @@ -110,7 +117,8 @@ impl<P> Adapter for FileAdapter<P>
where
P: AsRef<Path> + Send + Sync,
{
async fn load_policy(&self, m: &mut dyn Model) -> Result<()> {
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> {
self.is_filtered = false;
self.load_policy_file(m, load_policy_line).await?;
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/adapter/memory_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub struct MemoryAdapter {

#[async_trait]
impl Adapter for MemoryAdapter {
async fn load_policy(&self, m: &mut dyn Model) -> Result<()> {
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> {
self.is_filtered = false;
for line in self.policy.iter() {
let sec = &line[0];
let ptype = &line[1];
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Filter<'a> {

#[async_trait]
pub trait Adapter: Send + Sync {
async fn load_policy(&self, m: &mut dyn Model) -> Result<()>;
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()>;
async fn load_filtered_policy<'a>(
&mut self,
m: &mut dyn Model,
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/null_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct NullAdapter;

#[async_trait]
impl Adapter for NullAdapter {
async fn load_policy(&self, _m: &mut dyn Model) -> Result<()> {
async fn load_policy(&mut self, _m: &mut dyn Model) -> Result<()> {
Ok(())
}

Expand Down
17 changes: 11 additions & 6 deletions src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,11 @@ impl CoreApi for Enforcer {
a: A,
) -> Result<Self> {
let mut e = Self::new_raw(m, a).await?;
e.load_policy().await?;

// Do not initialize the full policy when using a filtered adapter
if !e.adapter.is_filtered() {
e.load_policy().await?;
}
Ok(e)
}

Expand Down Expand Up @@ -1373,12 +1377,13 @@ mod tests {
tokio::test
)]
async fn test_filtered_file_adapter() {
let mut e = Enforcer::new(
"examples/rbac_with_domains_model.conf",
let adapter = FileAdapter::new_filtered_adapter(
"examples/rbac_with_domains_policy.csv",
)
.await
.unwrap();
);
let mut e =
Enforcer::new("examples/rbac_with_domains_model.conf", adapter)
.await
.unwrap();

let filter = Filter {
p: vec!["", "domain1"],
Expand Down

0 comments on commit 7d909e8

Please sign in to comment.