return mapper(dbSession).selectOrganizationUuidsOfUserWithGlobalPermission(userId, permission);
}
+ /**
+ * @deprecated replaced by {@link #keepAuthorizedProjectUuids(DbSession, Collection, Integer, String)}
+ */
+ @Deprecated
public Set<Long> keepAuthorizedProjectIds(DbSession dbSession, Collection<Long> componentIds, @Nullable Integer userId, String permission) {
return executeLargeInputsIntoSet(
componentIds,
*/
package org.sonar.server.component.ws;
-import java.util.Collection;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import java.util.Set;
import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.Paging;
-import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonarqube.ws.client.component.SearchWsRequest;
import static com.google.common.base.Preconditions.checkArgument;
-import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.util.Protobuf.setNullable;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
-import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
+import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE;
private List<ComponentDto> searchComponents(DbSession dbSession, OrganizationDto organization, ComponentQuery query, Paging paging) {
List<ComponentDto> componentDtos = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), query, paging.offset(), paging.pageSize());
- return filterAuthorizedComponents(dbSession, componentDtos);
- }
-
- private List<ComponentDto> filterAuthorizedComponents(DbSession dbSession, List<ComponentDto> componentDtos) {
- if (userSession.isRoot()) {
- // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
- // a call to UserSession, which would transparently support roots.
- // Meanwhile root is explicitly handled.
- return componentDtos;
- }
- Set<String> projectUuids = componentDtos.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
- List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
- Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
- Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
- return componentDtos.stream()
- .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
- .collect(MoreCollectors.toList());
+ return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
}
private static SearchWsResponse buildResponse(List<ComponentDto> components, OrganizationDto organization, Paging paging) {
private SearchResults toSearchResults(SearchRequest request) {
userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) {
- List<ComponentDto> authorizedFavorites = getAuthorizedFavorites(dbSession);
+ List<ComponentDto> authorizedFavorites = getAuthorizedFavorites();
Paging paging = Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal(authorizedFavorites.size());
List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
.skip(paging.offset())
}
}
- private List<ComponentDto> getAuthorizedFavorites(DbSession dbSession) {
+ private List<ComponentDto> getAuthorizedFavorites() {
List<ComponentDto> componentDtos = favoriteFinder.list();
- Set<Long> favoriteComponentIds = componentDtos.stream()
- .map(ComponentDto::getId)
- .collect(MoreCollectors.toSet(componentDtos.size()));
- Set<Long> authorizedFavoriteComponentIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, favoriteComponentIds, userSession.getUserId(), UserRole.USER);
- return componentDtos.stream()
- .filter(dto -> authorizedFavoriteComponentIds.contains(dto.getId()))
- .collect(MoreCollectors.toList());
+ return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
}
private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) {
import static org.sonar.api.rules.RuleType.BUG;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
-import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
import static org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER;
List<IssueDto> allIssues = dbClient.issueDao().selectByKeys(dbSession, issueKeys);
List<ComponentDto> allProjects = getComponents(dbSession, allIssues.stream().map(IssueDto::getProjectUuid).collect(MoreCollectors.toSet()));
- this.projectsByUuid = getAuthorizedProjects(dbSession, allProjects).stream().collect(uniqueIndex(ComponentDto::uuid, identity()));
+ this.projectsByUuid = getAuthorizedProjects(allProjects).stream().collect(uniqueIndex(ComponentDto::uuid, identity()));
this.issues = getAuthorizedIssues(allIssues);
this.componentsByUuid = getComponents(dbSession,
issues.stream().map(DefaultIssue::componentUuid).collect(MoreCollectors.toSet())).stream()
return dbClient.componentDao().selectByUuids(dbSession, componentUuids);
}
- private List<ComponentDto> getAuthorizedProjects(DbSession dbSession, List<ComponentDto> projectDtos) {
- Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
- Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
- projectDtos.stream().map(ComponentDto::getId).collect(toList()),
- userSession.getUserId(), UserRole.USER);
- return projectDtos.stream()
- .filter(project -> authorizedProjectIds.contains(projectIdsByUuids.get(project.projectUuid())))
- .collect(MoreCollectors.toList());
+ private List<ComponentDto> getAuthorizedProjects(List<ComponentDto> projectDtos) {
+ return userSession.keepAuthorizedComponents(UserRole.USER, projectDtos);
}
private List<DefaultIssue> getAuthorizedIssues(List<IssueDto> allIssues) {
import static org.sonar.api.resources.Qualifiers.SUBVIEW;
import static org.sonar.api.resources.Qualifiers.VIEW;
import static org.sonar.core.util.stream.MoreCollectors.toList;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.server.measure.ws.MeasureDtoToWsMeasure.updateMeasureBuilder;
import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
}
private List<ComponentDto> getAuthorizedProjects(List<ComponentDto> componentDtos) {
- if (userSession.isRoot()) {
- // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
- // a call to UserSession, which would transparently support roots.
- // Meanwhile root is explicitly handled.
- return componentDtos;
- }
- Set<String> projectUuids = componentDtos.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
- List<ComponentDto> projectDtos = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
- Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
- Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
- projectDtos.stream().map(ComponentDto::getId).collect(toList()),
- userSession.getUserId(), UserRole.USER);
- return componentDtos.stream()
- .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
- .collect(MoreCollectors.toList());
+ return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
}
private List<MetricDto> searchMetrics() {
import com.google.common.io.Resources;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import java.util.Set;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.user.UserSession;
import static java.util.Optional.ofNullable;
-import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_INDEX;
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- List<ComponentDto> projects = getAuthorizedComponents(dbSession, searchComponents(dbSession, request));
+ List<ComponentDto> projects = getAuthorizedComponents(searchComponents(dbSession, request));
JsonWriter json = response.newJsonWriter();
json.beginArray();
for (ComponentDto project : projects) {
return projects;
}
- private List<ComponentDto> getAuthorizedComponents(DbSession dbSession, List<ComponentDto> components) {
- if (userSession.isRoot() || components.isEmpty()) {
- // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
- // a call to UserSession, which would transparently support roots.
- // Meanwhile root is explicitly handled.
- return components;
- }
- Set<String> projectUuids = components.stream().map(ComponentDto::projectUuid).collect(MoreCollectors.toSet());
- List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
- Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
- Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
- return components.stream()
- .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
- .collect(MoreCollectors.toList());
+ private List<ComponentDto> getAuthorizedComponents(List<ComponentDto> components) {
+ return userSession.keepAuthorizedComponents(UserRole.USER, components);
}
private static void addProject(JsonWriter json, ComponentDto project) {
ComponentDto directory = newDirectory(module, "path/to/directoy").setUuid("directory-uuid").setKey("directory-key").setName("Directory Name");
db.components().insertComponents(project, module, directory,
newFileDto(module, directory, "file-uuid").setKey("file-key").setLanguage("java").setName("File Name"));
- setBrowsePermissionOnUser(project);
+ userSession.addProjectPermission(UserRole.USER, project);
String response = ws.newRequest()
.setMediaType(MediaTypes.JSON)
}
private void setBrowsePermissionOnUser(ComponentDto... projects) {
- Arrays.stream(projects).forEach(project -> db.users().insertProjectPermissionOnUser(user, UserRole.USER, project));
- db.getSession().commit();
+ Arrays.stream(projects).forEach(project -> userSession.addProjectPermission(UserRole.USER, project));
}
private SearchWsResponse call(SearchWsRequest wsRequest) {
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.permission.UserPermissionDto;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.favorite.FavoriteFinder;
import org.sonar.server.tester.UserSessionRule;
@Rule
public DbTester db = DbTester.create();
private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
private FavoriteFinder favoriteFinder = new FavoriteFinder(dbClient, userSession);
ComponentDto otherUserFavorite = ComponentTesting.newPrivateProjectDto(organizationDto).setKey("K42");
db.components().insertComponent(otherUserFavorite);
db.favorites().add(otherUserFavorite, 42);
- dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(organizationDto.getUuid(), UserRole.USER, 42, otherUserFavorite.getId()));
db.commit();
SearchResponse result = call();
private void addComponent(ComponentDto component) {
db.components().insertComponent(component);
db.favorites().add(component, USER_ID);
- dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(component.getOrganizationUuid(), UserRole.USER, USER_ID, component.getId()));
db.commit();
+ userSession.addProjectPermission(UserRole.USER, component);
}
private SearchResponse call(@Nullable Integer page, @Nullable Integer pageSize) {
import com.google.common.base.Joiner;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
}
private void setBrowsePermissionOnUser(ComponentDto... projects) {
- for (ComponentDto project : projects) {
- db.users().insertProjectPermissionOnUser(user, UserRole.USER, project);
- }
- dbSession.commit();
+ Arrays.stream(projects).forEach(p -> userSession.addProjectPermission(UserRole.USER, p));
}
}
}
private void setBrowsePermissionOnUser(ComponentDto... projects) {
- Arrays.stream(projects).forEach(project -> db.users().insertProjectPermissionOnUser(user, UserRole.USER, project));
- db.getSession().commit();
+ Arrays.stream(projects).forEach(project -> userSession.addProjectPermission(UserRole.USER, project));
}
private void verifyResult(String json, String expectedJsonFile) {