From e1f3c47a66989f592545792c8cc3daa78eccebe1 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 29 Apr 2016 15:47:38 +0200 Subject: [PATCH] SONAR-7212 Remove "sonar.issues.defaultAssigneeLogin" when removing a user --- .../main/java/org/sonar/db/user/UserDao.java | 3 + .../java/org/sonar/db/user/UserMapper.java | 2 + .../org/sonar/db/user/UserMapper.xml | 11 +++ .../java/org/sonar/db/user/UserDaoTest.java | 81 ++++++++++++------- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java index 7d746d50bdf..e7c9d9e8dd4 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java @@ -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; diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java index e577c57e241..f65b396ee28 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java @@ -75,6 +75,8 @@ public interface UserMapper { void deleteUserRoles(long userId); + void deletePropertiesMatchingLogin(@Param("propertyKeys") List propertyKeys, @Param("login") String login); + void deactivateUser(@Param("id") long userId, @Param("now") long now); diff --git a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml index 36f61886772..298742e43a5 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -135,6 +135,17 @@ DELETE FROM issue_filter_favourites WHERE user_login=#{id} + + DELETE FROM properties + + text_value=#{login} + AND prop_key IN + + #{property} + + + + UPDATE users SET active=${_false}, email=null, scm_accounts=null, external_identity=null, external_identity_provider=null, diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java index 2e5f9ecd41b..fda1aa3a8f7 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -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); -- 2.39.5