From 4473576ffa2fe46b81ba91ffbc8a92581364fe74 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 6 Jun 2023 09:21:12 +0200 Subject: [PATCH] SONAR-19445 Review and improve the endpoints in WebAPI that rely on ComponentDto --- .../sonar/db/component/ComponentDbTester.java | 13 ++++- .../db/notification/NotificationDbTester.java | 18 +++---- .../java/org/sonar/db/user/UserDbTester.java | 14 +---- .../component/index/ComponentIndexer.java | 12 ++--- .../server/user/AbstractUserSession.java | 4 +- .../server/user/ThreadLocalUserSession.java | 6 +-- .../org/sonar/server/user/UserSession.java | 8 +-- .../sonar/server/tester/UserSessionRule.java | 11 ++-- .../server/notification/ws/AddActionIT.java | 46 ++++++++++------ .../server/notification/ws/ListActionIT.java | 30 ++++++----- .../notification/ws/RemoveActionIT.java | 11 ++-- .../server/badge/ws/ProjectBadgesSupport.java | 7 +-- .../sonar/server/ce/ws/ActivityAction.java | 25 ++++----- .../server/ce/ws/ActivityStatusAction.java | 19 +++---- .../org/sonar/server/ce/ws/CancelAction.java | 7 ++- .../server/component/ComponentFinder.java | 29 ++++++++--- .../server/favorite/ws/SearchAction.java | 8 +-- .../server/hotspot/ws/AddCommentAction.java | 2 +- .../sonar/server/hotspot/ws/AssignAction.java | 2 +- .../server/hotspot/ws/ChangeStatusAction.java | 2 +- .../hotspot/ws/DeleteCommentAction.java | 2 +- .../server/hotspot/ws/EditCommentAction.java | 2 +- .../server/hotspot/ws/HotspotWsSupport.java | 22 ++++---- .../sonar/server/hotspot/ws/ShowAction.java | 31 ++++++----- .../server/notification/ws/AddAction.java | 26 ++++------ .../server/notification/ws/ListAction.java | 28 +++++----- .../notification/ws/NotificationUpdater.java | 52 +++++++++---------- .../server/notification/ws/RemoveAction.java | 25 ++++----- .../permission/ws/PermissionWsSupport.java | 2 +- .../ws/template/ApplyTemplateAction.java | 2 +- .../projectdump/ws/ProjectDumpWsSupport.java | 6 +-- .../server/scannercache/ws/GetAction.java | 15 +++--- .../server/setting/ws/SettingValidations.java | 8 +-- .../sonar/server/setting/ws/ValuesAction.java | 8 +-- 34 files changed, 266 insertions(+), 237 deletions(-) diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java index 107084fdb81..02085333860 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java @@ -275,8 +275,12 @@ public class ComponentDbTester { } public final ComponentDto insertSubportfolio(ComponentDto parentPortfolio) { - ComponentDto subPortfolioComponent = ComponentTesting.newSubPortfolio(parentPortfolio); - return insertComponentAndPortfolio(subPortfolioComponent, true, defaults(), sp -> sp.setParentUuid(sp.getRootUuid())); + return insertSubportfolio(parentPortfolio, defaults()); + } + + public final ComponentDto insertSubportfolio(ComponentDto parentPortfolio, Consumer consumer) { + ComponentDto subPortfolioComponent = ComponentTesting.newSubPortfolio(parentPortfolio).setPrivate(true); + return insertComponentAndPortfolio(subPortfolioComponent, true, consumer, sp -> sp.setParentUuid(sp.getRootUuid())); } public void addPortfolioReference(String portfolioUuid, String... referencerUuids) { @@ -484,6 +488,11 @@ public class ComponentDbTester { return insertSnapshot(snapshotDto); } + public SnapshotDto insertSnapshot(PortfolioDto portfolio) { + SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(portfolio.getUuid()); + return insertSnapshot(snapshotDto); + } + public SnapshotDto insertSnapshot(BranchDto branchDto, Consumer consumer) { SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(branchDto); consumer.accept(snapshotDto); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/notification/NotificationDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/notification/NotificationDbTester.java index f7ca6784397..9f8d04071b8 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/notification/NotificationDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/notification/NotificationDbTester.java @@ -25,7 +25,7 @@ import org.sonar.core.util.stream.MoreCollectors; 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.project.ProjectDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; @@ -42,22 +42,22 @@ public class NotificationDbTester { this.dbSession = db.getSession(); } - public void assertExists(String channel, String dispatcher, String userUuid, @Nullable ComponentDto component) { + public void assertExists(String channel, String dispatcher, String userUuid, @Nullable ProjectDto project) { List result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() - .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) - .setEntityUuid(component == null ? null : component.uuid()) - .setUserUuid(userUuid) - .build(), dbSession).stream() - .filter(prop -> component == null ? prop.getEntityUuid() == null : prop.getEntityUuid() != null) + .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) + .setEntityUuid(project == null ? null : project.getUuid()) + .setUserUuid(userUuid) + .build(), dbSession).stream() + .filter(prop -> project == null ? prop.getEntityUuid() == null : prop.getEntityUuid() != null) .collect(MoreCollectors.toList()); assertThat(result).hasSize(1); assertThat(result.get(0).getValue()).isEqualTo("true"); } - public void assertDoesNotExist(String channel, String dispatcher, String userUuid, @Nullable ComponentDto component) { + public void assertDoesNotExist(String channel, String dispatcher, String userUuid, @Nullable ProjectDto project) { List result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) - .setEntityUuid(component == null ? null : component.uuid()) + .setEntityUuid(project == null ? null : project.getUuid()) .setUserUuid(userUuid) .build(), dbSession); assertThat(result).isEmpty(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java index 132681d52b2..4d08a86d156 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java @@ -300,11 +300,6 @@ public class UserDbTester { return dto; } - public void deleteProjectPermissionFromAnyone(ComponentDto project, String permission) { - db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, null, project.uuid(), project); - db.commit(); - } - public void deleteProjectPermissionFromAnyone(EntityDto project, String permission) { db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, null, project.getUuid(), project); db.commit(); @@ -389,11 +384,6 @@ public class UserDbTester { db.commit(); } - public void deletePermissionFromUser(ComponentDto project, UserDto user, String permission) { - db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user, permission, project); - db.commit(); - } - public void deletePermissionFromUser(EntityDto project, UserDto user, String permission) { db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user, permission, project); db.commit(); @@ -406,10 +396,10 @@ public class UserDbTester { checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission), "%s can't be granted on a public project", permission); EntityDto entityDto; - if (project.qualifier().equals(Qualifiers.VIEW) || project.qualifier().equals(Qualifiers.SUBVIEW)){ + if (project.qualifier().equals(Qualifiers.VIEW) || project.qualifier().equals(Qualifiers.SUBVIEW)) { entityDto = db.getDbClient().portfolioDao().selectByUuid(db.getSession(), project.uuid()) .orElseThrow(); - }else{ + } else { BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.branchUuid()) .orElseThrow(); // I don't know if this check is worth it diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java index 5b47fb11c89..396c5149b99 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java @@ -201,12 +201,12 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe bulk.stop(); } - public static ComponentDoc toDocument(EntityDto component) { + public static ComponentDoc toDocument(EntityDto entity) { return new ComponentDoc() - .setId(component.getUuid()) - .setAuthUuid(component.getAuthUuid()) - .setName(component.getName()) - .setKey(component.getKey()) - .setQualifier(component.getQualifier()); + .setId(entity.getUuid()) + .setAuthUuid(entity.getAuthUuid()) + .setName(entity.getName()) + .setKey(entity.getKey()) + .setQualifier(entity.getQualifier()); } } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/AbstractUserSession.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/AbstractUserSession.java index e34ca857a73..cf79bdbf547 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/AbstractUserSession.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/AbstractUserSession.java @@ -113,7 +113,7 @@ public abstract class AbstractUserSession implements UserSession { } @Override - public final boolean hasChildProjectsPermission(String permission, ProjectDto application) { + public final boolean hasChildProjectsPermission(String permission, EntityDto application) { return hasChildProjectsPermission(permission, application.getUuid()); } @@ -211,7 +211,7 @@ public abstract class AbstractUserSession implements UserSession { } @Override - public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) { + public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) { if (!APP.equals(application.getQualifier()) || hasChildProjectsPermission(projectPermission, application)) { return this; } diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java index f5a3119f673..4be97dd5e96 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java @@ -142,7 +142,7 @@ public class ThreadLocalUserSession implements UserSession { } @Override - public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) { + public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) { get().checkChildProjectsPermission(projectPermission, application); return this; } @@ -190,8 +190,8 @@ public class ThreadLocalUserSession implements UserSession { } @Override - public boolean hasChildProjectsPermission(String permission, ProjectDto project) { - return get().hasChildProjectsPermission(permission, project); + public boolean hasChildProjectsPermission(String permission, EntityDto application) { + return get().hasChildProjectsPermission(permission, application); } @Override diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserSession.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserSession.java index 4e4bdc05b2f..12c5bf460d7 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserSession.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserSession.java @@ -153,7 +153,7 @@ public interface UserSession { boolean hasChildProjectsPermission(String permission, ComponentDto component); - boolean hasChildProjectsPermission(String permission, ProjectDto component); + boolean hasChildProjectsPermission(String permission, EntityDto application); boolean hasPortfolioChildProjectsPermission(String permission, ComponentDto component); @@ -185,7 +185,7 @@ public interface UserSession { UserSession checkComponentPermission(String projectPermission, ComponentDto component); /** - * Ensures that {@link #hasEntityPermission(String, ProjectDto)} is {@code true}, + * Ensures that {@link #hasEntityPermission(String, EntityDto)} is {@code true}, * otherwise throws a {@link org.sonar.server.exceptions.ForbiddenException}. */ UserSession checkEntityPermission(String projectPermission, EntityDto entity); @@ -197,10 +197,10 @@ public interface UserSession { UserSession checkChildProjectsPermission(String projectPermission, ComponentDto project); /** - * Ensures that {@link #hasChildProjectsPermission(String, ProjectDto)} is {@code true} + * Ensures that {@link #hasChildProjectsPermission(String, EntityDto)} is {@code true} * otherwise throws a {@link org.sonar.server.exceptions.ForbiddenException}. */ - UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application); + UserSession checkChildProjectsPermission(String projectPermission, EntityDto application); /** * Ensures that {@link #hasComponentUuidPermission(String, String)} is {@code true}, diff --git a/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java b/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java index 946bf0f5f74..3d4f35ac0f3 100644 --- a/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java +++ b/server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java @@ -180,6 +180,11 @@ public class UserSessionRule implements TestRule, UserSession { return this; } + public UserSessionRule registerPortfolios(PortfolioDto... portfolioDtos) { + ensureAbstractMockUserSession().registerPortfolios(portfolioDtos); + return this; + } + public UserSessionRule registerProjects(ProjectDto... projectDtos) { ensureAbstractMockUserSession().registerProjects(projectDtos); return this; @@ -274,8 +279,8 @@ public class UserSessionRule implements TestRule, UserSession { } @Override - public boolean hasChildProjectsPermission(String permission, ProjectDto component) { - return currentUserSession.hasChildProjectsPermission(permission, component); + public boolean hasChildProjectsPermission(String permission, EntityDto application) { + return currentUserSession.hasChildProjectsPermission(permission, application); } @Override @@ -383,7 +388,7 @@ public class UserSessionRule implements TestRule, UserSession { } @Override - public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) { + public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) { currentUserSession.checkChildProjectsPermission(projectPermission, application); return this; } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/AddActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/AddActionIT.java index 7fcb3ea1688..e2a8ab14bfe 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/AddActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/AddActionIT.java @@ -27,6 +27,7 @@ import org.sonar.api.notifications.NotificationChannel; import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.BadRequestException; @@ -62,18 +63,18 @@ public class AddActionIT { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public DbTester db = DbTester.create(); + public DbTester db = DbTester.create(true); - private DbClient dbClient = db.getDbClient(); + private final DbClient dbClient = db.getDbClient(); - private NotificationChannel emailChannel = new FakeNotificationChannel("EmailChannel"); - private NotificationChannel twitterChannel = new FakeNotificationChannel("TwitterChannel"); + private final NotificationChannel emailChannel = new FakeNotificationChannel("EmailChannel"); + private final NotificationChannel twitterChannel = new FakeNotificationChannel("TwitterChannel"); // default channel, based on class simple name - private NotificationChannel defaultChannel = new FakeNotificationChannel("EmailNotificationChannel"); + private final NotificationChannel defaultChannel = new FakeNotificationChannel("EmailNotificationChannel"); - private Dispatchers dispatchers = mock(Dispatchers.class); + private final Dispatchers dispatchers = mock(Dispatchers.class); - private WsActionTester ws = new WsActionTester(new AddAction(new NotificationCenter( + private final WsActionTester ws = new WsActionTester(new AddAction(new NotificationCenter( new NotificationDispatcherMetadata[] {}, new NotificationChannel[] {emailChannel, twitterChannel, defaultChannel}), new NotificationUpdater(dbClient), dispatchers, dbClient, TestComponentFinder.from(db), userSession)); @@ -104,7 +105,7 @@ public class AddActionIT { public void add_notification_on_private_with_USER_permission() { UserDto user = db.users().insertUser(); userSession.logIn(user); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); userSession.addProjectPermission(USER, project); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); @@ -118,8 +119,8 @@ public class AddActionIT { public void add_notification_on_public_project() { UserDto user = db.users().insertUser(); userSession.logIn(user); - ComponentDto project = db.components().insertPublicProject().getMainBranchComponent(); - userSession.registerComponents(project); + ProjectDto project = db.components().insertPublicProject().getProjectDto(); + userSession.registerProjects(project); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); @@ -134,7 +135,7 @@ public class AddActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); userSession.addProjectPermission(USER, project); call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null); @@ -150,7 +151,7 @@ public class AddActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); call(NOTIF_MY_NEW_ISSUES, null, null, null); userSession.addProjectPermission(USER, project); @@ -166,8 +167,8 @@ public class AddActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPublicProject().getMainBranchComponent(); - userSession.registerComponents(project); + ProjectDto project = db.components().insertPublicProject().getProjectDto(); + userSession.registerProjects(project); call(NOTIF_MY_NEW_ISSUES, null, null, null); call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null); @@ -210,7 +211,7 @@ public class AddActionIT { } @Test - public void fail_if_login_provided_and_not_system_administrsator() { + public void fail_if_login_provided_and_not_system_administrator() { UserDto user = db.users().insertUser(); userSession.logIn(user).setNonSystemAdministrator(); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); @@ -219,6 +220,17 @@ public class AddActionIT { .isInstanceOf(ForbiddenException.class); } + @Test + public void fail_if_project_provided_and_not_project_user() { + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); + UserDto user = db.users().insertUser(); + userSession.logIn(user); + when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); + + assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null)) + .isInstanceOf(ForbiddenException.class); + } + @Test public void fail_when_notification_already_exists() { UserDto user = db.users().insertUser(); @@ -299,8 +311,8 @@ public class AddActionIT { when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, "VIEW_1", null)) - .isInstanceOf(BadRequestException.class) - .hasMessageContaining("Component 'VIEW_1' must be a project"); + .isInstanceOf(NotFoundException.class) + .hasMessageContaining("Project 'VIEW_1' not found"); } @Test diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/ListActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/ListActionIT.java index 49eefadb1c9..b07d27ced1c 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/ListActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/ListActionIT.java @@ -26,7 +26,8 @@ import org.sonar.api.server.ws.WebService; 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.ProjectData; +import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -56,7 +57,7 @@ public class ListActionIT { @Rule public final UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public final DbTester db = DbTester.create(); + public final DbTester db = DbTester.create(true); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); @@ -110,16 +111,16 @@ public class ListActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - db.users().insertProjectPermissionOnUser(user, USER, project); - ComponentDto anotherProject = db.components().insertPrivateProject().getMainBranchComponent(); - notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project); + ProjectData project = db.components().insertPrivateProject(); + db.users().insertProjectPermissionOnUser(user, USER, project.getMainBranchComponent()); + ProjectDto anotherProject = db.components().insertPrivateProject().getProjectDto(); + notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project.getProjectDto()); notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, anotherProject); dbSession.commit(); ListResponse result = call(); - assertThat(result.getNotificationsList()).extracting(Notification::getProject).containsOnly(project.getKey()); + assertThat(result.getNotificationsList()).extracting(Notification::getProject).containsOnly(project.getProjectDto().getKey()); } @Test @@ -155,10 +156,10 @@ public class ListActionIT { UserDto user = db.users().insertUser(); userSession.logIn(user); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); - db.users().insertProjectPermissionOnUser(user, USER, project); - notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project); - notificationUpdater.add(dbSession, emailChannel.getKey(), "Unknown Notification", user, project); + ProjectData project = db.components().insertPrivateProject(); + db.users().insertProjectPermissionOnUser(user, USER, project.getMainBranchComponent()); + notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project.getProjectDto()); + notificationUpdater.add(dbSession, emailChannel.getKey(), "Unknown Notification", user, project.getProjectDto()); dbSession.commit(); ListResponse result = call(); @@ -174,7 +175,7 @@ public class ListActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS)); when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); db.users().insertProjectPermissionOnUser(user, USER, project); notificationUpdater.add(dbSession, twitterChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null); notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null); @@ -241,8 +242,9 @@ public class ListActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS)); when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS)); - ComponentDto project = db.components().insertPrivateProject(p -> p.setKey(KEY_PROJECT_EXAMPLE_001).setName("My Project")).getMainBranchComponent(); - db.users().insertProjectPermissionOnUser(user, USER, project); + ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey(KEY_PROJECT_EXAMPLE_001).setName("My Project")); + ProjectDto project = projectData.getProjectDto(); + db.users().insertProjectPermissionOnUser(user, USER, projectData.getMainBranchComponent()); notificationUpdater.add(dbSession, twitterChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null); notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null); notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_NEW_ISSUES, user, null); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/RemoveActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/RemoveActionIT.java index f135134a2c4..b89a124e92e 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/RemoveActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/RemoveActionIT.java @@ -27,6 +27,7 @@ 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.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.BadRequestException; @@ -61,7 +62,7 @@ public class RemoveActionIT { @Rule public final UserSessionRule userSession = UserSessionRule.standalone(); @Rule - public final DbTester db = DbTester.create(); + public final DbTester db = DbTester.create(true); private final DbClient dbClient = db.getDbClient(); private final DbSession dbSession = db.getSession(); @@ -111,7 +112,7 @@ public class RemoveActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); notificationUpdater.add(dbSession, defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project); dbSession.commit(); @@ -126,7 +127,7 @@ public class RemoveActionIT { userSession.logIn(user); when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES)); - ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent(); + ProjectDto project = db.components().insertPrivateProject().getProjectDto(); notificationUpdater.add(dbSession, defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project); dbSession.commit(); @@ -287,8 +288,8 @@ public class RemoveActionIT { RemoveRequest request = this.request.setProject("VIEW_1"); assertThatThrownBy(() -> call(request)) - .isInstanceOf(BadRequestException.class) - .hasMessage("Component 'VIEW_1' must be a project"); + .isInstanceOf(NotFoundException.class) + .hasMessage("Project 'VIEW_1' not found"); } @Test diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java index 3be91be3c74..472514a8c10 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java @@ -76,12 +76,7 @@ public class ProjectBadgesSupport { String branchName = request.param(PARAM_BRANCH); ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey); - BranchDto branch; - if (branchName == null) { - branch = componentFinder.getMainBranch(dbSession, project); - } else { - branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null); - } + BranchDto branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null); if (!branch.getBranchType().equals(BRANCH)) { throw generateInvalidProjectException(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java index f30975adf1d..be3c1414890 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java @@ -47,6 +47,7 @@ import org.sonar.db.ce.CeTaskQuery; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentQuery; +import org.sonar.db.entity.EntityDto; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Ce; import org.sonarqube.ws.Ce.ActivityResponse; @@ -167,8 +168,8 @@ public class ActivityAction implements CeWsAction { private ActivityResponse doHandle(Request request) { try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto component = loadComponent(dbSession, request); - checkPermission(component); + EntityDto entity = loadEntity(dbSession, request); + checkPermission(entity); // if a task searched by uuid is found all other parameters are ignored Optional taskSearchedById = searchTaskByUuid(dbSession, request); if (taskSearchedById.isPresent()) { @@ -178,7 +179,7 @@ public class ActivityAction implements CeWsAction { Paging.forPageIndex(1).withPageSize(1).andTotal(1), 0); } - CeTaskQuery query = buildQuery(dbSession, request, component); + CeTaskQuery query = buildQuery(dbSession, request, entity); return buildPaginatedResponse(dbSession, query, parseInt(request.getP()), parseInt(request.getPs())); } @@ -230,26 +231,26 @@ public class ActivityAction implements CeWsAction { } @CheckForNull - private ComponentDto loadComponent(DbSession dbSession, Request request) { + private EntityDto loadEntity(DbSession dbSession, Request request) { String componentKey = request.getComponent(); if (componentKey != null) { - Optional foundComponent; - foundComponent = dbClient.componentDao().selectByKey(dbSession, componentKey); + Optional foundComponent; + foundComponent = dbClient.entityDao().selectByKey(dbSession, componentKey); return checkFoundWithOptional(foundComponent, "Component '%s' does not exist", componentKey); } else { return null; } } - private void checkPermission(@Nullable ComponentDto component) { + private void checkPermission(@Nullable EntityDto entity) { // fail fast if not logged in userSession.checkLoggedIn(); - if (component == null) { + if (entity == null) { userSession.checkIsSystemAdministrator(); } else { - userSession.checkComponentPermission(UserRole.ADMIN, component); + userSession.checkEntityPermission(UserRole.ADMIN, entity); } } @@ -268,7 +269,7 @@ public class ActivityAction implements CeWsAction { return activity.map(ceActivityDto -> formatter.formatActivity(dbSession, ceActivityDto, null)); } - private CeTaskQuery buildQuery(DbSession dbSession, Request request, @Nullable ComponentDto component) { + private CeTaskQuery buildQuery(DbSession dbSession, Request request, @Nullable EntityDto entity) { CeTaskQuery query = new CeTaskQuery(); query.setType(request.getType()); query.setOnlyCurrents(parseBoolean(request.getOnlyCurrents())); @@ -283,8 +284,8 @@ public class ActivityAction implements CeWsAction { } String componentQuery = request.getQ(); - if (component != null) { - query.setMainComponentUuid(component.uuid()); + if (entity != null) { + query.setMainComponentUuid(entity.getUuid()); } else if (componentQuery != null) { query.setMainComponentUuids(loadComponents(dbSession, componentQuery).stream() .map(ComponentDto::uuid) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java index d1ee942d2f8..f14be612769 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java @@ -32,6 +32,7 @@ import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; import org.sonar.server.ws.KeyExamples; @@ -83,8 +84,8 @@ public class ActivityStatusAction implements CeWsAction { private ActivityStatusWsResponse doHandle(Request request) { try (DbSession dbSession = dbClient.openSession(false)) { - Optional component = searchComponent(dbSession, request); - String componentUuid = component.map(ComponentDto::uuid).orElse(null); + Optional component = searchComponent(dbSession, request); + String componentUuid = component.map(EntityDto::getUuid).orElse(null); checkPermissions(component.orElse(null)); int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid); int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid); @@ -106,17 +107,17 @@ public class ActivityStatusAction implements CeWsAction { } } - private Optional searchComponent(DbSession dbSession, Request request) { - ComponentDto component = null; + private Optional searchComponent(DbSession dbSession, Request request) { + EntityDto entity = null; if (request.getComponentKey() != null) { - component = componentFinder.getByKey(dbSession, request.getComponentKey()); + entity = componentFinder.getEntityByKey(dbSession, request.getComponentKey()); } - return Optional.ofNullable(component); + return Optional.ofNullable(entity); } - private void checkPermissions(@Nullable ComponentDto component) { - if (component != null) { - userSession.checkComponentPermission(UserRole.ADMIN, component); + private void checkPermissions(@Nullable EntityDto entity) { + if (entity != null) { + userSession.checkEntityPermission(UserRole.ADMIN, entity); } else { userSession.checkIsSystemAdministrator(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CancelAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CancelAction.java index a30f0057069..697d7b67efc 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CancelAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CancelAction.java @@ -19,10 +19,7 @@ */ package org.sonar.server.ce.ws; -import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; - import java.util.Optional; - import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -35,12 +32,14 @@ import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; import org.sonar.server.user.UserSession; +import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; + public class CancelAction implements CeWsAction { public static final String PARAM_TASK_ID = "id"; private final UserSession userSession; - private DbClient dbClient; + private final DbClient dbClient; private final CeQueue queue; public CancelAction(UserSession userSession, DbClient dbClient, CeQueue queue) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java index 2d3cc1b1000..44190a17471 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java @@ -32,6 +32,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.project.ProjectDto; import org.sonar.server.exceptions.NotFoundException; @@ -45,6 +46,7 @@ public class ComponentFinder { private static final String LABEL_PROJECT = "Project"; private static final String LABEL_COMPONENT = "Component"; private static final String LABEL_PROJECT_NOT_FOUND = "Project '%s' not found"; + private static final String LABEL_ENTITY_NOT_FOUND = "Component '%s' not found"; private final DbClient dbClient; private final ResourceTypes resourceTypes; @@ -64,6 +66,25 @@ public class ComponentFinder { return getByKey(dbSession, checkParamNotEmpty(componentKey, parameterNames.getKeyParam())); } + public EntityDto getEntityByUuidOrKey(DbSession dbSession, @Nullable String entityUuid, @Nullable String entityKey, ParamNames parameterNames) { + checkByUuidOrKey(entityUuid, entityKey, parameterNames); + + if (entityUuid != null) { + return getEntityByUuid(dbSession, checkParamNotEmpty(entityUuid, parameterNames.getUuidParam())); + } + return getEntityByKey(dbSession, checkParamNotEmpty(entityKey, parameterNames.getKeyParam())); + } + + public EntityDto getEntityByKey(DbSession dbSession, String entityKey) { + return dbClient.entityDao().selectByKey(dbSession, entityKey) + .orElseThrow(() -> new NotFoundException(String.format(LABEL_ENTITY_NOT_FOUND, entityKey))); + } + + public EntityDto getEntityByUuid(DbSession dbSession, String entityUuid) { + return dbClient.entityDao().selectByUuid(dbSession, entityUuid) + .orElseThrow(() -> new NotFoundException(String.format(LABEL_ENTITY_NOT_FOUND, entityUuid))); + } + public ProjectDto getProjectByKey(DbSession dbSession, String projectKey) { return dbClient.projectDao().selectProjectByKey(dbSession, projectKey) .orElseThrow(() -> new NotFoundException(String.format(LABEL_PROJECT_NOT_FOUND, projectKey))); @@ -105,12 +126,8 @@ public class ComponentFinder { .orElseThrow(() -> new NotFoundException(String.format("Branch uuid '%s' not found", branchUuid))); } - public BranchDto getBranchOrPullRequest(DbSession dbSession, ComponentDto project, @Nullable String branchKey, @Nullable String pullRequestKey) { - return getBranchOrPullRequest(dbSession, project.uuid(), project.getKey(), branchKey, pullRequestKey); - } - - public BranchDto getBranchOrPullRequest(DbSession dbSession, ProjectDto project, @Nullable String branchKey, @Nullable String pullRequestKey) { - return getBranchOrPullRequest(dbSession, project.getUuid(), project.getKey(), branchKey, pullRequestKey); + public BranchDto getBranchOrPullRequest(DbSession dbSession, EntityDto entity, @Nullable String branchKey, @Nullable String pullRequestKey) { + return getBranchOrPullRequest(dbSession, entity.getUuid(), entity.getKey(), branchKey, pullRequestKey); } public ProjectAndBranch getAppOrProjectAndBranch(DbSession dbSession, String projectKey, @Nullable String branchKey, @Nullable String pullRequestKey) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/favorite/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/favorite/ws/SearchAction.java index 39db8a79ccc..8344bba9755 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/favorite/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/favorite/ws/SearchAction.java @@ -124,12 +124,12 @@ public class SearchAction implements FavoritesWsAction { .forEach(builder::addFavorites); } - private static Favorite toWsFavorite(Favorite.Builder builder, EntityDto componentDto) { + private static Favorite toWsFavorite(Favorite.Builder builder, EntityDto entity) { builder .clear() - .setKey(componentDto.getKey()); - ofNullable(componentDto.getName()).ifPresent(builder::setName); - ofNullable(componentDto.getQualifier()).ifPresent(builder::setQualifier); + .setKey(entity.getKey()); + ofNullable(entity.getName()).ifPresent(builder::setName); + ofNullable(entity.getQualifier()).ifPresent(builder::setQualifier); return builder.build(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AddCommentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AddCommentAction.java index 6277973a6dc..1db5aba3ef1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AddCommentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AddCommentAction.java @@ -80,7 +80,7 @@ public class AddCommentAction implements HotspotsWsAction { String comment = request.mandatoryParam(PARAM_COMMENT); try (DbSession dbSession = dbClient.openSession(false)) { IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey); - hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.USER); + hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.USER); DefaultIssue defaultIssue = hotspot.toDefaultIssue(); IssueChangeContext context = hotspotWsSupport.newIssueChangeContextWithoutMeasureRefresh(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AssignAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AssignAction.java index 7c7438b8b19..b7547af68ae 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AssignAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AssignAction.java @@ -110,7 +110,7 @@ public class AssignAction implements HotspotsWsAction { IssueDto hotspotDto = hotspotWsSupport.loadHotspot(dbSession, hotspotKey); checkHotspotStatusAndResolution(hotspotDto); - hotspotWsSupport.loadAndCheckProject(dbSession, hotspotDto, UserRole.USER); + hotspotWsSupport.loadAndCheckBranch(dbSession, hotspotDto, UserRole.USER); UserDto assignee = isNullOrEmpty(login) ? null : getAssignee(dbSession, login); IssueChangeContext context = hotspotWsSupport.newIssueChangeContextWithoutMeasureRefresh(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ChangeStatusAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ChangeStatusAction.java index 37f23646e29..7111c7ab1cb 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ChangeStatusAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ChangeStatusAction.java @@ -111,7 +111,7 @@ public class ChangeStatusAction implements HotspotsWsAction { String newResolution = resolutionParam(request, newStatus); try (DbSession dbSession = dbClient.openSession(false)) { IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey); - hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.SECURITYHOTSPOT_ADMIN); + hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.SECURITYHOTSPOT_ADMIN); if (needStatusUpdate(hotspot, newStatus, newResolution)) { String transitionKey = toTransitionKey(newStatus, newResolution); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/DeleteCommentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/DeleteCommentAction.java index 7be6e76825b..daba316cf6f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/DeleteCommentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/DeleteCommentAction.java @@ -83,7 +83,7 @@ public class DeleteCommentAction implements HotspotsWsAction { } private void validate(DbSession dbSession, IssueChangeDto issueChangeDto) { - hotspotWsSupport.loadAndCheckProject(dbSession, issueChangeDto.getIssueKey()); + hotspotWsSupport.loadAndCheckBranch(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments"); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/EditCommentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/EditCommentAction.java index 47ada036db1..01eaffe2c2c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/EditCommentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/EditCommentAction.java @@ -102,7 +102,7 @@ public class EditCommentAction implements HotspotsWsAction { } private void validate(DbSession dbSession, IssueChangeDto issueChangeDto) { - hotspotWsSupport.loadAndCheckProject(dbSession, issueChangeDto.getIssueKey()); + hotspotWsSupport.loadAndCheckBranch(dbSession, issueChangeDto.getIssueKey()); checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments"); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsSupport.java index f33bdbfbb54..dfc7c1ce6b9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsSupport.java @@ -27,8 +27,10 @@ import org.sonar.api.web.UserRole; import org.sonar.core.issue.IssueChangeContext; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; @@ -51,9 +53,9 @@ public class HotspotWsSupport { return userSession.checkLoggedIn().getUuid(); } - ComponentDto loadAndCheckProject(DbSession dbSession, String hotspotKey) { + BranchDto loadAndCheckBranch(DbSession dbSession, String hotspotKey) { IssueDto hotspot = loadHotspot(dbSession, hotspotKey); - return loadAndCheckProject(dbSession, hotspot, UserRole.USER); + return loadAndCheckBranch(dbSession, hotspot, UserRole.USER); } IssueDto loadHotspot(DbSession dbSession, String hotspotKey) { @@ -63,15 +65,17 @@ public class HotspotWsSupport { .orElseThrow(() -> new NotFoundException(format("Hotspot '%s' does not exist", hotspotKey))); } - ComponentDto loadAndCheckProject(DbSession dbSession, IssueDto hotspot, String userRole) { - String projectUuid = hotspot.getProjectUuid(); - checkArgument(projectUuid != null, "Hotspot '%s' has no project", hotspot.getKee()); + BranchDto loadAndCheckBranch(DbSession dbSession, IssueDto hotspot, String userRole) { + String branchUuid = hotspot.getProjectUuid(); + checkArgument(branchUuid != null, "Hotspot '%s' has no branch", hotspot.getKee()); - ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, projectUuid) - .orElseThrow(() -> new NotFoundException(format("Project with uuid '%s' does not exist", projectUuid))); - userSession.checkComponentPermission(userRole, project); + BranchDto branch = dbClient.branchDao().selectByUuid(dbSession, branchUuid) + .orElseThrow(() -> new NotFoundException(format("Branch with uuid '%s' does not exist", branchUuid))); + ProjectDto project = dbClient.projectDao().selectByUuid(dbSession, branch.getProjectUuid()) + .orElseThrow(() -> new NotFoundException(format("Project with uuid '%s' does not exist", branch.getProjectUuid()))); - return project; + userSession.checkEntityPermission(userRole, project); + return branch; } boolean canChangeStatus(ComponentDto project) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java index f5c82402406..5af876ed042 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java @@ -177,9 +177,9 @@ public class ShowAction implements HotspotsWsAction { private void formatComponents(Components components, ShowWsResponse.Builder responseBuilder) { responseBuilder - .setProject(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getProject(), components.getBranch(), components.getPullRequest())) + .setProject(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getBranchComponent(), components.getBranch(), components.getPullRequest())) .setComponent(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getComponent(), components.getBranch(), components.getPullRequest())); - responseBuilder.setCanChangeStatus(hotspotWsSupport.canChangeStatus(components.getProject())); + responseBuilder.setCanChangeStatus(hotspotWsSupport.canChangeStatus(components.getBranchComponent())); } private static void formatRule(ShowWsResponse.Builder responseBuilder, RuleDto ruleDto) { @@ -266,7 +266,7 @@ public class ShowAction implements HotspotsWsAction { .filter(Optional::isPresent) .map(Optional::get) .collect(toSet()); - Set preloadedComponents = ImmutableSet.of(components.project, components.component); + Set preloadedComponents = ImmutableSet.of(components.branchComponent, components.component); FormattingContext formattingContext = issueChangeSupport .newFormattingContext(dbSession, singleton(hotspot), Load.ALL, preloadedUsers, preloadedComponents); @@ -298,27 +298,26 @@ public class ShowAction implements HotspotsWsAction { private Components loadComponents(DbSession dbSession, IssueDto hotspot) { String componentUuid = hotspot.getComponentUuid(); - - ComponentDto project = hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.USER); - BranchDto branch = dbClient.branchDao().selectByUuid(dbSession, project.branchUuid()).orElseThrow(() -> new IllegalStateException("Can't find branch " + project.branchUuid())); - checkArgument(componentUuid != null, "Hotspot '%s' has no component", hotspot.getKee()); - boolean hotspotOnProject = Objects.equals(project.uuid(), componentUuid); - ComponentDto component = hotspotOnProject ? project - : dbClient.componentDao().selectByUuid(dbSession, componentUuid) + + BranchDto branch = hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.USER); + ComponentDto component = dbClient.componentDao().selectByUuid(dbSession, componentUuid) .orElseThrow(() -> new NotFoundException(format("Component with uuid '%s' does not exist", componentUuid))); + boolean hotspotOnBranch = Objects.equals(branch.getUuid(), componentUuid); + ComponentDto branchComponent = hotspotOnBranch ? component : dbClient.componentDao().selectByUuid(dbSession, branch.getUuid()) + .orElseThrow(() -> new NotFoundException(format("Component with uuid '%s' does not exist", componentUuid))); - return new Components(project, component, branch); + return new Components(branchComponent, component, branch); } private static final class Components { - private final ComponentDto project; + private final ComponentDto branchComponent; private final ComponentDto component; private final String branch; private final String pullRequest; - private Components(ComponentDto project, ComponentDto component, BranchDto branch) { - this.project = project; + private Components(ComponentDto branchComponent, ComponentDto component, BranchDto branch) { + this.branchComponent = branchComponent; this.component = component; if (branch.isMain()) { this.branch = null; @@ -342,8 +341,8 @@ public class ShowAction implements HotspotsWsAction { return pullRequest; } - public ComponentDto getProject() { - return project; + public ComponentDto getBranchComponent() { + return branchComponent; } public ComponentDto getComponent() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/AddAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/AddAction.java index 66ebe4bd565..a67178a38d9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/AddAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/AddAction.java @@ -22,15 +22,13 @@ package org.sonar.server.notification.ws; import java.util.List; import java.util.Optional; import org.sonar.api.notifications.NotificationChannel; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Scopes; 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.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.issue.notification.MyNewIssuesNotificationHandler; @@ -40,13 +38,13 @@ import org.sonar.server.ws.KeyExamples; import static java.util.Optional.empty; import static java.util.Optional.ofNullable; +import static org.sonar.server.exceptions.BadRequestException.checkRequest; +import static org.sonar.server.exceptions.NotFoundException.checkFound; import static org.sonar.server.notification.ws.NotificationsWsParameters.ACTION_ADD; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_CHANNEL; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_LOGIN; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_PROJECT; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_TYPE; -import static org.sonar.server.exceptions.NotFoundException.checkFound; -import static org.sonar.server.exceptions.BadRequestException.checkRequest; public class AddAction implements NotificationsWsAction { private final NotificationCenter notificationCenter; @@ -91,10 +89,10 @@ public class AddAction implements NotificationsWsAction { action.createParam(PARAM_TYPE) .setDescription("Notification type. Possible values are for:" + - "
    " + - "
  • Global notifications: %s
  • " + - "
  • Per project notifications: %s
  • " + - "
", + "
    " + + "
  • Global notifications: %s
  • " + + "
  • Per project notifications: %s
  • " + + "
", String.join(", ", dispatchers.getGlobalDispatchers()), String.join(", ", dispatchers.getProjectDispatchers())) .setRequired(true) @@ -117,7 +115,7 @@ public class AddAction implements NotificationsWsAction { try (DbSession dbSession = dbClient.openSession(false)) { checkPermissions(request); UserDto user = getUser(dbSession, request); - Optional project = searchProject(dbSession, request); + Optional project = searchProject(dbSession, request); notificationUpdater.add(dbSession, request.getChannel(), request.getType(), user, project.orElse(null)); dbSession.commit(); } @@ -128,11 +126,9 @@ public class AddAction implements NotificationsWsAction { return checkFound(dbClient.userDao().selectByLogin(dbSession, login), "User '%s' not found", login); } - private Optional searchProject(DbSession dbSession, AddRequest request) { - Optional project = request.getProject() == null ? empty() : Optional.of(componentFinder.getByKey(dbSession, request.getProject())); - project.ifPresent(p -> checkRequest(Qualifiers.PROJECT.equals(p.qualifier()) && Scopes.PROJECT.equals(p.scope()), - "Component '%s' must be a project", request.getProject())); - project.ifPresent(p -> userSession.checkComponentPermission(UserRole.USER, p)); + private Optional searchProject(DbSession dbSession, AddRequest request) { + Optional project = request.getProject() == null ? empty() : Optional.of(componentFinder.getProjectByKey(dbSession, request.getProject())); + project.ifPresent(p -> userSession.checkEntityPermission(UserRole.USER, p)); return project; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/ListAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/ListAction.java index 983e91e7b7f..4e3c468c358 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/ListAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/ListAction.java @@ -37,6 +37,7 @@ import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.db.user.UserDto; @@ -120,10 +121,10 @@ public class ListAction implements NotificationsWsAction { private UnaryOperator addNotifications(DbSession dbSession, UserDto user) { return response -> { List properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserUuid(user.getUuid()).build(), dbSession); - Map componentsByUuid = searchProjects(dbSession, properties); + Map entitiesByUuid = searchProjects(dbSession, properties); Predicate isNotification = prop -> prop.getKey().startsWith("notification."); - Predicate isComponentInDb = prop -> prop.getEntityUuid() == null || componentsByUuid.containsKey(prop.getEntityUuid()); + Predicate isComponentInDb = prop -> prop.getEntityUuid() == null || entitiesByUuid.containsKey(prop.getEntityUuid()); Notification.Builder notification = Notification.newBuilder(); @@ -131,7 +132,7 @@ public class ListAction implements NotificationsWsAction { .filter(isNotification) .filter(channelAndDispatcherAuthorized()) .filter(isComponentInDb) - .map(toWsNotification(notification, componentsByUuid)) + .map(toWsNotification(notification, entitiesByUuid)) .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder())) .thenComparing(Notification::getChannel) .thenComparing(Notification::getType)) @@ -154,19 +155,19 @@ public class ListAction implements NotificationsWsAction { return (prop.getEntityUuid() != null && dispatchers.getProjectDispatchers().contains(dispatcher)) || dispatchers.getGlobalDispatchers().contains(dispatcher); } - private Map searchProjects(DbSession dbSession, List properties) { - Set componentUuids = properties.stream() + private Map searchProjects(DbSession dbSession, List properties) { + Set entityUuids = properties.stream() .map(PropertyDto::getEntityUuid) .filter(Objects::nonNull) .collect(MoreCollectors.toSet(properties.size())); - Set authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedEntityUuids(dbSession, componentUuids, userSession.getUuid(), UserRole.USER); - return dbClient.componentDao().selectByUuids(dbSession, componentUuids) + Set authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedEntityUuids(dbSession, entityUuids, userSession.getUuid(), UserRole.USER); + return dbClient.entityDao().selectByUuids(dbSession, entityUuids) .stream() - .filter(c -> authorizedProjectUuids.contains(c.uuid())) - .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); + .filter(c -> authorizedProjectUuids.contains(c.getUuid())) + .collect(MoreCollectors.uniqueIndex(EntityDto::getUuid)); } - private static Function toWsNotification(Notification.Builder notification, Map projectsByUuid) { + private static Function toWsNotification(Notification.Builder notification, Map projectsByUuid) { return property -> { notification.clear(); List propertyKey = Splitter.on(".").splitToList(property.getKey()); @@ -178,12 +179,11 @@ public class ListAction implements NotificationsWsAction { }; } - private static void populateProjectFields(Builder notification, String componentUuid, - Map projectsByUuid) { - ComponentDto project = projectsByUuid.get(componentUuid); + private static void populateProjectFields(Builder notification, String componentUuid, Map projectsByUuid) { + EntityDto project = projectsByUuid.get(componentUuid); notification .setProject(project.getKey()) - .setProjectName(project.name()); + .setProjectName(project.getName()); } private void checkPermissions(Request request) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationUpdater.java index 9256dd903cd..5b08dd4e292 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationUpdater.java @@ -25,7 +25,7 @@ import javax.annotation.Nullable; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.entity.EntityDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.db.user.UserDto; @@ -45,50 +45,50 @@ public class NotificationUpdater { /** * Add a notification to a user. */ - public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { + public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable EntityDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); - String projectUuid = project == null ? null : project.uuid(); + String projectUuid = project == null ? null : project.getUuid(); String projectKey = project == null ? null : project.getKey(); - String projectName = project == null ? null : project.name(); - String qualifier = project == null ? null : project.qualifier(); + String projectName = project == null ? null : project.getName(); + String qualifier = project == null ? null : project.getQualifier(); List existingNotification = dbClient.propertiesDao().selectByQuery( - PropertyQuery.builder() - .setKey(key) - .setEntityUuid(projectUuid) - .setUserUuid(user.getUuid()) - .build(), - dbSession).stream() + PropertyQuery.builder() + .setKey(key) + .setEntityUuid(projectUuid) + .setUserUuid(user.getUuid()) + .build(), + dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(existingNotification.isEmpty() || !PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification already added"); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() - .setKey(key) - .setUserUuid(user.getUuid()) - .setValue(PROP_NOTIFICATION_VALUE) - .setEntityUuid(projectUuid), + .setKey(key) + .setUserUuid(user.getUuid()) + .setValue(PROP_NOTIFICATION_VALUE) + .setEntityUuid(projectUuid), user.getLogin(), projectKey, projectName, qualifier); } /** * Remove a notification from a user. */ - public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { + public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable EntityDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); - String projectUuid = project == null ? null : project.uuid(); + String projectUuid = project == null ? null : project.getUuid(); String projectKey = project == null ? null : project.getKey(); - String projectName = project == null ? null : project.name(); - String qualifier = project == null ? null : project.qualifier(); + String projectName = project == null ? null : project.getName(); + String qualifier = project == null ? null : project.getQualifier(); List existingNotification = dbClient.propertiesDao().selectByQuery( - PropertyQuery.builder() - .setKey(key) - .setEntityUuid(projectUuid) - .setUserUuid(user.getUuid()) - .build(), - dbSession).stream() + PropertyQuery.builder() + .setKey(key) + .setEntityUuid(projectUuid) + .setUserUuid(user.getUuid()) + .build(), + dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(!existingNotification.isEmpty() && PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification doesn't exist"); @@ -100,7 +100,7 @@ public class NotificationUpdater { .setEntityUuid(projectUuid), user.getLogin(), projectKey, projectName, qualifier); } - private static Predicate notificationScope(@Nullable ComponentDto project) { + private static Predicate notificationScope(@Nullable EntityDto project) { return prop -> project == null ? (prop.getEntityUuid() == null) : (prop.getEntityUuid() != null); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/RemoveAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/RemoveAction.java index f1dcdf22050..dd9ee79cf83 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/RemoveAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/RemoveAction.java @@ -23,14 +23,12 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import org.sonar.api.notifications.NotificationChannel; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Scopes; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.issue.notification.MyNewIssuesNotificationHandler; @@ -40,13 +38,13 @@ import org.sonar.server.ws.KeyExamples; import static java.util.Optional.empty; import static java.util.Optional.ofNullable; +import static org.sonar.server.exceptions.BadRequestException.checkRequest; +import static org.sonar.server.exceptions.NotFoundException.checkFound; import static org.sonar.server.notification.ws.NotificationsWsParameters.ACTION_REMOVE; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_CHANNEL; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_LOGIN; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_PROJECT; import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_TYPE; -import static org.sonar.server.exceptions.NotFoundException.checkFound; -import static org.sonar.server.exceptions.BadRequestException.checkRequest; public class RemoveAction implements NotificationsWsAction { private final NotificationCenter notificationCenter; @@ -91,10 +89,10 @@ public class RemoveAction implements NotificationsWsAction { action.createParam(PARAM_TYPE) .setDescription("Notification type. Possible values are for:" + - "
    " + - "
  • Global notifications: %s
  • " + - "
  • Per project notifications: %s
  • " + - "
", + "
    " + + "
  • Global notifications: %s
  • " + + "
  • Per project notifications: %s
  • " + + "
", dispatchers.getGlobalDispatchers().stream().sorted().collect(Collectors.joining(", ")), dispatchers.getProjectDispatchers().stream().sorted().collect(Collectors.joining(", "))) .setRequired(true) @@ -117,7 +115,7 @@ public class RemoveAction implements NotificationsWsAction { try (DbSession dbSession = dbClient.openSession(false)) { checkPermissions(request); UserDto user = getUser(dbSession, request); - Optional project = searchProject(dbSession, request); + Optional project = searchProject(dbSession, request); notificationUpdater.remove(dbSession, request.getChannel(), request.getType(), user, project.orElse(null)); dbSession.commit(); } @@ -128,11 +126,8 @@ public class RemoveAction implements NotificationsWsAction { return checkFound(dbClient.userDao().selectByLogin(dbSession, login), "User '%s' not found", login); } - private Optional searchProject(DbSession dbSession, RemoveRequest request) { - Optional project = request.getProject() == null ? empty() : Optional.of(componentFinder.getByKey(dbSession, request.getProject())); - project.ifPresent(p -> checkRequest(Qualifiers.PROJECT.equals(p.qualifier()) && Scopes.PROJECT.equals(p.scope()), - "Component '%s' must be a project", request.getProject())); - return project; + private Optional searchProject(DbSession dbSession, RemoveRequest request) { + return request.getProject() == null ? empty() : Optional.of(componentFinder.getProjectByKey(dbSession, request.getProject())); } private void checkPermissions(RemoveRequest request) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java index 963e6734aeb..c3fb9db0d67 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java @@ -81,7 +81,7 @@ public class PermissionWsSupport { return Optional.empty(); } - public ComponentDto getRootComponentOrModule(DbSession dbSession, ProjectWsRef projectRef) { + public ComponentDto getRootComponent(DbSession dbSession, ProjectWsRef projectRef) { return componentFinder.getRootComponentByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key()); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java index 284ce6cbbeb..bf30894174b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java @@ -92,7 +92,7 @@ public class ApplyTemplateAction implements PermissionsWsAction { PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef( request.getTemplateId(), request.getTemplateName())); - ComponentDto project = wsSupport.getRootComponentOrModule(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey())); + ComponentDto project = wsSupport.getRootComponent(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey())); checkGlobalAdmin(userSession); permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(project)); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/ProjectDumpWsSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/ProjectDumpWsSupport.java index e8a180e417d..bf9f67a1aaf 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/ProjectDumpWsSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/ProjectDumpWsSupport.java @@ -23,7 +23,7 @@ import org.sonar.api.server.ServerSide; 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.db.project.ProjectDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; @@ -42,8 +42,8 @@ public class ProjectDumpWsSupport { public void verifyAdminOfProjectByKey(String projectKey) { try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto project = componentFinder.getByKey(dbSession, projectKey); - userSession.checkComponentPermission(UserRole.ADMIN, project); + ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); + userSession.checkEntityPermission(UserRole.ADMIN, project); } } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java index b704ddeb92c..505e468c0aa 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java @@ -32,7 +32,9 @@ import org.sonar.api.web.UserRole; import org.sonar.db.DbClient; import org.sonar.db.DbInputStream; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; +import org.sonar.db.project.ProjectDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.scannercache.ScannerCache; @@ -85,10 +87,11 @@ public class GetAction implements AnalysisCacheWsAction { String branchKey = request.param(BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto component = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, projectKey, branchKey, null); - checkPermission(component); + ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); + checkPermission(project); + BranchDto branchDto = componentFinder.getBranchOrPullRequest(dbSession, project, branchKey, null); - try (DbInputStream dbInputStream = cache.get(component.uuid())) { + try (DbInputStream dbInputStream = cache.get(branchDto.getUuid())) { if (dbInputStream == null) { throw new NotFoundException("No cache for given branch or pull request"); } @@ -117,9 +120,9 @@ public class GetAction implements AnalysisCacheWsAction { .orElse(false); } - private void checkPermission(ComponentDto project) { - if (userSession.hasComponentPermission(UserRole.SCAN, project) || - userSession.hasComponentPermission(UserRole.ADMIN, project) || + private void checkPermission(ProjectDto project) { + if (userSession.hasEntityPermission(UserRole.SCAN, project) || + userSession.hasEntityPermission(UserRole.ADMIN, project) || userSession.hasPermission(SCAN)) { return; } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java index f1f5e18f090..bf30d61bd81 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java @@ -88,14 +88,14 @@ public class SettingValidations { } private static boolean checkComponentQualifier(SettingData data, @Nullable PropertyDefinition definition) { - EntityDto component = data.entity; - if (component == null) { + EntityDto entity = data.entity; + if (entity == null) { return true; } if (definition == null) { - return SUPPORTED_QUALIFIERS.contains(component.getQualifier()); + return SUPPORTED_QUALIFIERS.contains(entity.getQualifier()); } - return definition.qualifiers().contains(component.getQualifier()); + return definition.qualifiers().contains(entity.getQualifier()); } public Consumer valueType() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java index 320e0bf4110..90c6735b840 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java @@ -144,15 +144,15 @@ public class ValuesAction implements SettingsWsAction { return Optional.empty(); } - EntityDto component = dbClient.entityDao().selectByKey(dbSession, componentKey) + EntityDto entity = dbClient.entityDao().selectByKey(dbSession, componentKey) .orElseThrow(() -> new NotFoundException(format("Component key '%s' not found", componentKey))); - if (!userSession.hasEntityPermission(USER, component) && - !userSession.hasEntityPermission(UserRole.SCAN, component) && + if (!userSession.hasEntityPermission(USER, entity) && + !userSession.hasEntityPermission(UserRole.SCAN, entity) && !userSession.hasPermission(GlobalPermission.SCAN)) { throw insufficientPrivilegesException(); } - return Optional.of(component); + return Optional.of(entity); } private List loadSettings(DbSession dbSession, Optional component, Set keys) { -- 2.39.5