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 StringAdapter to file adapter module #343

Closed
wants to merge 1 commit into from
Closed
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
157 changes: 157 additions & 0 deletions src/adapter/file_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,160 @@ fn load_filtered_policy_line(
false
}
}

pub struct StringAdapter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New file: string_adapter.rs

policy_string: String,
is_filtered: bool,
}

impl StringAdapter {
pub fn new(policy_string: String) -> Self {
StringAdapter {
policy_string,
is_filtered: false,
}
}

pub fn new_filtered_adapter(policy_string: String) -> Self {
StringAdapter {
policy_string,
is_filtered: true,
}
}

fn load_policy_from_string(
&mut self,
m: &mut dyn Model,
handler: LoadPolicyFileHandler,
) -> Result<()> {
let lines = self.policy_string.lines();
for line in lines {
handler(line.to_string(), m);
}
Ok(())
}

fn load_filtered_policy_from_string<'a>(
&mut self,
m: &mut dyn Model,
filter: Filter<'a>,
handler: LoadFilteredPolicyFileHandler<'a>,
) -> Result<bool> {
let mut is_filtered = false;
let lines = self.policy_string.lines();
for line in lines {
if handler(line.to_string(), m, &filter) {
is_filtered = true;
}
}
Ok(is_filtered)
}

fn save_policy_to_string(&mut self, policies: String) -> Result<()> {
self.policy_string = policies;
Ok(())
}
}

#[async_trait]
impl Adapter for StringAdapter {
async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> {
self.is_filtered = false;
self.load_policy_from_string(m, load_policy_line)?;
Ok(())
}

async fn load_filtered_policy<'a>(
&mut self,
m: &mut dyn Model,
f: Filter<'a>,
) -> Result<()> {
self.is_filtered = self.load_filtered_policy_from_string(
m,
f,
load_filtered_policy_line,
)?;
Ok(())
}

async fn save_policy(&mut self, m: &mut dyn Model) -> Result<()> {
let mut policies = String::new();
let ast_map = m.get_model().get("p").ok_or_else(|| {
ModelError::P("Missing policy definition in conf file".to_owned())
})?;

for (ptype, ast) in ast_map {
for rule in ast.get_policy() {
writeln!(policies, "{},{}", ptype, rule.join(","))
.map_err(|e| AdapterError(e.into()))?;
}
}

if let Some(ast_map) = m.get_model().get("g") {
for (ptype, ast) in ast_map {
for rule in ast.get_policy() {
writeln!(policies, "{},{}", ptype, rule.join(","))
.map_err(|e| AdapterError(e.into()))?;
}
}
}

self.save_policy_to_string(policies)?;
Ok(())
}

async fn clear_policy(&mut self) -> Result<()> {
self.save_policy_to_string(String::new())?;
Ok(())
}

async fn add_policy(
&mut self,
_sec: &str,
_ptype: &str,
_rule: Vec<String>,
) -> Result<bool> {
Ok(true)
}

async fn add_policies(
&mut self,
_sec: &str,
_ptype: &str,
_rules: Vec<Vec<String>>,
) -> Result<bool> {
Ok(true)
}

async fn remove_policy(
&mut self,
_sec: &str,
_ptype: &str,
_rule: Vec<String>,
) -> Result<bool> {
Ok(true)
}

async fn remove_policies(
&mut self,
_sec: &str,
_ptype: &str,
_rules: Vec<Vec<String>>,
) -> Result<bool> {
Ok(true)
}

async fn remove_filtered_policy(
&mut self,
_sec: &str,
_ptype: &str,
_field_index: usize,
_field_values: Vec<String>,
) -> Result<bool> {
Ok(true)
}

fn is_filtered(&self) -> bool {
self.is_filtered
}
}
2 changes: 1 addition & 1 deletion src/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod memory_adapter;
pub mod null_adapter;

#[cfg(not(target_arch = "wasm32"))]
pub use file_adapter::FileAdapter;
pub use file_adapter::{FileAdapter, StringAdapter};
pub use memory_adapter::MemoryAdapter;
pub use null_adapter::NullAdapter;

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub mod error;
pub mod frontend;
pub mod prelude;

#[cfg(not(target_arch = "wasm32"))]
pub use adapter::FileAdapter;
pub use adapter::{Adapter, Filter, MemoryAdapter, NullAdapter};
#[cfg(not(target_arch = "wasm32"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this? and location?

pub use adapter::{FileAdapter, StringAdapter};

#[cfg(feature = "cached")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is missing

pub use cache::{Cache, DefaultCache};
Expand Down
Loading