From 518e18bf793fa576e09bce11f62e62f45e0c2f71 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 26 Jun 2013 07:59:29 +0200 Subject: [PATCH] SONAR-4411 Java implementation of user deactivation --- .../sonar/core/user/DefaultUserFinder.java | 2 +- .../java/org/sonar/core/user/UserDao.java | 33 ++++++++++++- .../java/org/sonar/core/user/UserMapper.java | 19 ++++---- .../org/sonar/core/user/UserMapper.xml | 43 +++++++++++++++++ .../core/user/DefaultUserFinderTest.java | 2 +- .../java/org/sonar/core/user/UserDaoTest.java | 32 +++++++++++-- .../UserDaoTest/deactivate_user-result.xml | 46 +++++++++++++++++++ .../core/user/UserDaoTest/deactivate_user.xml | 42 +++++++++++++++++ ...yLogin.xml => selectActiveUserByLogin.xml} | 0 9 files changed, 203 insertions(+), 16 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml rename sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/{selectUserByLogin.xml => selectActiveUserByLogin.xml} (100%) diff --git a/sonar-core/src/main/java/org/sonar/core/user/DefaultUserFinder.java b/sonar-core/src/main/java/org/sonar/core/user/DefaultUserFinder.java index 2ddfb1a76c9..929fb60b112 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/DefaultUserFinder.java +++ b/sonar-core/src/main/java/org/sonar/core/user/DefaultUserFinder.java @@ -42,7 +42,7 @@ public class DefaultUserFinder implements UserFinder { @Override @CheckForNull public User findByLogin(String login) { - UserDto dto = userDao.selectUserByLogin(login); + UserDto dto = userDao.selectActiveUserByLogin(login); return dto != null ? dto.toUser() : null; } diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java index 42a28e35a5d..6a2ce6a499b 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java @@ -44,7 +44,7 @@ public class UserDao { * @return the user, null if user not found */ @CheckForNull - public UserDto selectUserByLogin(String login) { + public UserDto selectActiveUserByLogin(String login) { SqlSession session = mybatis.openSession(); try { UserMapper mapper = session.getMapper(UserMapper.class); @@ -81,6 +81,37 @@ public class UserDao { } } + /** + * Deactivate a user and drops all his preferences. + * @return false if the user does not exist, true if the existing user has been deactivated + */ + public boolean deactivateUserByLogin(String login) { + SqlSession session = mybatis.openSession(); + try { + UserMapper mapper = session.getMapper(UserMapper.class); + UserDto dto = mapper.selectUserByLogin(login); + if (dto == null) { + return false; + } + + mapper.removeUserFromGroups(dto.getId()); + mapper.deleteUserActiveDashboards(dto.getId()); + mapper.deleteUserDashboards(dto.getId()); + mapper.deleteUserIssueFilters(dto.getLogin()); + mapper.deleteUserIssueFilterFavourites(dto.getLogin()); + mapper.deleteUserMeasureFilters(dto.getId()); + mapper.deleteUserMeasureFilterFavourites(dto.getId()); + mapper.deleteUserProperties(dto.getId()); + mapper.deleteUserRoles(dto.getId()); + mapper.deactivateUser(dto.getId()); + session.commit(); + return true; + + } finally { + MyBatis.closeQuietly(session); + } + } + /** * Search for group by name. * diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java b/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java index 74b5e08e9a7..8678b52d363 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java @@ -25,9 +25,6 @@ import org.sonar.api.user.UserQuery; import javax.annotation.CheckForNull; import java.util.List; -/** - * @since 3.2 - */ public interface UserMapper { /** @@ -36,17 +33,21 @@ public interface UserMapper { @CheckForNull UserDto selectUserByLogin(String login); - /** - * @since 3.6 - */ List selectUsersByLogins(@Param("logins") List logins); - /** - * @since 3.6 - */ List selectUsers(UserQuery query); @CheckForNull GroupDto selectGroupByName(String name); + void removeUserFromGroups(long userId); + void deleteUserActiveDashboards(long userId); + void deleteUserDashboards(long userId); + void deleteUserIssueFilters(String login); + void deleteUserIssueFilterFavourites(String login); + void deleteUserMeasureFilters(long userId); + void deleteUserMeasureFilterFavourites(long userId); + void deleteUserProperties(long userId); + void deleteUserRoles(long userId); + void deactivateUser(long userId); } diff --git a/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml index c662b2e03a9..3b5f9003cb6 100644 --- a/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml @@ -55,5 +55,48 @@ from groups where name=#{id} + + delete from groups_users where user_id=#{id} + + + + delete from user_roles where user_id=#{id} + + + + delete from properties where user_id=#{id} + + + + delete from dashboards where user_id=#{id} + + + + delete from active_dashboards where user_id=#{id} + + + + delete from user_roles where user_id=#{id} + + + + delete from measure_filters where user_id=#{id} + + + + delete from measure_filter_favourites where user_id=#{id} + + + + delete from issue_filters where user_login=#{id} + + + + delete from issue_filter_favourites where user_login=#{id} + + + + update users set active=${_false} where id=#{id} + diff --git a/sonar-core/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java b/sonar-core/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java index 5e59d83ad5b..6cdd037953d 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java @@ -36,7 +36,7 @@ public class DefaultUserFinderTest { @Test public void findByLogin() throws Exception { UserDto dto = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com"); - when(dao.selectUserByLogin("david")).thenReturn(dto); + when(dao.selectActiveUserByLogin("david")).thenReturn(dto); assertThat(finder.findByLogin("david").name()).isEqualTo("David"); } diff --git a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java index 3df4e1d875e..2badeb166a7 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java @@ -43,17 +43,17 @@ public class UserDaoTest extends AbstractDaoTestCase { @Test public void selectUserByLogin_ignore_inactive() { - setupData("selectUserByLogin"); + setupData("selectActiveUserByLogin"); - UserDto user = dao.selectUserByLogin("inactive_user"); + UserDto user = dao.selectActiveUserByLogin("inactive_user"); assertThat(user).isNull(); } @Test public void selectUserByLogin_not_found() { - setupData("selectUserByLogin"); + setupData("selectActiveUserByLogin"); - UserDto user = dao.selectUserByLogin("not_found"); + UserDto user = dao.selectActiveUserByLogin("not_found"); assertThat(user).isNull(); } @@ -153,4 +153,28 @@ public class UserDaoTest extends AbstractDaoTestCase { GroupDto group = dao.selectGroupByName("not-found"); assertThat(group).isNull(); } + + @Test + public void deactivate_user() { + setupData("deactivate_user"); + + String login = "marius"; + boolean deactivated = dao.deactivateUserByLogin(login); + assertThat(deactivated).isTrue(); + assertThat(dao.selectActiveUserByLogin(login)).isNull(); + checkTables("deactivate_user", + "dashboards", "active_dashboards", "groups_users", "issue_filters", + "issue_filter_favourites", "measure_filters", "measure_filter_favourites", + "properties", "user_roles"); + } + + @Test + public void deactivate_missing_user() { + setupData("deactivate_user"); + + String login = "does_not_exist"; + boolean deactivated = dao.deactivateUserByLogin(login); + assertThat(deactivated).isFalse(); + assertThat(dao.selectActiveUserByLogin(login)).isNull(); + } } diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml new file mode 100644 index 00000000000..7ec29baf07d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml new file mode 100644 index 00000000000..1b102962bcd --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUserByLogin.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml similarity index 100% rename from sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUserByLogin.xml rename to sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml -- 2.39.5