Skip to content

Commit

Permalink
feat: add StringAdapter to file adapter module
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbei101 committed Oct 5, 2024
1 parent c5a05d3 commit ef958db
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
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 {
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"))]
pub use adapter::{FileAdapter, StringAdapter};

#[cfg(feature = "cached")]
pub use cache::{Cache, DefaultCache};
Expand Down

0 comments on commit ef958db

Please sign in to comment.