diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-05-10 21:38:57 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-05-10 23:08:31 +0200 |
commit | 09b18e8e5722ad989175a6ec7c09b1edccfa962d (patch) | |
tree | 996f5c8595cbf927f561cb557a8a3d24b3e4f5b6 /server | |
parent | 507a364e04aeda658041c3794ab96667ebdf531e (diff) | |
download | sonarqube-09b18e8e5722ad989175a6ec7c09b1edccfa962d.tar.gz sonarqube-09b18e8e5722ad989175a6ec7c09b1edccfa962d.zip |
Remove most usages of AuthorizationDao#keepAuthorizedProjectIds()
replaced by UserSession#keepAuthorizedComponents()
Diffstat (limited to 'server')
10 files changed, 24 insertions, 94 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java index 06eda851274..7f7639c1416 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/AuthorizationDao.java @@ -124,6 +124,10 @@ public class AuthorizationDao implements Dao { 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, diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java index 5c57fbb2f66..658de71faa9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -19,11 +19,8 @@ */ 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; @@ -32,7 +29,7 @@ import org.sonar.api.server.ws.Response; 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; @@ -46,13 +43,11 @@ import org.sonarqube.ws.WsComponents.SearchWsResponse; 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; @@ -156,23 +151,7 @@ public class SearchAction implements ComponentsWsAction { 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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java index 88bdc899617..800345779c9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/favorite/ws/SearchAction.java @@ -86,7 +86,7 @@ public class SearchAction implements FavoritesWsAction { 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()) @@ -97,15 +97,9 @@ public class SearchAction implements FavoritesWsAction { } } - 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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java index dc51adcc989..573c6c83e9b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java @@ -70,7 +70,6 @@ import static org.sonar.api.rule.Severity.BLOCKER; 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; @@ -302,7 +301,7 @@ public class BulkChangeAction implements IssuesWsAction { 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() @@ -321,14 +320,8 @@ public class BulkChangeAction implements IssuesWsAction { 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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java index bc21efd66fb..6c216afb214 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchAction.java @@ -49,7 +49,6 @@ import static org.sonar.api.resources.Qualifiers.PROJECT; 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; @@ -141,21 +140,7 @@ public class SearchAction implements MeasuresWsAction { } 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() { diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java index 2a119d7a7ed..6e3836f783c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/IndexAction.java @@ -21,25 +21,20 @@ package org.sonar.server.project.ws; 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; @@ -99,7 +94,7 @@ public class IndexAction implements ProjectsWsAction { @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) { @@ -132,20 +127,8 @@ public class IndexAction implements ProjectsWsAction { 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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java index b3d35693ad0..0a22c4c68da 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java @@ -228,7 +228,7 @@ public class SearchActionTest { 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) @@ -245,8 +245,7 @@ public class SearchActionTest { } 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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/SearchActionTest.java index 0b5f908c9ec..25dcf91d6a4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/SearchActionTest.java @@ -28,12 +28,10 @@ import org.sonar.api.server.ws.WebService; 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; @@ -62,7 +60,6 @@ public class SearchActionTest { @Rule public DbTester db = DbTester.create(); private DbClient dbClient = db.getDbClient(); - private DbSession dbSession = db.getSession(); private FavoriteFinder favoriteFinder = new FavoriteFinder(dbClient, userSession); @@ -132,7 +129,6 @@ public class SearchActionTest { 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(); @@ -186,8 +182,8 @@ public class SearchActionTest { 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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java index 120234b0eaf..8b15aa45dc6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchActionTest.java @@ -21,6 +21,7 @@ package org.sonar.server.measure.ws; 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; @@ -510,9 +511,6 @@ public class SearchActionTest { } 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)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java index a732a8ead2d..8c321b2e9cd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/IndexActionTest.java @@ -224,8 +224,7 @@ public class IndexActionTest { } 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) { |