Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

#[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<()> {

Check warning on line 13 in src/adapter/null_adapter.rs

View check run for this annotation

Codecov / codecov/patch

src/adapter/null_adapter.rs#L13

Added line #L13 was not covered by tests
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
Loading