Clean up put_key impl

This commit is contained in:
Phillip Davis 2022-07-05 21:06:58 -04:00
parent 63e1fb4f96
commit 5d80ff41a2
No known key found for this signature in database
GPG Key ID: EF6116274E70E43F
2 changed files with 18 additions and 71 deletions

View File

@ -1,7 +1,8 @@
use enum_iterator::IntoEnumIterator; use enum_iterator::IntoEnumIterator;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::hash::Hash;
#[derive(IntoEnumIterator, Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq)] #[derive(IntoEnumIterator, Copy, Clone, Serialize, Deserialize, Debug, Eq, PartialEq, Hash)]
#[repr(u8)] #[repr(u8)]
pub enum Action { pub enum Action {
#[serde(rename = "*")] #[serde(rename = "*")]

View File

@ -7,6 +7,7 @@ use std::ops::Deref;
use std::path::Path; use std::path::Path;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
use std::collections::HashSet;
use enum_iterator::IntoEnumIterator; use enum_iterator::IntoEnumIterator;
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
@ -87,76 +88,21 @@ impl HeedAuthStore {
// create inverted database. // create inverted database.
let db = self.action_keyid_index_expiration; let db = self.action_keyid_index_expiration;
let actions = if key.actions.contains(&Action::All) { let mut actions = HashSet::new();
// if key.actions contains All, we iterate over all actions. for action in &key.actions {
Action::into_enum_iter().collect() match action {
} else if key.actions.contains(&Action::DocumentsAll) { Action::All => actions.extend(Action::into_enum_iter()),
// if key.actions.contains.DocumentsAll add all actions related to documents. Action::DocumentsAll => { actions.extend([Action::DocumentsGet, Action::DocumentsDelete, Action::DocumentsGet].iter()); },
key.actions Action::IndexesAll => { actions.extend([Action::IndexesAdd, Action::IndexesDelete, Action::IndexesUpdate, Action::IndexesUpdate].iter()); },
.iter() Action::SettingsAll => { actions.extend([Action::SettingsGet, Action::SettingsUpdate].iter()); },
.cloned() Action::DumpsAll => { actions.insert(Action::DumpsCreate); },
.filter(|action| { Action::TasksAll => { actions.insert(Action::TasksGet); },
// Prevents duplicate entries in the actions vector Action::StatsAll => { actions.insert(Action::StatsGet); },
*action != Action::DocumentsAdd other => { actions.insert(*other); }
&& *action != Action::DocumentsDelete }
&& *action != Action::DocumentsGet }
})
.chain(vec![ let actions = actions.iter().collect::<Vec<&Action>>();
Action::DocumentsAdd,
Action::DocumentsDelete,
Action::DocumentsGet,
])
.collect()
} else if key.actions.contains(&Action::IndexesAll) {
key.actions
.iter()
.cloned()
.filter(|action| {
*action != Action::IndexesAdd
&& *action != Action::IndexesGet
&& *action != Action::IndexesDelete
&& *action != Action::IndexesUpdate
})
.chain(vec![
Action::IndexesAdd,
Action::IndexesGet,
Action::IndexesDelete,
Action::IndexesUpdate,
])
.collect()
} else if key.actions.contains(&Action::SettingsAll) {
key.actions
.iter()
.cloned()
.filter(|action| {
*action != Action::SettingsGet && *action != Action::SettingsUpdate
})
.chain(vec![Action::SettingsGet, Action::SettingsUpdate])
.collect()
} else if key.actions.contains(&Action::TasksAll) {
key.actions
.iter()
.cloned()
.filter(|action| *action != Action::TasksGet)
.chain(vec![Action::TasksGet])
.collect()
} else if key.actions.contains(&Action::DumpsAll) {
key.actions
.iter()
.cloned()
.filter(|action| *action != Action::DumpsCreate)
.chain(vec![Action::DumpsCreate])
.collect()
} else if key.actions.contains(&Action::StatsAll) {
key.actions
.iter()
.cloned()
.filter(|action| *action != Action::StatsGet)
.chain(vec![Action::StatsGet])
.collect()
} else {
key.actions.clone()
};
let no_index_restriction = key.indexes.contains(&StarOr::Star); let no_index_restriction = key.indexes.contains(&StarOr::Star);
for action in actions { for action in actions {