acl: Implement query functions
This commit is contained in:
parent
02fbb6804e
commit
3923654c94
1 changed files with 37 additions and 17 deletions
|
@ -5,18 +5,19 @@ pub struct CSV {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_resource_name(username: String, id: i8) -> Result<String, ()> {
|
impl CSV {
|
||||||
let mut ret = Err(());
|
fn find<T: for<'a> Deserialize<'a> + PartialEq>(&self, needle: T, path: &str) -> Option<T> {
|
||||||
let file = std::fs::File::open("./resource_shortcuts.csv").unwrap();
|
let p = format!("{}/{}", self.path, path);
|
||||||
|
println!("{}", p);
|
||||||
|
let file = std::fs::File::open(p).unwrap();
|
||||||
let mut reader = csv::Reader::from_reader(file);
|
let mut reader = csv::Reader::from_reader(file);
|
||||||
for result in reader.deserialize() {
|
reader
|
||||||
let rs: ResourceShortcuts = result.unwrap();
|
.deserialize()
|
||||||
if rs.username == username && rs.id == id {
|
.map(|x: Result<T, csv::Error>| x.unwrap())
|
||||||
ret = Ok(rs.resource);
|
.collect::<Vec<T>>()
|
||||||
break;
|
.into_iter()
|
||||||
|
.find(|x: &T| x == &needle)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ACL for CSV {
|
impl ACL for CSV {
|
||||||
|
@ -49,11 +50,30 @@ impl ACL for CSV {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(username) = get_username(method, identifier) {
|
/// Queries
|
||||||
if let Ok(resource_name) = get_resource_name(username.clone(), resource_shortcut) {
|
fn get_username(&self, method: String, identifier: String) -> Option<String> {
|
||||||
if auth_user(username, resource_name).is_ok() {
|
let needle = AuthMethod {
|
||||||
println!("Open Sesame!");
|
method: method,
|
||||||
|
identifier: identifier,
|
||||||
|
username: "".to_string(),
|
||||||
|
};
|
||||||
|
let res = self.find::<AuthMethod>(needle, "auth_methods.csv").unwrap();
|
||||||
|
Some(res.username)
|
||||||
}
|
}
|
||||||
|
fn get_resource(&self, username: String, id: i8) -> Option<String> {
|
||||||
|
let needle = ResourceShortcuts {
|
||||||
|
username: username,
|
||||||
|
id: id,
|
||||||
|
resource: "".to_string(),
|
||||||
|
};
|
||||||
|
let res = self
|
||||||
|
.find::<ResourceShortcuts>(needle, "resource_shortcuts.csv")
|
||||||
|
.unwrap();
|
||||||
|
Some(res.resource)
|
||||||
}
|
}
|
||||||
|
fn is_allowed(&self, username: String, resource: String) -> Option<()> {
|
||||||
|
let needle = ACLEntry { username, resource };
|
||||||
|
self.find::<ACLEntry>(needle, "acl_entries.csv").unwrap();
|
||||||
|
Some(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue