From 5d80ff41a29feebbe03069d00616af1c5c7b1b17 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Tue, 5 Jul 2022 21:06:58 -0400 Subject: [PATCH] Clean up put_key impl --- meilisearch-auth/src/action.rs | 3 +- meilisearch-auth/src/store.rs | 86 +++++++--------------------------- 2 files changed, 18 insertions(+), 71 deletions(-) diff --git a/meilisearch-auth/src/action.rs b/meilisearch-auth/src/action.rs index cecc919aa..94a15eb9d 100644 --- a/meilisearch-auth/src/action.rs +++ b/meilisearch-auth/src/action.rs @@ -1,7 +1,8 @@ use enum_iterator::IntoEnumIterator; 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)] pub enum Action { #[serde(rename = "*")] diff --git a/meilisearch-auth/src/store.rs b/meilisearch-auth/src/store.rs index 06bb87275..8697afc38 100644 --- a/meilisearch-auth/src/store.rs +++ b/meilisearch-auth/src/store.rs @@ -7,6 +7,7 @@ use std::ops::Deref; use std::path::Path; use std::str; use std::sync::Arc; +use std::collections::HashSet; use enum_iterator::IntoEnumIterator; use hmac::{Hmac, Mac}; @@ -87,76 +88,21 @@ impl HeedAuthStore { // create inverted database. let db = self.action_keyid_index_expiration; - let actions = if key.actions.contains(&Action::All) { - // if key.actions contains All, we iterate over all actions. - Action::into_enum_iter().collect() - } else if key.actions.contains(&Action::DocumentsAll) { - // if key.actions.contains.DocumentsAll add all actions related to documents. - key.actions - .iter() - .cloned() - .filter(|action| { - // Prevents duplicate entries in the actions vector - *action != Action::DocumentsAdd - && *action != Action::DocumentsDelete - && *action != Action::DocumentsGet - }) - .chain(vec![ - 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 mut actions = HashSet::new(); + for action in &key.actions { + match action { + Action::All => actions.extend(Action::into_enum_iter()), + Action::DocumentsAll => { actions.extend([Action::DocumentsGet, Action::DocumentsDelete, Action::DocumentsGet].iter()); }, + Action::IndexesAll => { actions.extend([Action::IndexesAdd, Action::IndexesDelete, Action::IndexesUpdate, Action::IndexesUpdate].iter()); }, + Action::SettingsAll => { actions.extend([Action::SettingsGet, Action::SettingsUpdate].iter()); }, + Action::DumpsAll => { actions.insert(Action::DumpsCreate); }, + Action::TasksAll => { actions.insert(Action::TasksGet); }, + Action::StatsAll => { actions.insert(Action::StatsGet); }, + other => { actions.insert(*other); } + } + } + + let actions = actions.iter().collect::>(); let no_index_restriction = key.indexes.contains(&StarOr::Star); for action in actions {