mapper(dbSession).deactivateUser(user.getLogin(), system2.now());
}
- public void deactivateSonarCloudUser(DbSession dbSession, UserDto user) {
- mapper(dbSession).deactivateSonarCloudUser(user.getLogin(), system2.now());
- }
-
public void cleanHomepage(DbSession dbSession, OrganizationDto organization) {
mapper(dbSession).clearHomepages("ORGANIZATION", organization.getUuid(), system2.now());
}
void deactivateUser(@Param("login") String login, @Param("now") long now);
- void deactivateSonarCloudUser(@Param("login") String login, @Param("now") long now);
-
void clearHomepages(@Param("homepageType") String type, @Param("homepageParameter") String value, @Param("now") long now);
void clearHomepage(@Param("login") String login, @Param("now") long now);
login = #{login, jdbcType=VARCHAR}
</update>
- <update id="deactivateSonarCloudUser" parameterType="map">
- update users set
- name = null,
- <include refid="deactivateUserUpdatedFields"/>
- where
- login = #{login, jdbcType=VARCHAR}
- </update>
-
<update id="clearHomepages" parameterType="map">
update users set
homepage_type = null,
assertThat(underTest.selectUserById(session, otherUser.getId())).isNotNull();
}
- @Test
- public void deactivate_sonarcloud_user() {
- UserDto user = insertActiveUser();
- insertUserGroup(user);
- UserDto otherUser = insertActiveUser();
- underTest.update(db.getSession(), user.setLastConnectionDate(10_000_000_000L));
- session.commit();
-
- underTest.deactivateSonarCloudUser(session, user);
-
- UserDto userReloaded = underTest.selectUserById(session, user.getId());
- assertThat(userReloaded.isActive()).isFalse();
- assertThat(userReloaded.getName()).isNull();
- assertThat(userReloaded.getLogin()).isEqualTo(user.getLogin());
- assertThat(userReloaded.getExternalId()).isEqualTo(user.getExternalId());
- assertThat(userReloaded.getExternalLogin()).isEqualTo(user.getExternalLogin());
- assertThat(userReloaded.getExternalIdentityProvider()).isEqualTo(user.getExternalIdentityProvider());
- assertThat(userReloaded.getEmail()).isNull();
- assertThat(userReloaded.getScmAccounts()).isNull();
- assertThat(userReloaded.getSalt()).isNull();
- assertThat(userReloaded.getCryptedPassword()).isNull();
- assertThat(userReloaded.isRoot()).isFalse();
- assertThat(userReloaded.getUpdatedAt()).isEqualTo(NOW);
- assertThat(userReloaded.getHomepageType()).isNull();
- assertThat(userReloaded.getHomepageParameter()).isNull();
- assertThat(userReloaded.getLastConnectionDate()).isNull();
- assertThat(underTest.selectUserById(session, otherUser.getId())).isNotNull();
- }
-
@Test
public void clean_users_homepage_when_deleting_organization() {
import org.sonar.db.property.PropertyQuery;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import org.sonar.server.user.index.UserIndexer;
import static java.util.Collections.singletonList;
import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_ENABLED;
-import static org.sonar.server.exceptions.NotFoundException.checkFound;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
+import static org.sonar.server.exceptions.NotFoundException.checkFound;
public class DeactivateAction implements UsersWsAction {
private final UserSession userSession;
private final UserJsonWriter userWriter;
private final DefaultOrganizationProvider defaultOrganizationProvider;
- private final boolean isSonarCloud;
public DeactivateAction(DbClient dbClient, UserIndexer userIndexer, UserSession userSession, UserJsonWriter userWriter,
- DefaultOrganizationProvider defaultOrganizationProvider, Configuration configuration) {
+ DefaultOrganizationProvider defaultOrganizationProvider) {
this.dbClient = dbClient;
this.userIndexer = userIndexer;
this.userSession = userSession;
this.userWriter = userWriter;
this.defaultOrganizationProvider = defaultOrganizationProvider;
- this.isSonarCloud = configuration.getBoolean(SONARCLOUD_ENABLED.getKey()).orElse(false);
}
@Override
public void handle(Request request, Response response) throws Exception {
String login;
- if (isSonarCloud) {
- login = request.mandatoryParam(PARAM_LOGIN);
- if (!login.equals(userSession.getLogin()) && !userSession.checkLoggedIn().isSystemAdministrator()) {
- throw new ForbiddenException("Insufficient privileges");
- }
- } else {
- userSession.checkLoggedIn().checkIsSystemAdministrator();
- login = request.mandatoryParam(PARAM_LOGIN);
- checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
- }
+ userSession.checkLoggedIn().checkIsSystemAdministrator();
+ login = request.mandatoryParam(PARAM_LOGIN);
+ checkRequest(!login.equals(userSession.getLogin()), "Self-deactivation is not possible");
try (DbSession dbSession = dbClient.openSession(false)) {
UserDto user = dbClient.userDao().selectByLogin(dbSession, login);
}
private void deactivateUser(DbSession dbSession, UserDto user) {
- if (isSonarCloud) {
- dbClient.userDao().deactivateSonarCloudUser(dbSession, user);
- } else {
- dbClient.userDao().deactivateUser(dbSession, user);
- }
+ dbClient.userDao().deactivateUser(dbSession, user);
}
private void writeResponse(Response response, String login) {
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.db.permission.OrganizationPermission.SCAN;
import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
-import static org.sonar.process.ProcessProperties.Property.SONARCLOUD_ENABLED;
import static org.sonar.server.user.index.UserIndexDefinition.FIELD_ACTIVE;
import static org.sonar.server.user.index.UserIndexDefinition.FIELD_UUID;
import static org.sonar.test.JsonAssert.assertJson;
private DbClient dbClient = db.getDbClient();
private UserIndexer userIndexer = new UserIndexer(dbClient, es.client());
private DbSession dbSession = db.getSession();
- private MapSettings settings = new MapSettings();
private WsActionTester ws = new WsActionTester(new DeactivateAction(dbClient, userIndexer, userSession,
- new UserJsonWriter(userSession), defaultOrganizationProvider, settings.asConfig()));
+ new UserJsonWriter(userSession), defaultOrganizationProvider));
@Test
public void deactivate_user_and_delete_his_related_data() {
deactivate(user.getLogin());
- verifyThatUserIsDeactivated(user.getLogin(), false);
+ verifyThatUserIsDeactivated(user.getLogin());
assertThat(es.client().prepareSearch(UserIndexDefinition.TYPE_USER)
.setQuery(boolQuery()
.must(termQuery(FIELD_UUID, user.getUuid()))
assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(dbSession, anotherUser.getUuid(), almSettingDto)).isNotNull();
}
- @Test
- public void user_can_deactivate_itself_on_sonarcloud() {
- WsActionTester customWs = newSonarCloudWs();
-
- UserDto user = db.users().insertUser();
- userSession.logIn(user.getLogin());
-
- deactivate(customWs, user.getLogin());
-
- verifyThatUserIsDeactivated(user.getLogin(), true);
- }
-
- @Test
- public void user_cannot_deactivate_another_user_on_sonarcloud() {
- WsActionTester customWs = newSonarCloudWs();
-
- UserDto user = db.users().insertUser();
- userSession.logIn(user.getLogin());
-
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privilege");
-
- deactivate(customWs, "other user");
- }
-
@Test
public void user_cannot_deactivate_itself_on_sonarqube() {
UserDto user = db.users().insertUser();
deactivate(admin.getLogin());
- verifyThatUserIsDeactivated(admin.getLogin(), false);
+ verifyThatUserIsDeactivated(admin.getLogin());
verifyThatUserExists(anotherAdmin.getLogin());
}
assertThat(db.users().selectUserByLogin(login)).isPresent();
}
- private void verifyThatUserIsDeactivated(String login, boolean isSonarCloud) {
+ private void verifyThatUserIsDeactivated(String login) {
Optional<UserDto> user = db.users().selectUserByLogin(login);
assertThat(user).isPresent();
assertThat(user.get().isActive()).isFalse();
assertThat(user.get().getEmail()).isNull();
assertThat(user.get().getScmAccountsAsList()).isEmpty();
- if (isSonarCloud) {
- assertThat(user.get().getName()).isNull();
- }
}
- private WsActionTester newSonarCloudWs() {
- settings.setProperty(SONARCLOUD_ENABLED.getKey(), true);
- return new WsActionTester(new DeactivateAction(dbClient, userIndexer, userSession,
- new UserJsonWriter(userSession), defaultOrganizationProvider, settings.asConfig()));
- }
}