From 50c07cc95cd4f5b7fc6c3009f767f268281a6d6e Mon Sep 17 00:00:00 2001 From: pfurio Date: Mon, 15 Apr 2024 15:23:52 +0200 Subject: [PATCH] catalog: remove user=organizationId exclusion, #TASK-6032 --- .../db/mongodb/UserMongoDBAdaptor.java | 15 ++--- .../opencga/catalog/managers/UserManager.java | 4 +- .../catalog/managers/CatalogManagerTest.java | 60 +++++++++++++++++++ 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java index a8db293e258..d2c0f14cd5f 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java @@ -353,21 +353,16 @@ private void extractSharedProjects(DataResult userDataResult, QueryOptions studyMap.put(study.getFqn(), study); studyProjectMap.put(study.getFqn(), project.getFqn()); - String owner = study.getFqn().split("@")[0]; - if (study.getGroups() != null) { for (Group group : study.getGroups()) { if (StudyManager.MEMBERS.equals(group.getId())) { // Add all the users that should be able to see the study to the map for (String userId : group.getUserIds()) { - // Exclude owner of the project - if (!owner.equals(userId)) { - if (users.contains(userId)) { - if (!userStudyMap.containsKey(userId)) { - userStudyMap.put(userId, new ArrayList<>()); - } - userStudyMap.get(userId).add(study.getFqn()); + if (users.contains(userId)) { + if (!userStudyMap.containsKey(userId)) { + userStudyMap.put(userId, new ArrayList<>()); } + userStudyMap.get(userId).add(study.getFqn()); } } break; @@ -378,7 +373,7 @@ private void extractSharedProjects(DataResult userDataResult, QueryOptions } } - // Add sharedProject information + // Add project information for (User user : userDataResult.getResults()) { if (userStudyMap.containsKey(user.getId())) { Map> projectStudyMap = new HashMap<>(); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java index 96098fedcff..f6a50e63890 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java @@ -519,7 +519,7 @@ public OpenCGAResult get(String organizationId, String userId, QueryOption * @return The requested users * @throws CatalogException CatalogException */ - public OpenCGAResult get(String organizationId, List userIdList, QueryOptions options, String token) + public OpenCGAResult get(@Nullable String organizationId, List userIdList, QueryOptions options, String token) throws CatalogException { ParamUtils.checkNotEmptyArray(userIdList, "userId"); ParamUtils.checkParameter(token, "token"); @@ -532,7 +532,7 @@ public OpenCGAResult get(String organizationId, List userIdList, Q .append("token", token); JwtPayload jwtPayload = validateToken(token); - if (userIdList.size() == 1 && jwtPayload.getUserId().equals(userIdList.get(0)) && StringUtils.isEmpty(organizationId)) { + if (StringUtils.isEmpty(organizationId)) { organizationId = jwtPayload.getOrganization(); } String userId = jwtPayload.getUserId(organizationId); diff --git a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java index 07edb4610b7..4854b1bb8da 100644 --- a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java +++ b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java @@ -17,6 +17,7 @@ package org.opencb.opencga.catalog.managers; import com.mongodb.BasicDBObject; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Ignore; @@ -256,6 +257,65 @@ public void testGetUserInfo() throws CatalogException { studyAdminToken1); } + @Test + public void testGetProjectsFromUserInfo() throws CatalogException { + String userId = organizationId; + catalogManager.getUserManager().create(userId, "test", "mail@mail.com", TestParamConstants.PASSWORD, organizationId, null, + opencgaToken); + catalogManager.getStudyManager().updateGroup(studyFqn, ParamConstants.MEMBERS_GROUP, ParamUtils.BasicUpdateAction.ADD, + new GroupUpdateParams(Collections.singletonList("test")), ownerToken); + String token = catalogManager.getUserManager().login(organizationId, userId, TestParamConstants.PASSWORD).getToken(); + + DataResult user = catalogManager.getUserManager().get(organizationId, userId, new QueryOptions(), token); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(organizationId, normalUserId3, new QueryOptions(), normalToken3); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(organizationId, orgOwnerUserId, new QueryOptions(), ownerToken); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(organizationId, orgAdminUserId1, new QueryOptions(), orgAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(organizationId, studyAdminUserId1, new QueryOptions(), studyAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(organizationId, normalUserId1, new QueryOptions(), orgAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + + user = catalogManager.getUserManager().get(null, normalUserId1, new QueryOptions(), normalToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(null, normalUserId3, new QueryOptions(), normalToken3); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(null, orgOwnerUserId, new QueryOptions(), ownerToken); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(null, orgAdminUserId1, new QueryOptions(), orgAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(null, studyAdminUserId1, new QueryOptions(), studyAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + + user = catalogManager.getUserManager().get(null, normalUserId1, new QueryOptions(), orgAdminToken1); + assertTrue(CollectionUtils.isNotEmpty(user.first().getProjects())); + System.out.println(user.first().getProjects().size()); + } + @Test public void testModifyUser() throws CatalogException, InterruptedException, IOException { ObjectMap params = new ObjectMap();