diff options
author | Guillaume Jambet <guillaume.jambet@sonarsource.com> | 2017-12-19 16:16:28 +0100 |
---|---|---|
committer | Stas Vilchik <stas.vilchik@sonarsource.com> | 2018-01-02 10:38:10 +0100 |
commit | 983509731f3ec3ae861da229de7980160608d9bd (patch) | |
tree | a9f4f8608cf11f3ab52be35cebd408dd9c7678ee | |
parent | 56c1dd13a73468dbbdc543f117876f9d574bc761 (diff) | |
download | sonarqube-983509731f3ec3ae861da229de7980160608d9bd.tar.gz sonarqube-983509731f3ec3ae861da229de7980160608d9bd.zip |
SONAR-10182 Clean homepage when deleting an organization
6 files changed, 81 insertions, 1 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java index 49a6bd9eff6..dcd27b2b809 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java @@ -35,6 +35,7 @@ import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.RowNotFoundException; +import org.sonar.db.organization.OrganizationDto; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; @@ -121,6 +122,10 @@ public class UserDao implements Dao { mapper(dbSession).deactivateUser(user.getLogin(), system2.now()); } + public void cleanHomepage(DbSession dbSession, OrganizationDto organization) { + mapper(dbSession).clearHomepage("ORGANIZATION", organization.getUuid(), system2.now()); + } + @CheckForNull public UserDto selectByLogin(DbSession session, String login) { return mapper(session).selectByLogin(login); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java index a894164aa04..f9fddb00f99 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java @@ -69,4 +69,5 @@ public interface UserMapper { void deactivateUser(@Param("login") String login, @Param("now") long now); + void clearHomepage(@Param("homepageType") String type, @Param("homepageValue") String value, @Param("now") long now); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml index 7e16d4ef582..db7e23a5b57 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -137,6 +137,16 @@ login = #{login, jdbcType=VARCHAR} </update> + <update id="clearHomepage" parameterType="map"> + update users set + homepage_type = null, + homepage_value = null, + updated_at = #{now, jdbcType=BIGINT} + where + homepage_type = #{homepageType, jdbcType=VARCHAR} + and homepage_value = #{homepageValue, jdbcType=VARCHAR} + </update> + <update id="setRoot"> update users set is_root = #{root, jdbcType=BOOLEAN}, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java index 448b17a4086..779a3bfdab2 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -34,6 +34,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; +import org.sonar.db.organization.OrganizationDto; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; @@ -417,6 +418,30 @@ public class UserDaoTest { } @Test + public void clean_users_homepage() { + + UserDto userUnderTest = newUserDto().setHomepageType("ORGANIZATION").setHomepageValue("dummy-organization-UUID"); + underTest.insert(session, userUnderTest); + + UserDto untouchedUser = newUserDto().setHomepageType("ORGANIZATION").setHomepageValue("not-so-dummy-organization-UUID"); + underTest.insert(session, untouchedUser); + + session.commit(); + + underTest.cleanHomepage(session, new OrganizationDto().setUuid("dummy-organization-UUID")); + + UserDto userWithAHomepageReloaded = underTest.selectUserById(session, userUnderTest.getId()); + assertThat(userWithAHomepageReloaded.getUpdatedAt()).isEqualTo(NOW); + assertThat(userWithAHomepageReloaded.getHomepageType()).isNull(); + assertThat(userWithAHomepageReloaded.getHomepageValue()).isNull(); + + UserDto untouchedUserReloaded = underTest.selectUserById(session, untouchedUser.getId()); + assertThat(untouchedUserReloaded.getUpdatedAt()).isEqualTo(untouchedUser.getUpdatedAt()); + assertThat(untouchedUserReloaded.getHomepageType()).isEqualTo(untouchedUser.getHomepageType()); + assertThat(untouchedUserReloaded.getHomepageValue()).isEqualTo(untouchedUser.getHomepageValue()); + } + + @Test public void does_not_fail_to_deactivate_missing_user() { underTest.deactivateUser(session, UserTesting.newUserDto()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java index 9a3bd8b79d8..e9eb145eda7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/ws/DeleteAction.java @@ -150,6 +150,7 @@ public class DeleteAction implements OrganizationsWsAction { Collection<String> logins = dbClient.organizationMemberDao().selectLoginsByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationMemberDao().deleteByOrganizationUuid(dbSession, organization.getUuid()); dbClient.organizationDao().deleteByUuid(dbSession, organization.getUuid()); + dbClient.userDao().cleanHomepage(dbSession, organization); userIndexer.commitAndIndexByLogins(dbSession, logins); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java index 31c33ec135d..bdb27b8e588 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/DeleteActionTest.java @@ -68,6 +68,7 @@ import static org.sonar.api.resources.Qualifiers.APP; import static org.sonar.api.resources.Qualifiers.PROJECT; import static org.sonar.api.resources.Qualifiers.VIEW; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER; +import static org.sonar.db.user.UserTesting.newUserDto; import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION; public class DeleteActionTest { @@ -91,7 +92,8 @@ public class DeleteActionTest { private UserIndex userIndex = new UserIndex(es.client(), System2.INSTANCE); private UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient); - private WsActionTester wsTester = new WsActionTester(new DeleteAction(userSession, dbClient, defaultOrganizationProvider, componentCleanerService, organizationFlags, userIndexer, qProfileFactory)); + private WsActionTester wsTester = new WsActionTester( + new DeleteAction(userSession, dbClient, defaultOrganizationProvider, componentCleanerService, organizationFlags, userIndexer, qProfileFactory)); @Test public void test_definition() { @@ -113,6 +115,42 @@ public class DeleteActionTest { } @Test + public void organization_deletion_also_ensure_that_homepage_on_this_organization_if_it_exists_is_cleared() throws Exception { + OrganizationDto organization = db.organizations().insert(); + UserDto user = dbClient.userDao().insert(session, newUserDto().setHomepageType("ORGANIZATION").setHomepageValue(organization.getUuid())); + session.commit(); + + userSession.logIn().addPermission(ADMINISTER, organization); + + wsTester.newRequest() + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); + + UserDto userReloaded = dbClient.userDao().selectUserById(session, user.getId()); + assertThat(userReloaded.getHomepageType()).isNull(); + assertThat(userReloaded.getHomepageValue()).isNull(); + } + + @Test + public void organization_deletion_also_ensure_that_homepage_on_project_belonging_to_this_organization_if_it_exists_is_cleared() { + OrganizationDto organization = db.organizations().insert(); + ComponentDto project = db.components().insertPrivateProject(organization); + UserDto user = dbClient.userDao().insert(session, + newUserDto().setHomepageType("PROJECT").setHomepageValue(project.uuid())); + session.commit(); + + userSession.logIn().addPermission(ADMINISTER, organization); + + wsTester.newRequest() + .setParam(PARAM_ORGANIZATION, organization.getKey()) + .execute(); + + UserDto userReloaded = dbClient.userDao().selectUserById(session, user.getId()); + assertThat(userReloaded.getHomepageType()).isNull(); + assertThat(userReloaded.getHomepageValue()).isNull(); + } + + @Test public void fail_with_IllegalStateException_if_organization_support_is_disabled() { organizationFlags.setEnabled(false); userSession.logIn(); |