]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4411 Java implementation of user deactivation
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 26 Jun 2013 05:59:29 +0000 (07:59 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 26 Jun 2013 05:59:29 +0000 (07:59 +0200)
sonar-core/src/main/java/org/sonar/core/user/DefaultUserFinder.java
sonar-core/src/main/java/org/sonar/core/user/UserDao.java
sonar-core/src/main/java/org/sonar/core/user/UserMapper.java
sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml
sonar-core/src/test/java/org/sonar/core/user/DefaultUserFinderTest.java
sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/deactivate_user.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectUserByLogin.xml [deleted file]

index 2ddfb1a76c929376ceae70055b1ac2054623a4da..929fb60b112c73fda777e1fb81d25b4cbcb0d500 100644 (file)
@@ -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;
   }
 
index 42a28e35a5d6a54b6aac2983517a8f41199944df..6a2ce6a499bbd47c36c5bb09f667f6773d5422a6 100644 (file)
@@ -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.
    *
index 74b5e08e9a7952e6542c5caed6b5f2be064e873f..8678b52d363c8a83c0ee33e7764b16f6806cf029 100644 (file)
@@ -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<UserDto> selectUsersByLogins(@Param("logins") List<String> logins);
 
-  /**
-   * @since 3.6
-   */
   List<UserDto> 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);
 }
index c662b2e03a96c97a933065cecf2035f01372bfee..3b5f9003cb6d0e99fb50a19f93e0c44550c5342c 100644 (file)
     from groups where name=#{id}
   </select>
 
+  <delete id="removeUserFromGroups" parameterType="long">
+    delete from groups_users where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserRoles" parameterType="long">
+    delete from user_roles where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserProperties" parameterType="long">
+    delete from properties where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserDashboards" parameterType="long">
+    delete from dashboards where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserActiveDashboards" parameterType="long">
+    delete from active_dashboards where user_id=#{id}
+  </delete>
+
+  <delete id="" parameterType="long">
+    delete from user_roles where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserMeasureFilters" parameterType="long">
+    delete from measure_filters where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserMeasureFilterFavourites" parameterType="long">
+    delete from measure_filter_favourites where user_id=#{id}
+  </delete>
+
+  <delete id="deleteUserIssueFilters" parameterType="String">
+    delete from issue_filters where user_login=#{id}
+  </delete>
+
+  <delete id="deleteUserIssueFilterFavourites" parameterType="String">
+    delete from issue_filter_favourites where user_login=#{id}
+  </delete>
+
+  <update id="deactivateUser" parameterType="long">
+    update users set active=${_false} where id=#{id}
+  </update>
 
 </mapper>
index 5e59d83ad5b6765684cb19c33c4ec0358c682572..6cdd037953d22d6990cd988109e363c3b7d608e9 100644 (file)
@@ -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");
   }
index 3df4e1d875e84810e1ec2575fbb39dad383abd14..2badeb166a76d34c54bc47b1e85c9fae4ebf6534 100644 (file)
@@ -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 (file)
index 0000000..7ec29ba
--- /dev/null
@@ -0,0 +1,46 @@
+<!--
+  ~ SonarQube, open source software quality management tool.
+  ~ Copyright (C) 2008-2013 SonarSource
+  ~ mailto:contact AT sonarsource DOT com
+  ~
+  ~ SonarQube is free software; you can redistribute it and/or
+  ~ modify it under the terms of the GNU Lesser General Public
+  ~ License as published by the Free Software Foundation; either
+  ~ version 3 of the License, or (at your option) any later version.
+  ~
+  ~ SonarQube is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  ~ Lesser General Public License for more details.
+  ~
+  ~ You should have received a copy of the GNU Lesser General Public License
+  ~ along with this program; if not, write to the Free Software Foundation,
+  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+  -->
+<dataset>
+  <!-- deactivated -->
+  <users id="100" login="marius" name="Marius" email="marius@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[false]"/>
+
+  <!-- deleted
+  <dashboards id="1" user_id="100" NAME="[null]" COLUMN_LAYOUT="[null]" CREATED_AT="[null]" DESCRIPTION="[null]" IS_GLOBAL="[false]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <active_dashboards id="1" user_id="100" dashboard_id="1" ORDER_INDEX="[null]"/>
+  <issue_filters id="1" user_login="marius" name="My issues"  CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <issue_filter_favourites id="1" user_login="marius" issue_filter_id="1" CREATED_AT="[null]"/>
+  <measure_filters id="1" user_id="100" name="My measures" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <measure_filter_favourites id="1" user_id="100" measure_filter_id="1" CREATED_AT="[null]"/>
+  <properties id="1" user_id="100" PROP_KEY="[null]" RESOURCE_ID="[null]" TEXT_VALUE="[null]" />
+  <groups_users user_id="100" group_id="200"/>
+  <user_roles id="1" user_id="100" role="admin" RESOURCE_ID="[null]"/>
+  -->
+
+  <users id="101" login="jcdus" name="Jean-Claude Dus" email="jcdus@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
+  <dashboards id="2" user_id="101" NAME="[null]" COLUMN_LAYOUT="[null]" CREATED_AT="[null]" DESCRIPTION="[null]" IS_GLOBAL="[false]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <active_dashboards id="2" user_id="101" dashboard_id="2" ORDER_INDEX="[null]"/>
+  <issue_filters id="2" user_login="jcdus" name="My issues" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <issue_filter_favourites id="2" user_login="jcdus" issue_filter_id="2" CREATED_AT="[null]"/>
+  <measure_filters id="2" user_id="101" name="My measures" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <measure_filter_favourites id="2" user_id="101" measure_filter_id="2" CREATED_AT="[null]"/>
+  <properties id="2" user_id="101" PROP_KEY="[null]" RESOURCE_ID="[null]" TEXT_VALUE="[null]" />
+  <groups_users user_id="101" group_id="200"/>
+  <user_roles id="2" user_id="101" role="admin" RESOURCE_ID="[null]"/>
+</dataset>
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 (file)
index 0000000..1b10296
--- /dev/null
@@ -0,0 +1,42 @@
+<!--
+  ~ SonarQube, open source software quality management tool.
+  ~ Copyright (C) 2008-2013 SonarSource
+  ~ mailto:contact AT sonarsource DOT com
+  ~
+  ~ SonarQube is free software; you can redistribute it and/or
+  ~ modify it under the terms of the GNU Lesser General Public
+  ~ License as published by the Free Software Foundation; either
+  ~ version 3 of the License, or (at your option) any later version.
+  ~
+  ~ SonarQube is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  ~ Lesser General Public License for more details.
+  ~
+  ~ You should have received a copy of the GNU Lesser General Public License
+  ~ along with this program; if not, write to the Free Software Foundation,
+  ~ Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+  -->
+<dataset>
+  <users id="100" login="marius" name="Marius" email="marius@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
+  <dashboards id="1" user_id="100" NAME="[null]" COLUMN_LAYOUT="[null]" CREATED_AT="[null]" DESCRIPTION="[null]" IS_GLOBAL="[false]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <active_dashboards id="1" user_id="100" dashboard_id="1" ORDER_INDEX="[null]"/>
+  <issue_filters id="1" user_login="marius" name="My issues"  CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <issue_filter_favourites id="1" user_login="marius" issue_filter_id="1" CREATED_AT="[null]"/>
+  <measure_filters id="1" user_id="100" name="My measures" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <measure_filter_favourites id="1" user_id="100" measure_filter_id="1" CREATED_AT="[null]"/>
+  <properties id="1" user_id="100" PROP_KEY="[null]" RESOURCE_ID="[null]" TEXT_VALUE="[null]" />
+  <groups_users user_id="100" group_id="200"/>
+  <user_roles id="1" user_id="100" role="admin" RESOURCE_ID="[null]"/>
+
+  <users id="101" login="jcdus" name="Jean-Claude Dus" email="jcdus@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
+  <dashboards id="2" user_id="101" NAME="[null]" COLUMN_LAYOUT="[null]" CREATED_AT="[null]" DESCRIPTION="[null]" IS_GLOBAL="[false]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <active_dashboards id="2" user_id="101" dashboard_id="2" ORDER_INDEX="[null]"/>
+  <issue_filters id="2" user_login="jcdus" name="My issues" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <issue_filter_favourites id="2" user_login="jcdus" issue_filter_id="2" CREATED_AT="[null]"/>
+  <measure_filters id="2" user_id="101" name="My measures" CREATED_AT="[null]" DATA="[null]" DESCRIPTION="[null]" SHARED="[false]" UPDATED_AT="[null]"/>
+  <measure_filter_favourites id="2" user_id="101" measure_filter_id="2" CREATED_AT="[null]"/>
+  <properties id="2" user_id="101" PROP_KEY="[null]" RESOURCE_ID="[null]" TEXT_VALUE="[null]" />
+  <groups_users user_id="101" group_id="200"/>
+  <user_roles id="2" user_id="101" role="admin" RESOURCE_ID="[null]"/>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/selectActiveUserByLogin.xml
new file mode 100644 (file)
index 0000000..0052e0c
--- /dev/null
@@ -0,0 +1,11 @@
+<dataset>
+  <!-- inactive -->
+  <users id="50" login="inactive_user" name="Disabled" email="inactive@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[false]"/>
+
+  <!-- active -->
+  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
+
+  <users id="102" login="jcdus" name="Jean-Claude Dus" email="jcdus@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
+
+
+</dataset>
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/selectUserByLogin.xml
deleted file mode 100644 (file)
index 0052e0c..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<dataset>
-  <!-- inactive -->
-  <users id="50" login="inactive_user" name="Disabled" email="inactive@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[false]"/>
-
-  <!-- active -->
-  <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
-
-  <users id="102" login="jcdus" name="Jean-Claude Dus" email="jcdus@lesbronzes.fr" created_at="2011-05-18" updated_at="2012-07-21" active="[true]"/>
-
-
-</dataset>