From 72de021319cae760d3b50ea6486967b0f07df155 Mon Sep 17 00:00:00 2001 From: MFijak Date: Mon, 8 Aug 2022 10:23:28 +0200 Subject: [PATCH] converted to HashMap --- src/api/core/ciphers.rs | 17 +++++++++++++---- src/db/models/cipher.rs | 12 +++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 621fbfc3..fa80e31f 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -1497,8 +1497,8 @@ pub struct CipherSyncData { pub cipher_collections: HashMap>, pub user_organizations: HashMap, pub user_collections: HashMap, - pub user_collections_groups: Vec, - pub user_groups: Vec, + pub user_collections_groups: HashMap, + pub user_groups: HashMap, } pub enum CipherSyncType { @@ -1554,8 +1554,17 @@ impl CipherSyncData { .collect() .await; - let user_collections_groups = CollectionGroup::find_by_user(user_uuid, conn).await; - let user_groups = Group::find_by_user(user_uuid, conn).await; + // Generate a HashMap with the collections_uuid as key and the CollectionGroup record + let user_collections_groups = stream::iter(CollectionGroup::find_by_user(user_uuid, conn).await) + .map(|collection_group| (collection_group.collections_uuid.clone(), collection_group)) + .collect() + .await; + + // Generate a HashMap with the group.uuid as key and the Group record + let user_groups = stream::iter(Group::find_by_user(user_uuid, conn).await) + .map(|group| (group.uuid.clone(), group)) + .collect() + .await; Self { cipher_attachments, diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 574d6202..01c11bb1 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -364,7 +364,9 @@ impl Cipher { conn: &DbConn, ) -> bool { match cipher_sync_data { - Some(cipher_sync_data) => cipher_sync_data.user_groups.iter().any(|group| group.access_all), + Some(cipher_sync_data) => { + cipher_sync_data.user_groups.iter().any(|hash_map_entry| hash_map_entry.1.access_all) + } None => Group::is_in_full_access_group(user_uuid, conn).await, } } @@ -400,12 +402,8 @@ impl Cipher { } //Group permissions - if let Some(gc) = cipher_sync_data - .user_collections_groups - .iter() - .find(|collection_group| collection_group.collections_uuid == collection.clone()) - { - rows.push((gc.read_only, gc.hide_passwords)); + if let Some(cg) = cipher_sync_data.user_collections_groups.get(collection) { + rows.push((cg.read_only, cg.hide_passwords)); } } }