From b21a14010c4941746cffb5de4296ec99a4ebf981 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 27 Sep 2017 18:24:40 +0200 Subject: [PATCH] SONAR-1330 Purge edit permissions when removing organization member --- .../qualityprofile/QProfileEditUsersDao.java | 4 ++++ .../QProfileEditUsersMapper.java | 2 ++ .../qualityprofile/QProfileEditUsersMapper.xml | 12 ++++++++++++ .../QProfileEditUsersDaoTest.java | 18 ++++++++++++++++++ .../organization/ws/RemoveMemberAction.java | 1 + .../ws/RemoveMemberActionTest.java | 16 ++++++++++++++++ 6 files changed, 53 insertions(+) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java index c41940af852..6a8350fb094 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java @@ -70,6 +70,10 @@ public class QProfileEditUsersDao implements Dao { mapper(dbSession).deleteByUser(user.getId()); } + public void deleteByOrganizationAndUser(DbSession dbSession, OrganizationDto organization, UserDto user) { + mapper(dbSession).deleteByOrganizationAndUser(organization.getUuid(), user.getId()); + } + private static QProfileEditUsersMapper mapper(DbSession dbSession) { return dbSession.getMapper(QProfileEditUsersMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java index 399a0e58f4d..25b2b18907e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java @@ -41,4 +41,6 @@ public interface QProfileEditUsersMapper { void deleteByQProfiles(@Param("qProfileUuids") Collection qProfileUuids); void deleteByUser(@Param("userId") int userId); + + void deleteByOrganizationAndUser(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml index 2365902336a..0256e05d9a2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml @@ -115,5 +115,17 @@ where user_id = #{userId, jdbcType=INTEGER} + + delete from qprofile_edit_users + + user_id=#{userId, jdbcType=INTEGER} + and qprofile_uuid in ( + select oq.uuid + from org_qprofiles oq + where oq.organization_uuid=#{organizationUuid, jdbcType=VARCHAR} + ) + + + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java index 35c8ee81f3d..67f9f1ca1d0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java @@ -329,4 +329,22 @@ public class QProfileEditUsersDaoTest { assertThat(underTest.exists(db.getSession(), profile2, user1)).isFalse(); assertThat(underTest.exists(db.getSession(), profile3, user2)).isTrue(); } + + @Test + public void deleteByOrganizationAndUser() { + OrganizationDto organization1 = db.organizations().insert(); + OrganizationDto organization2 = db.organizations().insert(); + QProfileDto profile1 = db.qualityProfiles().insert(organization1); + QProfileDto profile2 = db.qualityProfiles().insert(organization2); + UserDto user = db.users().insertUser(); + db.organizations().addMember(organization1, user); + db.organizations().addMember(organization2, user); + db.qualityProfiles().addUserPermission(profile1, user); + db.qualityProfiles().addUserPermission(profile2, user); + + underTest.deleteByOrganizationAndUser(db.getSession(), organization1, user); + + assertThat(underTest.exists(db.getSession(), profile1, user)).isFalse(); + assertThat(underTest.exists(db.getSession(), profile2, user)).isTrue(); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/RemoveMemberAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/RemoveMemberAction.java index a927bdfcbd5..688e2dbdba9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/RemoveMemberAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/RemoveMemberAction.java @@ -98,6 +98,7 @@ public class RemoveMemberAction implements OrganizationsWsAction { String organizationUuid = organization.getUuid(); dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationUuid, userId); dbClient.permissionTemplateDao().deleteUserPermissionsByOrganization(dbSession, organizationUuid, userId); + dbClient.qProfileEditUsersDao().deleteByOrganizationAndUser(dbSession, organization, user); dbClient.userGroupDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId); dbClient.propertiesDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId); dbClient.propertiesDao().deleteByOrganizationAndMatchingLogin(dbSession, organizationUuid, user.getLogin(), singletonList(DEFAULT_ISSUE_ASSIGNEE)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/RemoveMemberActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/RemoveMemberActionTest.java index fe6a84f5132..a6bfbb75049 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/RemoveMemberActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/RemoveMemberActionTest.java @@ -38,6 +38,7 @@ import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.db.permission.template.PermissionTemplateUserDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; +import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsTester; @@ -185,6 +186,21 @@ public class RemoveMemberActionTest { .containsOnly(user.getId()); } + @Test + public void remove_qprofiles_user_permission() { + OrganizationDto anotherOrganization = db.organizations().insert(); + db.organizations().addMember(anotherOrganization, user); + QProfileDto profile = db.qualityProfiles().insert(organization); + QProfileDto anotherProfile = db.qualityProfiles().insert(anotherOrganization); + db.qualityProfiles().addUserPermission(profile, user); + db.qualityProfiles().addUserPermission(anotherProfile, user); + + call(organization.getKey(), user.getLogin()); + + assertThat(db.getDbClient().qProfileEditUsersDao().exists(dbSession, profile, user)).isFalse(); + assertThat(db.getDbClient().qProfileEditUsersDao().exists(dbSession, anotherProfile, user)).isTrue(); + } + @Test public void remove_from_organization_groups() { OrganizationDto anotherOrganization = db.organizations().insert(); -- 2.39.5