]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23594 Fix SSF-606
authorNolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com>
Thu, 14 Nov 2024 15:06:29 +0000 (16:06 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 14 Nov 2024 20:02:49 +0000 (20:02 +0000)
server/sonar-webserver-common/src/it/java/org/sonar/server/common/user/service/UserServiceIT.java
server/sonar-webserver-common/src/main/java/org/sonar/server/common/management/ManagedInstanceChecker.java
server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/service/UserService.java
server/sonar-webserver-common/src/test/java/com/sonar/server/common/management/ManagedInstanceCheckerTest.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/user/controller/DefaultUserController.java

index 58b649e1fa4ff45f36d1540072f7b4528f45a59d..14233509ae1bb3c39e021e0fcb3272832416d9cc 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.server.common.user.service;
 
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
 import java.util.Collection;
@@ -30,6 +33,7 @@ import java.util.stream.IntStream;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.server.authentication.IdentityProvider;
 import org.sonar.api.utils.DateUtils;
@@ -87,6 +91,7 @@ import static org.mockito.Mockito.when;
 import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
 import static org.sonar.db.user.UserTesting.newUserDto;
 
+@RunWith(DataProviderRunner.class)
 public class UserServiceIT {
 
   private static final UsersSearchRequest SEARCH_REQUEST = getBuilderWithDefaultsPageSize().build();
@@ -839,6 +844,25 @@ public class UserServiceIT {
     assertThat(updatedUser.getExternalId()).isEqualTo("prov_id");
     assertThat(updatedUser.getExternalLogin()).isEqualTo("prov_login");
   }
+  @DataProvider
+  public static Object[][] updateUserProvider() {
+    return new Object[][]{
+      {new UpdateUser().setName("new name")},
+      {new UpdateUser().setEmail("newEmail@test.com")},
+      {new UpdateUser().setName("new name").setEmail("newEmail@test.com")}};
+  }
+
+  @Test
+  @UseDataProvider("updateUserProvider")
+  public void updateUser_whenIncorrectPayloadForManagedInstance_shouldThrow(UpdateUser updateUser) {
+    doThrow(BadRequestException.create("message")).when(managedInstanceChecker).throwIfInstanceIsManaged(any());
+
+    UserDto userDto = db.users().insertUser();
+
+    assertThatThrownBy(() -> userService.updateUser(userDto.getUuid(), updateUser))
+      .isInstanceOf(BadRequestException.class)
+      .hasMessage("message");
+  }
 
   private void assertUserWithFilter(Function<UsersSearchRequest.Builder, UsersSearchRequest.Builder> query, String userLogin, boolean isExpectedToBeThere) {
 
index dad6f4ac6d23acdf06438ac69bd43a5b5455a953..6d007a93c3fd73d714dfb1e90295e41a4ea520f9 100644 (file)
@@ -41,6 +41,10 @@ public class ManagedInstanceChecker {
     BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), INSTANCE_EXCEPTION_MESSAGE);
   }
 
+  public void throwIfInstanceIsManaged(String errorMessage) {
+    BadRequestException.checkRequest(!managedInstanceService.isInstanceExternallyManaged(), errorMessage);
+  }
+
   public void throwIfProjectIsManaged(DbSession dbSession, String projectUuid) {
     BadRequestException.checkRequest(!managedProjectService.isProjectManaged(dbSession, projectUuid), PROJECT_EXCEPTION_MESSAGE);
   }
index ed83c9673d0ce58e6b20756d85ce3bcaab34c194..a3291090e3f1b27d302f88b24a4568512437ac03 100644 (file)
@@ -222,6 +222,7 @@ public class UserService {
   public UserInformation updateUser(String uuid, UpdateUser updateUser) {
     try (DbSession dbSession = dbClient.openSession(false)) {
       throwIfInvalidChangeOfExternalProvider(updateUser);
+      throwIfManagedInstanceAndNameOrEmailUpdated(updateUser);
       UserDto userDto = findUserOrThrow(uuid, dbSession);
       userUpdater.updateAndCommit(dbSession, userDto, updateUser, u -> {
       });
@@ -247,6 +248,14 @@ public class UserService {
     Optional.ofNullable(updateUser.externalIdentityProvider()).ifPresent(this::assertProviderIsSupported);
   }
 
+  private void throwIfManagedInstanceAndNameOrEmailUpdated(UpdateUser updateUser) {
+    boolean isNameChanged = updateUser.isNameChanged();
+    boolean isEmailDefined = updateUser.isEmailChanged();
+    if (isNameChanged || isEmailDefined) {
+      managedInstanceChecker.throwIfInstanceIsManaged("User name and email cannot be updated when the instance is externally managed");
+    }
+  }
+
   private void assertProviderIsSupported(String newExternalProvider) {
     List<String> allowedIdentityProviders = getAvailableIdentityProviders();
 
index 83b3c73270798cb879be8acc96d3d84326337404..092ac87b501d22264648f4e3aefda9366dd35c3c 100644 (file)
@@ -62,6 +62,26 @@ public class ManagedInstanceCheckerTest {
       .hasMessage(INSTANCE_EXCEPTION_MESSAGE);
   }
 
+  @Test
+  public void throwIfInstanceIsManaged_whenCustomErrorMessage_shouldThrowWithCustomError() {
+    when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(true);
+
+    String customErrorMessage = "Custom error message";
+
+    assertThatThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged(customErrorMessage))
+      .isInstanceOf(BadRequestException.class)
+      .hasMessage(customErrorMessage);
+  }
+
+  @Test
+  public void throwIfInstanceIsManaged_whenCustomErrorMessageAndInstanceManaged_shouldNotThrow() {
+    when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
+
+    String customErrorMessage = "Custom error message";
+
+    assertThatNoException().isThrownBy(() -> managedInstanceChecker.throwIfInstanceIsManaged(customErrorMessage));
+  }
+
   @Test
   public void throwIfInstanceIsManaged_whenInstanceNotExternallyManaged_shouldNotThrow() {
     when(managedInstanceService.isInstanceExternallyManaged()).thenReturn(false);
index c6da3fe0422f77feb2f260329b18b0c2294dd49e..859fc39e369d04e32cb6c063869720961924c203 100644 (file)
@@ -117,6 +117,7 @@ public class DefaultUserController implements UserController {
   @Override
   public UserRestResponse updateUser(String id, UserUpdateRestRequest updateRequest) {
     userSession.checkLoggedIn().checkIsSystemAdministrator();
+
     UpdateUser update = toUpdateUser(updateRequest);
     UserInformation updatedUser = userService.updateUser(id, update);
     return usersSearchResponseGenerator.toRestUser(updatedUser);