Skip to content

Commit

Permalink
fix(groups): renaming and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
matlink committed Jan 1, 2024
1 parent 930adb8 commit 8311f45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/api/core/organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ async fn get_org_collections_details(org_id: &str, headers: ManagerHeadersLoose,

let coll_users = CollectionUser::find_by_organization(org_id, &mut conn).await;
// uuids of users in groups having access to all collections
let all_access_group_uuids = GroupUser::get_all_access_group_users_uuid(org_id, &mut conn).await;
let has_full_access_via_group = if CONFIG.org_groups_enabled() {
GroupUser::get_members_of_full_access_groups(org_id, &mut conn).await
} else {
vec![]
};

let has_full_access = user_org.access_all || has_full_access_via_group.contains(&user_org.uuid);

for col in Collection::find_by_organization(org_id, &mut conn).await {
let groups: Vec<Value> = if CONFIG.org_groups_enabled() {
Expand All @@ -342,9 +348,9 @@ async fn get_org_collections_details(org_id: &str, headers: ManagerHeadersLoose,
};

// uuids of users belonging to a group of this collection
let group_users = GroupUser::get_collection_group_users_uuid(&col.uuid, &mut conn).await;
let has_collection_access_via_group = GroupUser::get_group_members_for_collection(&col.uuid, &mut conn).await;

let mut assigned = false;
let mut assigned = has_full_access;
let users: Vec<Value> = coll_users
.iter()
.filter(|collection_user| collection_user.collection_uuid == col.uuid)
Expand All @@ -361,12 +367,7 @@ async fn get_org_collections_details(org_id: &str, headers: ManagerHeadersLoose,
// if current user is in any collection-assigned group
// or in a group having access to all collections
// or itself has access to all collections
if group_users.contains(&user_org.uuid)
|| all_access_group_uuids.contains(&user_org.uuid)
|| user_org.access_all
{
assigned = true;
}
assigned = !assigned && has_collection_access_via_group.contains(&user_org.uuid);

let mut json_object = col.to_json();
json_object["Assigned"] = json!(assigned);
Expand Down
8 changes: 4 additions & 4 deletions src/db/models/group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashSet;

use chrono::{NaiveDateTime, Utc};
use serde_json::Value;

Expand Down Expand Up @@ -488,22 +486,23 @@ impl GroupUser {
}}
}

pub async fn get_collection_group_users_uuid(collection_uuid: &str, conn: &mut DbConn) -> HashSet<String> {
pub async fn get_group_members_for_collection(collection_uuid: &str, conn: &mut DbConn) -> Vec<String> {
db_run! { conn: {
groups_users::table
.inner_join(collections_groups::table.on(
collections_groups::groups_uuid.eq(groups_users::groups_uuid)
))
.filter(collections_groups::collections_uuid.eq(collection_uuid))
.select(groups_users::users_organizations_uuid)
.distinct()
.load::<String>(conn)
.expect("Error loading group users for collection")
}}
.into_iter()
.collect()
}

pub async fn get_all_access_group_users_uuid(org_uuid: &str, conn: &mut DbConn) -> HashSet<String> {
pub async fn get_members_of_full_access_groups(org_uuid: &str, conn: &mut DbConn) -> Vec<String> {
db_run! { conn: {
groups_users::table
.inner_join(groups::table.on(
Expand All @@ -512,6 +511,7 @@ impl GroupUser {
.filter(groups::organizations_uuid.eq(org_uuid))
.filter(groups::access_all.eq(true))
.select(groups_users::users_organizations_uuid)
.distinct()
.load::<String>(conn)
.expect("Error loading all access group users for organization")
}}
Expand Down

0 comments on commit 8311f45

Please sign in to comment.