]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1330 Purge edit permissions when deleting user
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 27 Sep 2017 16:22:39 +0000 (18:22 +0200)
committerStas Vilchik <stas.vilchik@sonarsource.com>
Mon, 2 Oct 2017 15:18:15 +0000 (17:18 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java

index df41e1f49637967cf67d0dfca4efaaabffc0b72b..c41940af852b1ef2e0e13b9a36bfdb30ffae777a 100644 (file)
@@ -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);
   }
index 4397d07571446dec5d392b78bde9450b0adc0f8d..399a0e58f4d71700e59d20677c6e499c15172675 100644 (file)
@@ -39,4 +39,6 @@ public interface QProfileEditUsersMapper {
   void delete(@Param("qProfileUuid") String qProfileUuid, @Param("userId") int userId);
 
   void deleteByQProfiles(@Param("qProfileUuids") Collection<String> qProfileUuids);
+
+  void deleteByUser(@Param("userId") int userId);
 }
index 1f46018f276b480deb83439891d339f0aec4048c..2365902336a37bede653b86284e99932adaf6136 100644 (file)
     where qprofile_uuid in <foreach collection="qProfileUuids" open="(" close=")" item="qProfileUuid" separator=",">#{qProfileUuid, jdbcType=VARCHAR}</foreach>
   </delete>
 
+  <delete id="deleteByUser" parameterType="map">
+    delete from qprofile_edit_users
+    where user_id = #{userId, jdbcType=INTEGER}
+  </delete>
+
 </mapper>
 
index 2f7f8298f61a37e078332aadabb715f77f9c35df..35c8ee81f3dc76400b8fc2b5270a0981307cc395 100644 (file)
@@ -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();
+  }
 }
index d85c4f86198c1d75abe918be4ddc6dc423954e63..1a51d47fd489a3754b6eff2e4d60fef1e5657d3d 100644 (file)
@@ -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);
index 9b8346c9de90ae7c65ac061d814650decfcff2da..b4ac5d581e1001a0e43109fd063a966087f98f80 100644 (file)
@@ -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();