From a2d415346a2943db647933ce67b4d9d243267d40 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Mon, 16 Aug 2021 13:35:14 -0500 Subject: [PATCH] SONAR-15142 When adding a project/application/portfolio, some permissions are deleted --- .../server/component/ComponentUpdater.java | 2 +- .../permission/PermissionTemplateService.java | 12 ++++---- .../component/ComponentUpdaterTest.java | 2 +- .../PermissionTemplateServiceTest.java | 30 +++++++++---------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java index 14a7b65c791..ef052363271 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -176,7 +176,7 @@ public class ComponentUpdater { } private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable String userUuid, @Nullable String userLogin) { - permissionTemplateService.applyDefault(dbSession, componentDto, userUuid); + permissionTemplateService.applyDefaultToNewComponent(dbSession, componentDto, userUuid); if (componentDto.qualifier().equals(PROJECT) && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, componentDto)) { favoriteUpdater.add(dbSession, componentDto, userUuid, userLogin, false); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java index 40ddcbcfe51..58b682e4c02 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/PermissionTemplateService.java @@ -99,17 +99,18 @@ public class PermissionTemplateService { } for (ComponentDto project : projects) { + dbClient.groupPermissionDao().deleteByRootComponentUuid(dbSession, project); + dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project); copyPermissions(dbSession, template, project, null); } projectIndexers.commitAndIndexComponents(dbSession, projects, ProjectIndexer.Cause.PERMISSION_CHANGE); } /** - * Apply the default permission template to project. The project can already exist (so it has permissions) or - * can be provisioned (so has no permissions yet). - * @param projectCreatorUserId id of the user who creates the project, only if project is provisioned. He will + * Apply the default permission template to a new project (has no permissions yet). + * @param projectCreatorUserId id of the user creating the project. */ - public void applyDefault(DbSession dbSession, ComponentDto component, @Nullable String projectCreatorUserId) { + public void applyDefaultToNewComponent(DbSession dbSession, ComponentDto component, @Nullable String projectCreatorUserId) { PermissionTemplateDto template = findTemplate(dbSession, component); checkArgument(template != null, "Cannot retrieve default permission template"); copyPermissions(dbSession, template, component, projectCreatorUserId); @@ -126,9 +127,6 @@ public class PermissionTemplateService { } private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable String projectCreatorUserUuid) { - dbClient.groupPermissionDao().deleteByRootComponentUuid(dbSession, project); - dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project); - List usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid()); Map userDtoMap = dbClient.userDao().selectByUuids(dbSession, usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet())) .stream().collect(Collectors.toMap(UserDto::getUuid, UserDto::getUuid)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java index c099512b972..032821b2cd4 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java @@ -180,7 +180,7 @@ public class ComponentUpdaterTest { .build(); ComponentDto dto = underTest.create(db.getSession(), project, userUuid, "user-login"); - verify(permissionTemplateService).applyDefault(db.getSession(), dto, userUuid); + verify(permissionTemplateService).applyDefaultToNewComponent(db.getSession(), dto, userUuid); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java index 155ebd88290..aa55361ae54 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java @@ -83,7 +83,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, privateProject, creator.getUuid()); + underTest.applyDefaultToNewComponent(session, privateProject, creator.getUuid()); assertThat(selectProjectPermissionsOfGroup(null, privateProject)).isEmpty(); } @@ -111,7 +111,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addAnyoneToTemplate(permissionTemplate, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, publicProject, null); + underTest.applyDefaultToNewComponent(session, publicProject, null); assertThat(selectProjectPermissionsOfGroup(null, publicProject)) .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -142,7 +142,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, privateProject, null); + underTest.applyDefaultToNewComponent(session, privateProject, null); assertThat(selectProjectPermissionsOfGroup(group, privateProject)) .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -173,7 +173,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, publicProject, null); + underTest.applyDefaultToNewComponent(session, publicProject, null); assertThat(selectProjectPermissionsOfGroup(group, publicProject)) .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -204,7 +204,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, publicProject, null); + underTest.applyDefaultToNewComponent(session, publicProject, null); assertThat(selectProjectPermissionsOfUser(user, publicProject)) .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -235,7 +235,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addUserToTemplate(permissionTemplate, user, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, privateProject, null); + underTest.applyDefaultToNewComponent(session, privateProject, null); assertThat(selectProjectPermissionsOfUser(user, privateProject)) .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -251,7 +251,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, publicProject, user.getUuid()); + underTest.applyDefaultToNewComponent(session, publicProject, user.getUuid()); assertThat(selectProjectPermissionsOfUser(user, publicProject)) .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -267,7 +267,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1"); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, privateProject, user.getUuid()); + underTest.applyDefaultToNewComponent(session, privateProject, user.getUuid()); assertThat(selectProjectPermissionsOfUser(user, privateProject)) .containsOnly("p1", UserRole.CODEVIEWER, UserRole.USER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, UserRole.SECURITYHOTSPOT_ADMIN, SCAN.getKey()); @@ -282,7 +282,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, portfolio, null); + underTest.applyDefaultToNewComponent(session, portfolio, null); assertThat(selectProjectPermissionsOfGroup(group, portfolio)) .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey()); @@ -298,7 +298,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, appPermissionTemplate, null); - underTest.applyDefault(session, view, null); + underTest.applyDefaultToNewComponent(session, view, null); assertThat(selectProjectPermissionsOfGroup(group, view)) .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey()); @@ -314,7 +314,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(portPermissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, portPermissionTemplate); - underTest.applyDefault(session, view, null); + underTest.applyDefaultToNewComponent(session, view, null); assertThat(selectProjectPermissionsOfGroup(group, view)) .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey()); @@ -328,7 +328,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, null); - underTest.applyDefault(session, view, null); + underTest.applyDefaultToNewComponent(session, view, null); assertThat(selectProjectPermissionsOfGroup(group, view)).containsOnly(PROVISION_PROJECTS.getKey()); } @@ -342,7 +342,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(permissionTemplate, null, null); - underTest.applyDefault(session, application, null); + underTest.applyDefaultToNewComponent(session, application, null); assertThat(selectProjectPermissionsOfGroup(group, application)) .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey()); @@ -359,7 +359,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(appPermissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, appPermissionTemplate, portPermissionTemplate); - underTest.applyDefault(session, application, null); + underTest.applyDefaultToNewComponent(session, application, null); assertThat(selectProjectPermissionsOfGroup(group, application)) .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey()); @@ -373,7 +373,7 @@ public class PermissionTemplateServiceTest { dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey()); dbTester.permissionTemplates().setDefaultTemplates(projectPermissionTemplate, null, null); - underTest.applyDefault(session, application, null); + underTest.applyDefaultToNewComponent(session, application, null); assertThat(selectProjectPermissionsOfGroup(group, application)).containsOnly(PROVISION_PROJECTS.getKey()); } -- 2.39.5