Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK-3908 - Unable to search for shared projects #2248

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.catalog.utils.FqnUtils;
import org.opencb.opencga.catalog.utils.UuidUtils;
import org.opencb.opencga.core.common.TimeUtils;
import org.opencb.opencga.core.config.Configuration;
Expand Down Expand Up @@ -705,13 +706,14 @@ private MongoDBIterator<Document> getMongoCursor(ClientSession clientSession, Qu
studyQuery.putIfNotEmpty(StudyDBAdaptor.QueryParams.UID.key(), query.getString(QueryParams.STUDY_UID.key()));
studyQuery.putIfNotEmpty(StudyDBAdaptor.QueryParams.ID.key(), query.getString(QueryParams.STUDY_ID.key()));
studyQuery.putIfNotEmpty(StudyDBAdaptor.QueryParams.OWNER.key(), query.getString(QueryParams.USER_ID.key()));
OpenCGAResult<Document> queryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(clientSession, studyQuery,
OpenCGAResult<Document> studiesResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(clientSession, studyQuery,
new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.FQN.key()), user);

if (queryResult.getNumResults() > 0) {
if (studiesResult.getNumResults() > 0) {
Set<String> projectFqn = new HashSet<>();
for (Document result : queryResult.getResults()) {
projectFqn.add(result.getString(StudyDBAdaptor.QueryParams.FQN.key()).split(":")[0]);
for (Document study : studiesResult.getResults()) {
String studyFqn = study.getString(StudyDBAdaptor.QueryParams.FQN.key());
projectFqn.add(FqnUtils.toProjectFqn(studyFqn));
}
query.put(QueryParams.FQN.key(), new ArrayList<>(projectFqn));
return getMongoCursor(clientSession, query, options);
Expand All @@ -730,7 +732,7 @@ private MongoDBIterator<Document> getMongoCursor(ClientSession clientSession, Qu
List<Long> externalUids = new ArrayList<>(); // Project uids from projects owned by a user other than "user"
while (iterator.hasNext()) {
Project project = iterator.next();
if (project.getFqn().startsWith(user + "@")) {
if (FqnUtils.getUser(project.getFqn()).equals(user)) {
ownerFqns.add(project.getFqn());
} else {
externalUids.add(project.getUid());
Expand All @@ -741,12 +743,13 @@ private MongoDBIterator<Document> getMongoCursor(ClientSession clientSession, Qu
List<String> externalFqns = new ArrayList<>();
if (!externalUids.isEmpty()) {
Query studyQuery = new Query(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), externalUids);
OpenCGAResult<Document> queryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(clientSession, studyQuery,
OpenCGAResult<Document> studiesResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().nativeGet(clientSession, studyQuery,
new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.FQN.key()), user);
if (queryResult.getNumResults() > 0) {
if (studiesResult.getNumResults() > 0) {
Set<String> projectFqn = new HashSet<>();
for (Document result : queryResult.getResults()) {
projectFqn.add(result.getString(StudyDBAdaptor.QueryParams.FQN.key().split(":")[0]));
for (Document study : studiesResult.getResults()) {
String studyFqn = study.getString(StudyDBAdaptor.QueryParams.FQN.key());
projectFqn.add(FqnUtils.toProjectFqn(studyFqn));
}
externalFqns.addAll(projectFqn);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.opencb.opencga.catalog.utils;

import org.opencb.opencga.core.api.ParamConstants;

import java.util.Objects;

public class FqnUtils {

public static class FQN {
private final String user;
private final String project;
private final String study;
private final String projectFqn;

public FQN(String fqn) {
if (fqn == null || fqn.isEmpty()) {
throw new IllegalArgumentException("Empty FQN");
}
int idx1 = fqn.indexOf(ParamConstants.USER_PROJECT_SEPARATOR);
int idx2 = fqn.indexOf(ParamConstants.PROJECT_STUDY_SEPARATOR, idx1 + 1);
if (idx1 < 0) {
throw new IllegalArgumentException("Invalid FQN. Expected <user>@<project>[:<study>]. Got '" + fqn + "'");
}
user = fqn.substring(0, idx1);
if (idx2 < 0) {
project = fqn.substring(idx1 + 1);
projectFqn = fqn;
study = null;
} else {
project = fqn.substring(idx1 + 1, idx2);
projectFqn = fqn.substring(0, idx2);
study = fqn.substring(idx2 + 1);
}
}

public FQN(String user, String project) {
this(user, project, null);
}

public FQN(String user, String project, String study) {
this.user = Objects.requireNonNull(user);
this.project = Objects.requireNonNull(project);
projectFqn = user + ParamConstants.USER_PROJECT_SEPARATOR + project;
this.study = study;
}

public String getUser() {
return user;
}

public String getProject() {
return project;
}

public String getProjectFqn() {
return projectFqn;
}

public String getStudy() {
return study;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder(user)
.append(ParamConstants.USER_PROJECT_SEPARATOR)
.append(project);
if (study != null) {
sb.append(ParamConstants.PROJECT_STUDY_SEPARATOR)
.append(study);
}
return sb.toString();
}
}

public static FQN parse(String fqn) {
return new FQN(fqn);
}

public static String getUser(String fqn) {
return new FQN(fqn).getUser();
}

public static String getProject(String fqn) {
return new FQN(fqn).getProject();
}

public static String getStudy(String fqn) {
return new FQN(fqn).getStudy();
}

public static String toProjectFqn(String fqn) {
return new FQN(fqn).getProjectFqn();
}

public static String buildFqn(String user, String project) {
return new FQN(user, project).toString();
}

public static String buildFqn(String user, String project, String study) {
return new FQN(user, project, study).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,24 @@ public void searchSampleNoPermissions() throws CatalogException {

@Test
public void searchProjects() throws CatalogException {
catalogManager.getUserManager().create("userid", "User Name", "mail@ebi.ac.uk", TestParamConstants.PASSWORD, "", null, Account.AccountType.FULL, null);
String token = catalogManager.getUserManager().login("userid", TestParamConstants.PASSWORD).getToken();
OpenCGAResult<Project> projectOpenCGAResult = catalogManager.getProjectManager().search(new Query(), QueryOptions.empty(), token);
assertTrue(projectOpenCGAResult.getResults().isEmpty());
assertEquals(0, projectOpenCGAResult.getEvents().size());
String otherUser = "user_tmp";
catalogManager.getUserManager().create(otherUser, "User Name", "mail@ebi.ac.uk", TestParamConstants.PASSWORD, "", null, Account.AccountType.FULL, null);
String otherUsertoken = catalogManager.getUserManager().login(otherUser, TestParamConstants.PASSWORD).getToken();
OpenCGAResult<Project> result = catalogManager.getProjectManager()
.search(new Query(), QueryOptions.empty(), otherUsertoken);
assertTrue(result.getResults().isEmpty());
assertEquals(0, result.getEvents().size());

// Create a new study in project2 with some dummy permissions for user
String s2 = catalogManager.getStudyManager().create(project2, "s2", null, "Study 2", "", null, null, null, null, INCLUDE_RESULT, sessionIdUser2).first().getId();
catalogManager.getStudyManager().updateGroup(s2, "@members", ParamUtils.BasicUpdateAction.ADD,
new GroupUpdateParams(Collections.singletonList(otherUser)), sessionIdUser2);

result = catalogManager.getProjectManager()
.search(new Query(), QueryOptions.empty(), otherUsertoken);
assertEquals(1, result.getNumResults());
assertEquals(project2, result.first().getId());
assertEquals("user2@pmp", result.first().getFqn());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.opencb.opencga.catalog.utils;

import org.junit.Test;

import static org.junit.Assert.*;

public class FqnUtilsTest {

@Test
public void testFqnFull() {
FqnUtils.FQN fqn = new FqnUtils.FQN("user@project:study");
assertEquals("user", fqn.getUser());
assertEquals("project", fqn.getProject());
assertEquals("user@project", fqn.getProjectFqn());
assertEquals("study", fqn.getStudy());
assertEquals("user@project:study", fqn.toString());
}

@Test
public void testFqnPartial() {
FqnUtils.FQN fqn = new FqnUtils.FQN("user@project");
assertEquals("user", fqn.getUser());
assertEquals("project", fqn.getProject());
assertEquals("user@project", fqn.getProjectFqn());
assertEquals(null, fqn.getStudy());
assertEquals("user@project", fqn.toString());
}
}