@Override
@CheckForNull
public User findByLogin(String login) {
- UserDto dto = userDao.selectUserByLogin(login);
+ UserDto dto = userDao.selectActiveUserByLogin(login);
return dto != null ? dto.toUser() : null;
}
* @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);
}
}
+ /**
+ * 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.
*
import javax.annotation.CheckForNull;
import java.util.List;
-/**
- * @since 3.2
- */
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);
}
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>
@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");
}
@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();
}
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();
+ }
}
--- /dev/null
+<!--
+ ~ 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>
--- /dev/null
+<!--
+ ~ 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>
--- /dev/null
+<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>
+++ /dev/null
-<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>