]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9903 Ignore SCM accounts that are duplicates when adding a user
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Oct 2017 13:08:05 +0000 (15:08 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Thu, 19 Oct 2017 10:12:11 +0000 (12:12 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java
server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/UpdateActionTest.java

index fc3dd43df6f3eab1cba25e2abdca680a8cb73e0c..1213e5bc0b7d598eef1bad5011ce66fcad97d811 100644 (file)
@@ -26,7 +26,6 @@ import java.util.List;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang.StringUtils;
 import org.sonar.core.user.DefaultUser;
 
 import static java.util.Objects.requireNonNull;
@@ -117,7 +116,7 @@ public class UserDto {
     return this;
   }
 
-  public UserDto setScmAccounts(@Nullable List list) {
+  public UserDto setScmAccounts(@Nullable List<String> list) {
     this.scmAccounts = encodeScmAccounts(list);
     return this;
   }
@@ -125,7 +124,7 @@ public class UserDto {
   @CheckForNull
   public static String encodeScmAccounts(@Nullable List<String> scmAccounts) {
     if (scmAccounts != null && !scmAccounts.isEmpty()) {
-      return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, StringUtils.join(scmAccounts, SCM_ACCOUNTS_SEPARATOR), SCM_ACCOUNTS_SEPARATOR);
+      return String.format("%s%s%s", SCM_ACCOUNTS_SEPARATOR, String.join(String.valueOf(SCM_ACCOUNTS_SEPARATOR), scmAccounts), SCM_ACCOUNTS_SEPARATOR);
     }
     return null;
   }
index cc14249f719390240b2a038fe9c0d6f0b5c42069..b87273aee3b772622c6c3becc17f171fb337f1ec 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Strings;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import java.util.Random;
@@ -33,7 +34,6 @@ import org.apache.commons.codec.digest.DigestUtils;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.platform.NewUserHandler;
 import org.sonar.api.server.ServerSide;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationMemberDto;
@@ -52,6 +52,7 @@ import static com.google.common.base.Strings.isNullOrEmpty;
 import static com.google.common.collect.Lists.newArrayList;
 import static java.lang.String.format;
 import static org.sonar.core.config.CorePropertyDefinitions.ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.db.user.UserDto.encryptPassword;
 import static org.sonar.server.ws.WsUtils.checkFound;
 import static org.sonar.server.ws.WsUtils.checkRequest;
@@ -351,7 +352,11 @@ public class UserUpdater {
 
   private static List<String> sanitizeScmAccounts(@Nullable List<String> scmAccounts) {
     if (scmAccounts != null) {
-      return scmAccounts.stream().filter(s -> !Strings.isNullOrEmpty(s)).collect(MoreCollectors.toList());
+      return new HashSet<>(scmAccounts).stream()
+        .map(Strings::emptyToNull)
+        .filter(Objects::nonNull)
+        .sorted(String::compareToIgnoreCase)
+        .collect(toList(scmAccounts.size()));
     }
     return Collections.emptyList();
   }
index 03d42b052457bf14e1c75ff502bb5b806b8d70fd..c8926316b7cf79c3d47edc7e771e35a7ae1b4e2e 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.user.ws;
 
+import java.util.Arrays;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -169,6 +170,32 @@ public class UpdateActionTest {
     assertThat(user.getScmAccountsAsList()).containsOnly("jon,snow");
   }
 
+  @Test
+  public void update_scm_account_ignores_duplicates() throws Exception {
+    createUser();
+
+    ws.newRequest()
+      .setParam("login", "john")
+      .setMultiParam("scmAccount", Arrays.asList("jon.snow", "jon.snow", "jon.jon", "jon.snow"))
+      .execute();
+
+    UserDto user = dbClient.userDao().selectByLogin(dbSession, "john");
+    assertThat(user.getScmAccountsAsList()).containsExactlyInAnyOrder("jon.jon", "jon.snow");
+  }
+
+  @Test
+  public void update_scm_account_ordered_case_insensitive() throws Exception {
+    createUser();
+
+    ws.newRequest()
+      .setParam("login", "john")
+      .setMultiParam("scmAccount", Arrays.asList("jon.3", "Jon.1", "JON.2"))
+      .execute();
+
+    UserDto user = dbClient.userDao().selectByLogin(dbSession, "john");
+    assertThat(user.getScmAccountsAsList()).containsExactly("Jon.1", "JON.2", "jon.3");
+  }
+
   @Test
   public void update_only_scm_accounts_with_deprecated_scmAccounts_parameter() throws Exception {
     createUser();