]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18534 Support excludedAttributes=members for SCIM groups requests
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Tue, 14 Mar 2023 15:00:25 +0000 (16:00 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:07 +0000 (20:04 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/scim/ScimUserDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/scim/ScimUserQuery.java
server/sonar-db-dao/src/main/resources/org/sonar/db/scim/ScimUserMapper.xml

index 701f99684ef9a1c3c9f4c0d72e82d241a8427e61..2fae3b3e5185a9fb92c329058134729a8257c21b 100644 (file)
@@ -22,11 +22,13 @@ package org.sonar.db.scim;
 import com.tngtech.java.junit.dataprovider.DataProvider;
 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import org.junit.Rule;
@@ -34,6 +36,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
@@ -198,6 +201,36 @@ public class ScimUserDaoIT {
     assertThat(scimUsersUuids).containsExactlyElementsOf(expectedScimUserUuids);
   }
 
+  @Test
+  public void findScimUsers_whenFilteringByGroupUuid_shouldReturnTheExpectedScimUsers() {
+    List<ScimUserTestData> scimUsersTestData = insertScimUsersWithUsers(List.of("userAInGroupA", "userBInGroupA", "userAInGroupB", "userNotInGroup"));
+    Map<String, ScimUserTestData> users = scimUsersTestData.stream()
+      .collect(Collectors.toMap(testData -> testData.getUserDto().getExternalId(), Function.identity()));
+
+    GroupDto group1dto = createGroupWithUsers(users.get("userAInGroupA"), users.get("userBInGroupA"));
+    createGroupWithUsers(users.get("userAInGroupB"));
+
+    ScimUserQuery query = ScimUserQuery.builder().groupUuid(group1dto.getUuid()).build();
+
+    List<ScimUserDto> scimUsers = scimUserDao.findScimUsers(dbSession, query, 0, 100);
+
+    List<String> scimUsersUuids = toScimUsersUuids(scimUsers);
+    assertThat(scimUsersUuids).containsExactlyInAnyOrder(
+      users.get("userAInGroupA").getScimUserUuid(),
+      users.get("userBInGroupA").getScimUserUuid()
+    );
+  }
+
+  private GroupDto createGroupWithUsers(ScimUserTestData... testUsers) {
+    GroupDto group = db.users().insertGroup();
+
+    UserDto[] userDtos = Arrays.stream(testUsers)
+      .map(ScimUserTestData::getUserDto)
+      .toArray(UserDto[]::new);
+    db.users().insertMembers(group, userDtos);
+    return group;
+  }
+
   @Test
   public void findScimUsers_whenFilteringByScimUuidsWithLongRange_shouldReturnTheExpectedScimUsers() {
     generateScimUsers(3000);
@@ -310,7 +343,9 @@ public class ScimUserDaoIT {
 
   private ScimUserTestData insertScimUserWithUser(String userLogin, String scimUuid) {
     UserDto userDto = db.users().insertUser(u -> u.setExternalId(userLogin));
-    return insertScimUser(scimUuid, userDto.getUuid());
+    ScimUserTestData scimUserTestData = insertScimUser(scimUuid, userDto.getUuid());
+    scimUserTestData.setUserDto(userDto);
+    return scimUserTestData;
   }
 
   private ScimUserTestData insertScimUser(String scimUserUuid) {
@@ -350,6 +385,7 @@ public class ScimUserDaoIT {
 
     private final String scimUserUuid;
     private final String userUuid;
+    private UserDto userDto;
 
     private ScimUserTestData(String scimUserUuid, String userUuid) {
       this.scimUserUuid = scimUserUuid;
@@ -364,6 +400,14 @@ public class ScimUserDaoIT {
       return userUuid;
     }
 
+    private UserDto getUserDto() {
+      return userDto;
+    }
+
+    private void setUserDto(UserDto userDto) {
+      this.userDto = userDto;
+    }
+
     @Override
     public boolean equals(Object o) {
       if (this == o)
index f428a62590215c42ddc8b12ee1d9b696387d302e..43827e664c2557cc3019168eebdc03e68325526e 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 
-import static com.google.common.base.Preconditions.checkState;
+import static org.sonar.api.utils.Preconditions.checkState;
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
 
 public class ScimUserDao implements Dao {
index 99ba5e05211829d0fb26c93d1818c2fedc2abae0..62cac3ea5ddbb102b9120557cdc2ef0823406245 100644 (file)
@@ -36,11 +36,14 @@ public class ScimUserQuery {
   private final String userName;
   private final Set<String> scimUserUuids;
   private final Set<String> userUuids;
+  private final String groupUuid;
 
-  private ScimUserQuery(@Nullable String userName, @Nullable Set<String> scimUserUuids, @Nullable Set<String> userUuids) {
+  private ScimUserQuery(@Nullable String userName, @Nullable Set<String> scimUserUuids,
+    @Nullable Set<String> userUuids, @Nullable String groupUuid) {
     this.userName = userName;
     this.scimUserUuids = scimUserUuids;
     this.userUuids = userUuids;
+    this.groupUuid = groupUuid;
   }
 
   @CheckForNull
@@ -58,6 +61,11 @@ public class ScimUserQuery {
     return userUuids;
   }
 
+  @CheckForNull
+  public String getGroupUuid() {
+    return groupUuid;
+  }
+
   public static ScimUserQuery empty() {
     return builder().build();
   }
@@ -89,6 +97,7 @@ public class ScimUserQuery {
     private String userName;
     private Set<String> scimUserUuids;
     private Set<String> userUuids;
+    private String groupUuid;
 
     private ScimUserQueryBuilder() {
     }
@@ -109,8 +118,13 @@ public class ScimUserQuery {
       return this;
     }
 
+    public ScimUserQueryBuilder groupUuid(String groupUuid) {
+      this.groupUuid = groupUuid;
+      return this;
+    }
+
     public ScimUserQuery build() {
-      return new ScimUserQuery(userName, scimUserUuids, userUuids);
+      return new ScimUserQuery(userName, scimUserUuids, userUuids, groupUuid);
     }
   }
 }
index a973a535c6bb1359d6d2c86ce9ddaf0322023e0e..44965cb925d6af55924cabd9aadf48d40fd2b47b 100644 (file)
@@ -4,20 +4,20 @@
 <mapper namespace="org.sonar.db.scim.ScimUserMapper">
 
   <sql id="scimUsersColumns">
-      scim_uuid as scimUserUuid,
-      user_uuid as userUuid
+      s.scim_uuid as scimUserUuid,
+      s.user_uuid as userUuid
   </sql>
 
   <select id="findAll" resultType="org.sonar.db.scim.ScimUserDto">
     select
     <include refid="scimUsersColumns"/>
-      from scim_users
+      from scim_users s
   </select>
 
   <select id="findByScimUuid" parameterType="String" resultType="org.sonar.db.scim.ScimUserDto">
     select
     <include refid="scimUsersColumns"/>
-      from scim_users
+      from scim_users s
     where
       scim_uuid = #{scimUserUuid,jdbcType=VARCHAR}
   </select>
@@ -25,7 +25,7 @@
   <select id="selectByLogins" parameterType="string" resultType="User">
     select
     <include refid="scimUsersColumns"/>
-      from scim_users
+      from scim_users s
     where
       scim_uuid in
         <foreach collection="list" open="(" close=")" item="login" separator=",">
@@ -36,7 +36,7 @@
   <select id="findByUserUuid" parameterType="String" resultType="org.sonar.db.scim.ScimUserDto">
     select
     <include refid="scimUsersColumns"/>
-      from scim_users
+      from scim_users s
     where
       user_uuid = #{userUuid,jdbcType=VARCHAR}
   </select>
@@ -66,6 +66,9 @@
   <sql id="sqlSelectByQuery">
     from scim_users s
     inner join users u on u.uuid=s.user_uuid
+    <if test="query.groupUuid != null">
+      left join groups_users gu on gu.user_uuid = u.uuid
+    </if>
     where 1=1
     <if test="query.userName != null">
         and lower(u.external_id) like lower(#{query.userName,jdbcType=VARCHAR}) escape '/'
@@ -82,6 +85,9 @@
             #{userUuid, jdbcType=VARCHAR}
         </foreach>
     </if>
+    <if test="query.groupUuid != null">
+        and gu.group_uuid = #{query.groupUuid, jdbcType=VARCHAR}
+    </if>
   </sql>
 
   <delete id="deleteByUserUuid" parameterType="String">