acl: Accept &self as a function parameter

This commit is contained in:
George Kaklamanos 2023-11-20 20:57:25 +02:00
parent c3cc8a2bd5
commit 26714d24f6
No known key found for this signature in database
GPG key ID: C0CAB8A6BDC9399D

View file

@ -37,50 +37,56 @@ impl PartialEq for ResourceShortcuts {
pub trait ACL {
/// `Option`s on delete operations mean that all associated data is removed
/// ACLEntry
fn allow_access(user: String, resource: String);
fn deny_access(user: Option<String>, resource: Option<String>);
fn allow_access(&self, user: String, resource: String);
fn deny_access(&self, user: Option<String>, resource: Option<String>);
/// AuthMethod
fn add_auth_method(user: String, method: String, identifier: String);
fn del_auth_method(user: Option<String>, method: Option<String>, identifier: Option<String>);
fn add_auth_method(&self, user: String, method: String, identifier: String);
fn del_auth_method(
&self,
user: Option<String>,
method: Option<String>,
identifier: Option<String>,
);
/// ResourceShortcuts
fn add_shortcut(user: String, resource: String, shortcut: i8);
fn del_shortcut(user: Option<String>, resource: Option<String>, shortcut: Option<i8>);
fn add_shortcut(&self, user: String, resource: String, shortcut: i8);
fn del_shortcut(&self, user: Option<String>, resource: Option<String>, shortcut: Option<i8>);
/// Queries
fn get_username(method: String, identifier: String) -> Result<String, ()>;
fn get_resource(username: String, shortcut: i8) -> Result<String, ()>;
fn is_allowed(username: String, resource: String) -> Result<(), ()>;
fn get_username(&self, method: String, identifier: String) -> Result<String, ()>;
fn get_resource(&self, username: String, shortcut: i8) -> Result<String, ()>;
fn is_allowed(&self, username: String, resource: String) -> Result<(), ()>;
fn del_user(user: String) {
Self::deny_access(Some(user.clone()), None);
Self::del_auth_method(Some(user.clone()), None, None);
Self::del_shortcut(Some(user), None, None);
fn del_user(&self, user: String) {
self.deny_access(Some(user.clone()), None);
self.del_auth_method(Some(user.clone()), None, None);
self.del_shortcut(Some(user), None, None);
}
fn del_resource(resource: String) {
Self::deny_access(None, Some(resource.clone()));
Self::del_shortcut(None, Some(resource), None);
fn del_resource(&self, resource: String) {
self.deny_access(None, Some(resource.clone()));
self.del_shortcut(None, Some(resource), None);
}
fn create_user(
&self,
username: String,
resource: String,
method: String,
identifier: String,
shortcut: i8,
) {
Self::allow_access(username.clone(), resource.clone());
Self::add_auth_method(username.clone(), method.clone(), identifier.clone());
Self::add_shortcut(username, resource, shortcut);
self.allow_access(username.clone(), resource.clone());
self.add_auth_method(username.clone(), method.clone(), identifier.clone());
self.add_shortcut(username, resource, shortcut);
}
fn auth_user(method: String, identifier: String, shortcut: i8) {
if let Ok(username) = Self::get_username(method, identifier) {
if let Ok(resource) = Self::get_resource(username.clone(), shortcut) {
if Self::is_allowed(username, resource).is_ok() {
println!("Open Sesame!");
fn auth_user(&self, method: String, identifier: String, shortcut: i8) {
if let Ok(username) = self.get_username(method, identifier) {
if let Ok(resource) = self.get_resource(username.clone(), shortcut) {
if self.is_allowed(username.clone(), resource).is_ok() {
println!("Open Sesame! {}", username);
}
}
}