-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,60 @@ | ||
use std::{ | ||
collections::{hash_map::Iter, HashMap}, | ||
fs, | ||
collections::HashMap, | ||
fs::{self, File}, | ||
io::Write, | ||
path::PathBuf, | ||
}; | ||
|
||
use crate::{consts::ZELLIJ_PLUGIN_PERMISSIONS_CACHE, data::PermissionType}; | ||
|
||
pub type GrantedPermission = HashMap<String, Vec<PermissionType>>; | ||
|
||
#[derive(Default, Debug)] | ||
pub struct GrantedPermission(HashMap<String, Vec<PermissionType>>); | ||
pub struct PermissionCache { | ||
path: PathBuf, | ||
granted: GrantedPermission, | ||
} | ||
|
||
impl GrantedPermission { | ||
pub fn insert(&mut self, k: String, v: Vec<PermissionType>) { | ||
self.0.insert(k, v); | ||
impl PermissionCache { | ||
pub fn cache(&mut self, plugin_name: String, permissions: Vec<PermissionType>) { | ||
self.granted.insert(plugin_name, permissions); | ||
} | ||
|
||
pub fn get(&self, k: &String) -> Option<&Vec<PermissionType>> { | ||
self.0.get(k) | ||
pub fn get_permissions(&self, plugin_name: String) -> Option<&Vec<PermissionType>> { | ||
self.granted.get(&plugin_name) | ||
} | ||
|
||
pub fn iter(&self) -> Iter<String, Vec<PermissionType>> { | ||
self.0.iter() | ||
pub fn check_permissions( | ||
&self, | ||
plugin_name: String, | ||
permissions: &Vec<PermissionType>, | ||
) -> bool { | ||
if let Some(target) = self.granted.get(&plugin_name) { | ||
if target == permissions { | ||
return true; | ||
} | ||
} | ||
|
||
false | ||
} | ||
|
||
pub fn from_cache_or_default() -> Self { | ||
let default_permission = ZELLIJ_PLUGIN_PERMISSIONS_CACHE.to_path_buf(); | ||
pub fn from_path_or_default(cache_path: Option<PathBuf>) -> Self { | ||
let cache_path = cache_path.unwrap_or(ZELLIJ_PLUGIN_PERMISSIONS_CACHE.to_path_buf()); | ||
|
||
match fs::read_to_string(&default_permission) { | ||
Ok(s) => GrantedPermission::from_string(s).unwrap_or_default(), | ||
let granted = match fs::read_to_string(cache_path.clone()) { | ||
Ok(raw_string) => PermissionCache::from_string(raw_string).unwrap_or_default(), | ||
Err(_) => GrantedPermission::default(), | ||
}; | ||
|
||
PermissionCache { | ||
path: cache_path, | ||
granted, | ||
} | ||
} | ||
|
||
pub fn write_to_file(&self) -> std::io::Result<()> { | ||
let mut f = File::create(&self.path)?; | ||
write!(f, "{}", PermissionCache::to_string(&self.granted))?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters