]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7212 Remove "sonar.issues.defaultAssigneeLogin" when removing a user
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 29 Apr 2016 13:47:38 +0000 (15:47 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 3 May 2016 12:26:31 +0000 (14:26 +0200)
sonar-db/src/main/java/org/sonar/db/user/UserDao.java
sonar-db/src/main/java/org/sonar/db/user/UserMapper.java
sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml
sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java

index 7d746d50bdf05679feeebf04b72a436568213268..e7c9d9e8dd4cca8322dc02badeb74248c62798d6 100644 (file)
@@ -37,6 +37,8 @@ import org.sonar.db.MyBatis;
 import org.sonar.db.RowNotFoundException;
 
 import static com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
 
 public class UserDao implements Dao {
 
@@ -177,6 +179,7 @@ public class UserDao implements Dao {
     mapper.deleteUserMeasureFilterFavourites(dto.getId());
     mapper.deleteUserProperties(dto.getId());
     mapper.deleteUserRoles(dto.getId());
+    mapper.deletePropertiesMatchingLogin(asList(DEFAULT_ISSUE_ASSIGNEE), dto.getLogin());
     mapper.deactivateUser(dto.getId(), system2.now());
     dbSession.commit();
     return true;
index e577c57e2419533cbcb94d714a70f232e05afa5e..f65b396ee28385bc0ccdee4a7bbde68c2ea7f5cd 100644 (file)
@@ -75,6 +75,8 @@ public interface UserMapper {
 
   void deleteUserRoles(long userId);
 
+  void deletePropertiesMatchingLogin(@Param("propertyKeys") List<String> propertyKeys, @Param("login") String login);
+
   void deactivateUser(@Param("id") long userId, @Param("now") long now);
 
 
index 36f618867728c21f497a6f4a394836bdb0fcaf4a..298742e43a5f4a872cd1ac777074952eed114e90 100644 (file)
     DELETE FROM issue_filter_favourites WHERE user_login=#{id}
   </delete>
 
+  <delete id="deletePropertiesMatchingLogin" parameterType="String">
+    DELETE FROM properties
+    <where>
+      text_value=#{login}
+      AND prop_key IN
+      <foreach item="property" index="index" collection="propertyKeys" open="(" separator="," close=")">
+        #{property}
+      </foreach>
+    </where>
+  </delete>
+
   <update id="deactivateUser" parameterType="long">
     UPDATE users SET active=${_false}, email=null, scm_accounts=null,
     external_identity=null, external_identity_provider=null,
index 2e5f9ecd41bee5a770c574cf1aed5cff97b0e893..fda1aa3a8f7497bfc95b67ee96d1386c6e6e9621 100644 (file)
@@ -53,7 +53,6 @@ import static org.sonar.db.user.GroupMembershipQuery.IN;
 import static org.sonar.db.user.GroupMembershipQuery.builder;
 import static org.sonar.db.user.UserTesting.newUserDto;
 
-
 public class UserDaoTest {
 
   @Rule
@@ -290,15 +289,15 @@ public class UserDaoTest {
   @Test
   public void deactivate_user() throws Exception {
     UserDto user = newActiveUser();
-    DashboardDto dashboard = newDashboard(user, false);
-    ActiveDashboardDto activeDashboard = newActiveDashboard(dashboard, user);
-    IssueFilterDto issueFilter = newIssueFilter(user, false);
-    IssueFilterFavouriteDto issueFilterFavourite = newIssueFilterFavourite(issueFilter, user);
-    MeasureFilterDto measureFilter = newMeasureFilter(user, false);
-    MeasureFilterFavouriteDto measureFilterFavourite = newMeasureFilterFavourite(measureFilter, user);
-    PropertyDto property = newProperty(user);
-    newUserRole(user);
-    newUserGroup(user);
+    DashboardDto dashboard = insertDashboard(user, false);
+    ActiveDashboardDto activeDashboard = insertActiveDashboard(dashboard, user);
+    IssueFilterDto issueFilter = insertIssueFilter(user, false);
+    IssueFilterFavouriteDto issueFilterFavourite = insertIssueFilterFavourite(issueFilter, user);
+    MeasureFilterDto measureFilter = insertMeasureFilter(user, false);
+    MeasureFilterFavouriteDto measureFilterFavourite = insertMeasureFilterFavourite(measureFilter, user);
+    PropertyDto property = insertProperty(user);
+    insertUserRole(user);
+    insertUserGroup(user);
 
     UserDto otherUser = newActiveUser();
 
@@ -335,8 +334,8 @@ public class UserDaoTest {
   @Test
   public void deactivate_user_does_not_remove_shared_dashboard() throws Exception {
     UserDto user = newActiveUser();
-    DashboardDto notSharedDashboard = newDashboard(user, false);
-    DashboardDto sharedDashboard = newDashboard(user, true);
+    DashboardDto notSharedDashboard = insertDashboard(user, false);
+    DashboardDto sharedDashboard = insertDashboard(user, true);
     session.commit();
 
     boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin());
@@ -351,8 +350,8 @@ public class UserDaoTest {
   @Test
   public void deactivate_user_does_not_remove_shared_issue_filter() throws Exception {
     UserDto user = newActiveUser();
-    IssueFilterDto notSharedFilter = newIssueFilter(user, false);
-    IssueFilterDto sharedFilter = newIssueFilter(user, true);
+    IssueFilterDto notSharedFilter = insertIssueFilter(user, false);
+    IssueFilterDto sharedFilter = insertIssueFilter(user, true);
     session.commit();
 
     boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin());
@@ -367,8 +366,8 @@ public class UserDaoTest {
   @Test
   public void deactivate_user_does_not_remove_shared_measure_filter() throws Exception {
     UserDto user = newActiveUser();
-    MeasureFilterDto notSharedFilter = newMeasureFilter(user, false);
-    MeasureFilterDto sharedFilter = newMeasureFilter(user, true);
+    MeasureFilterDto notSharedFilter = insertMeasureFilter(user, false);
+    MeasureFilterDto sharedFilter = insertMeasureFilter(user, true);
     session.commit();
 
     boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin());
@@ -380,6 +379,24 @@ public class UserDaoTest {
     assertThat(sharedFilterReloaded.getUserId()).isEqualTo(user.getId());
   }
 
+  @Test
+  public void deactivate_user_also_remove_default_assignee_login_properties() throws Exception {
+    UserDto user = newActiveUser();
+    insertProperty("sonar.issues.defaultAssigneeLogin", user.getLogin(), 10L);
+    insertProperty("sonar.issues.defaultAssigneeLogin", user.getLogin(), 11L);
+    insertProperty("sonar.issues.defaultAssigneeLogin", user.getLogin(), 12L);
+
+    UserDto otherUser = newActiveUser();
+    insertProperty("sonar.issues.defaultAssigneeLogin", otherUser.getLogin(), 13L);
+
+    session.commit();
+
+    boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin());
+    assertThat(deactivated).isTrue();
+
+    assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setKey("sonar.issues.defaultAssigneeLogin").build(), session)).hasSize(1);
+  }
+
   @Test
   public void deactivate_missing_user() {
     String login = "does_not_exist";
@@ -462,72 +479,78 @@ public class UserDaoTest {
   @Test
   public void exists_by_email() throws Exception {
     UserDto activeUser = newActiveUser();
-    UserDto disableUser = newUser(false);
+    UserDto disableUser = insertUser(false);
 
     assertThat(underTest.doesEmailExist(session, activeUser.getEmail())).isTrue();
     assertThat(underTest.doesEmailExist(session, disableUser.getEmail())).isFalse();
     assertThat(underTest.doesEmailExist(session, "unknown")).isFalse();
   }
 
-  private UserDto newActiveUser(){
-    return newUser(true);
+  private UserDto newActiveUser() {
+    return insertUser(true);
   }
 
-  private UserDto newUser(boolean active){
+  private UserDto insertUser(boolean active) {
     UserDto dto = newUserDto().setActive(active);
     underTest.insert(session, dto);
     return dto;
   }
 
-  private DashboardDto newDashboard(UserDto user, boolean shared){
+  private DashboardDto insertDashboard(UserDto user, boolean shared) {
     DashboardDto dto = new DashboardDto().setUserId(user.getId()).setShared(shared);
     dbClient.dashboardDao().insert(session, dto);
     return dto;
   }
 
-  private ActiveDashboardDto newActiveDashboard(DashboardDto dashboard, UserDto user){
+  private ActiveDashboardDto insertActiveDashboard(DashboardDto dashboard, UserDto user) {
     ActiveDashboardDto dto = new ActiveDashboardDto().setDashboardId(dashboard.getId()).setUserId(user.getId());
     dbClient.activeDashboardDao().insert(session, dto);
     return dto;
   }
 
-  private IssueFilterDto newIssueFilter(UserDto user, boolean shared){
+  private IssueFilterDto insertIssueFilter(UserDto user, boolean shared) {
     IssueFilterDto dto = new IssueFilterDto().setUserLogin(user.getLogin()).setName(randomAlphanumeric(100)).setShared(shared);
     dbClient.issueFilterDao().insert(session, dto);
     return dto;
   }
 
-  private IssueFilterFavouriteDto newIssueFilterFavourite(IssueFilterDto filter, UserDto user){
+  private IssueFilterFavouriteDto insertIssueFilterFavourite(IssueFilterDto filter, UserDto user) {
     IssueFilterFavouriteDto dto = new IssueFilterFavouriteDto().setUserLogin(user.getLogin()).setIssueFilterId(filter.getId());
     dbClient.issueFilterFavouriteDao().insert(session, dto);
     return dto;
   }
 
-  private MeasureFilterDto newMeasureFilter(UserDto user, boolean shared){
+  private MeasureFilterDto insertMeasureFilter(UserDto user, boolean shared) {
     MeasureFilterDto dto = new MeasureFilterDto().setUserId(user.getId()).setName(randomAlphanumeric(100)).setShared(shared);
     dbClient.measureFilterDao().insert(session, dto);
     return dto;
   }
 
-  private MeasureFilterFavouriteDto newMeasureFilterFavourite(MeasureFilterDto measureFilter, UserDto user){
+  private MeasureFilterFavouriteDto insertMeasureFilterFavourite(MeasureFilterDto measureFilter, UserDto user) {
     MeasureFilterFavouriteDto dto = new MeasureFilterFavouriteDto().setUserId(user.getId()).setMeasureFilterId(measureFilter.getId());
     dbClient.measureFilterFavouriteDao().insert(session, dto);
     return dto;
   }
 
-  private PropertyDto newProperty(UserDto user){
+  private PropertyDto insertProperty(UserDto user) {
     PropertyDto dto = new PropertyDto().setKey(randomAlphanumeric(100)).setUserId(user.getId());
     dbClient.propertiesDao().insertProperty(session, dto);
     return dto;
   }
 
-  private UserRoleDto newUserRole(UserDto user){
+  private PropertyDto insertProperty(String key, String value, long componentId) {
+    PropertyDto dto = new PropertyDto().setKey(key).setValue(value).setResourceId(componentId);
+    dbClient.propertiesDao().insertProperty(session, dto);
+    return dto;
+  }
+
+  private UserRoleDto insertUserRole(UserDto user) {
     UserRoleDto dto = new UserRoleDto().setUserId(user.getId()).setRole(randomAlphanumeric(64));
     dbClient.roleDao().insertUserRole(session, dto);
     return dto;
   }
 
-  private UserGroupDto newUserGroup(UserDto user){
+  private UserGroupDto insertUserGroup(UserDto user) {
     GroupDto group = new GroupDto().setName(randomAlphanumeric(30));
     dbClient.groupDao().insert(session, group);