From f0af72abc18ccbe6eb0227d38dc898f052e04eda Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 27 Sep 2017 18:22:39 +0200 Subject: [PATCH] SONAR-1330 Purge edit permissions when deleting user --- .../qualityprofile/QProfileEditUsersDao.java | 4 ++++ .../QProfileEditUsersMapper.java | 2 ++ .../QProfileEditUsersMapper.xml | 5 +++++ .../QProfileEditUsersDaoTest.java | 20 +++++++++++++++++++ .../server/user/ws/DeactivateAction.java | 1 + .../server/user/ws/DeactivateActionTest.java | 13 ++++++++++++ 6 files changed, 45 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 df41e1f4963..c41940af852 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 @@ -66,6 +66,10 @@ public class QProfileEditUsersDao implements Dao { executeLargeUpdates(qProfiles.stream().map(QProfileDto::getKee).collect(toList()), p -> mapper(dbSession).deleteByQProfiles(p)); } + public void deleteByUser(DbSession dbSession, UserDto user) { + mapper(dbSession).deleteByUser(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 4397d075714..399a0e58f4d 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 @@ -39,4 +39,6 @@ public interface QProfileEditUsersMapper { void delete(@Param("qProfileUuid") String qProfileUuid, @Param("userId") int userId); void deleteByQProfiles(@Param("qProfileUuids") Collection qProfileUuids); + + void deleteByUser(@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 1f46018f276..2365902336a 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 @@ -110,5 +110,10 @@ where qprofile_uuid in #{qProfileUuid, jdbcType=VARCHAR} + + delete from qprofile_edit_users + where user_id = #{userId, jdbcType=INTEGER} + + 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 2f7f8298f61..35c8ee81f3d 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 @@ -309,4 +309,24 @@ public class QProfileEditUsersDaoTest { assertThat(underTest.exists(db.getSession(), profile3, user1)).isTrue(); assertThat(underTest.exists(db.getSession(), anotherProfile, user1)).isTrue(); } + + @Test + public void deleteByUser() { + OrganizationDto organization1 = db.organizations().insert(); + OrganizationDto organization2 = db.organizations().insert(); + QProfileDto profile1 = db.qualityProfiles().insert(organization1); + QProfileDto profile2 = db.qualityProfiles().insert(organization2); + QProfileDto profile3 = db.qualityProfiles().insert(organization1); + UserDto user1 = db.users().insertUser(); + UserDto user2 = db.users().insertUser(); + db.qualityProfiles().addUserPermission(profile1, user1); + db.qualityProfiles().addUserPermission(profile2, user1); + db.qualityProfiles().addUserPermission(profile3, user2); + + underTest.deleteByUser(db.getSession(), user1); + + assertThat(underTest.exists(db.getSession(), profile1, user1)).isFalse(); + assertThat(underTest.exists(db.getSession(), profile2, user1)).isFalse(); + assertThat(underTest.exists(db.getSession(), profile3, user2)).isTrue(); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java index d85c4f86198..1a51d47fd48 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java @@ -100,6 +100,7 @@ public class DeactivateAction implements UsersWsAction { dbClient.userGroupDao().deleteByUserId(dbSession, userId); dbClient.userPermissionDao().deleteByUserId(dbSession, userId); dbClient.permissionTemplateDao().deleteUserPermissionsByUserId(dbSession, userId); + dbClient.qProfileEditUsersDao().deleteByUser(dbSession, user); dbClient.organizationMemberDao().deleteByUserId(dbSession, userId); dbClient.userDao().deactivateUser(dbSession, user); userIndexer.commitAndIndex(dbSession, user); diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java index 9b8346c9de9..b4ac5d581e1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java @@ -35,6 +35,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; @@ -177,6 +178,18 @@ public class DeactivateActionTest { assertThat(db.getDbClient().permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, anotherTemplate.getId())).extracting(PermissionTemplateUserDto::getUserId).isEmpty(); } + @Test + public void deactivate_user_deletes_his_qprofiles_permissions() { + logInAsSystemAdministrator(); + UserDto user = insertUser(newUserDto()); + QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); + db.qualityProfiles().addUserPermission(profile, user); + + deactivate(user.getLogin()).getInput(); + + assertThat(db.getDbClient().qProfileEditUsersDao().exists(dbSession, profile, user)).isFalse(); + } + @Test public void deactivate_user_deletes_his_default_assignee_settings() { logInAsSystemAdministrator(); -- 2.39.5