.orElseThrow(() -> NOT_MANAGED_INSTANCE_EXCEPTION);
}
+ @Override
+ public boolean isUserManaged(DbSession dbSession, String login) {
+ return findManagedInstanceService()
+ .map(managedInstanceService -> managedInstanceService.isUserManaged(dbSession, login))
+ .orElse(false);
+ }
+
private Optional<ManagedInstanceService> findManagedInstanceService() {
Set<ManagedInstanceService> managedInstanceServices = delegates.stream()
.filter(ManagedInstanceService::isInstanceExternallyManaged)
String getManagedUsersSqlFilter(boolean filterByManaged);
String getManagedGroupsSqlFilter(boolean filterByManaged);
+
+ boolean isUserManaged(DbSession dbSession, String login);
}
assertThat(groupUuidToManaged).containsExactlyInAnyOrderEntriesOf(Map.of("a", false, "b", false));
}
+ @Test
+ public void isUserManaged_delegatesToRightService_andPropagateAnswer() {
+ DelegatingManagedInstanceService managedInstanceService = new DelegatingManagedInstanceService(Set.of(new NeverManagedInstanceService(), new AlwaysManagedInstanceService()));
+
+ assertThat(managedInstanceService.isUserManaged(dbSession, "login")).isTrue();
+ }
+
+ @Test
+ public void isUserManaged_whenNoDelegates_returnsFalse() {
+ DelegatingManagedInstanceService managedInstanceService = new DelegatingManagedInstanceService(Set.of());
+
+ assertThat(managedInstanceService.isUserManaged(dbSession, "login")).isFalse();
+ }
+
@Test
public void getGroupUuidToManaged_delegatesToRightService_andPropagateAnswer() {
Set<String> groupUuids = Set.of("a", "b");
public String getManagedGroupsSqlFilter(boolean filterByManaged) {
return null;
}
+
+ @Override
+ public boolean isUserManaged(DbSession dbSession, String login) {
+ return false;
+ }
}
private static class AlwaysManagedInstanceService implements ManagedInstanceService {
public String getManagedGroupsSqlFilter(boolean filterByManaged) {
return "any filter";
}
+
+ @Override
+ public boolean isUserManaged(DbSession dbSession, String login) {
+ return true;
+ }
}
}
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
-import org.sonar.server.management.ManagedInstanceChecker;
+import org.sonar.server.management.ManagedInstanceService;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.ExternalIdentity;
import org.sonar.server.ws.TestRequest;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.mockito.Mockito.doThrow;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
import static org.sonar.test.JsonAssert.assertJson;
private final DbSession dbSession = db.getSession();
private final UserAnonymizer userAnonymizer = new UserAnonymizer(db.getDbClient(), () -> "anonymized");
private final UserDeactivator userDeactivator = new UserDeactivator(dbClient, userAnonymizer);
- private final ManagedInstanceChecker managedInstanceChecker = mock(ManagedInstanceChecker.class);
- private final WsActionTester ws = new WsActionTester(new DeactivateAction(dbClient, userSession, new UserJsonWriter(userSession), userDeactivator, managedInstanceChecker));
+ private final ManagedInstanceService managedInstanceService = mock(ManagedInstanceService.class);
+
+ private final WsActionTester ws = new WsActionTester(new DeactivateAction(dbClient, userSession, new UserJsonWriter(userSession), userDeactivator, managedInstanceService
+ ));
@Test
public void deactivate_user_and_delete_their_related_data() {
@Test
public void handle_whenUserManagedAndInstanceManaged_shouldThrowBadRequestException() {
- BadRequestException badRequestException = BadRequestException.create("message");
- doThrow(badRequestException).when(managedInstanceChecker).throwIfInstanceIsManaged();
-
createAdminUser();
logInAsSystemAdministrator();
- UserDto user = db.users().insertUser(u -> u.setLocal(false));
+ UserDto user = db.users().insertUser();
+ when(managedInstanceService.isUserManaged(any(), eq(user.getLogin()))).thenReturn(true);
- assertThatThrownBy(() -> deactivate(user.getLogin()))
- .isEqualTo(badRequestException);
+ assertThatExceptionOfType(BadRequestException.class)
+ .isThrownBy(() -> deactivate(user.getLogin()))
+ .withMessage("Operation not allowed when the instance is externally managed.");
}
@Test
assertThatThrownBy(() -> deactivate(login))
.isInstanceOf(UnauthorizedException.class)
.hasMessage("Authentication is required");
- verify(managedInstanceChecker, never()).throwIfInstanceIsManaged();
}
private void logInAsSystemAdministrator() {
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
-import org.sonar.server.management.ManagedInstanceChecker;
+import org.sonar.server.management.ManagedInstanceService;
import org.sonar.server.user.UserSession;
import static java.util.Collections.singletonList;
private final UserSession userSession;
private final UserJsonWriter userWriter;
private final UserDeactivator userDeactivator;
- private final ManagedInstanceChecker managedInstanceChecker;
+ private final ManagedInstanceService managedInstanceService;
public DeactivateAction(DbClient dbClient, UserSession userSession, UserJsonWriter userWriter,
- UserDeactivator userDeactivator, ManagedInstanceChecker managedInstanceChecker) {
+ UserDeactivator userDeactivator, ManagedInstanceService managedInstanceService) {
this.dbClient = dbClient;
this.userSession = userSession;
this.userWriter = userWriter;
this.userDeactivator = userDeactivator;
- this.managedInstanceChecker = managedInstanceChecker;
+ this.managedInstanceService = managedInstanceService;
}
@Override
}
private void preventManagedUserDeactivationIfManagedInstance(DbSession dbSession, String login) {
- UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
- if (userDto != null && !userDto.isLocal()) {
- managedInstanceChecker.throwIfInstanceIsManaged();
- }
+ checkRequest(!managedInstanceService.isUserManaged(dbSession, login), "Operation not allowed when the instance is externally managed.");
}
private void writeResponse(Response response, String login) {