diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2021-10-13 16:35:52 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-10-22 20:03:27 +0000 |
commit | 029e98315813bf42740329c63f6b54c72044fe92 (patch) | |
tree | edb4a14fc000a499643252492ac42c8fcde91991 /server/sonar-db-dao | |
parent | dd6441108cb87bb35bd6247c160736a9b2835c5a (diff) | |
download | sonarqube-029e98315813bf42740329c63f6b54c72044fe92.tar.gz sonarqube-029e98315813bf42740329c63f6b54c72044fe92.zip |
SONAR-15514 Create api/qualitygates/search_users service
Diffstat (limited to 'server/sonar-db-dao')
19 files changed, 327 insertions, 200 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java index 76b901601a7..a95d8f5235f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDao.java @@ -27,7 +27,7 @@ import org.sonar.db.DbSession; import org.sonar.db.Pagination; import org.sonar.db.user.GroupDto; import org.sonar.db.user.SearchGroupMembershipDto; -import org.sonar.db.user.SearchGroupsQuery; +import org.sonar.db.user.SearchPermissionQuery; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -61,11 +61,11 @@ public class QualityGateGroupPermissionsDao implements Dao { return dbSession.getMapper(QualityGateGroupPermissionsMapper.class); } - public List<SearchGroupMembershipDto> selectByQuery(DbSession dbSession, SearchGroupsQuery query, Pagination pagination) { + public List<SearchGroupMembershipDto> selectByQuery(DbSession dbSession, SearchPermissionQuery query, Pagination pagination) { return mapper(dbSession).selectByQuery(query, pagination); } - public int countByQuery(DbSession dbSession, SearchGroupsQuery query) { + public int countByQuery(DbSession dbSession, SearchPermissionQuery query) { return mapper(dbSession).countByQuery(query); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java index b7c23e1fa99..2d0800155cf 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsMapper.java @@ -23,7 +23,7 @@ import java.util.List; import org.apache.ibatis.annotations.Param; import org.sonar.db.Pagination; import org.sonar.db.user.SearchGroupMembershipDto; -import org.sonar.db.user.SearchGroupsQuery; +import org.sonar.db.user.SearchPermissionQuery; public interface QualityGateGroupPermissionsMapper { @@ -33,7 +33,7 @@ public interface QualityGateGroupPermissionsMapper { void insert(@Param("dto") QualityGateGroupPermissionsDto dto, @Param("now") long now); - List<SearchGroupMembershipDto> selectByQuery(@Param("query") SearchGroupsQuery query, @Param("pagination") Pagination pagination); + List<SearchGroupMembershipDto> selectByQuery(@Param("query") SearchPermissionQuery query, @Param("pagination") Pagination pagination); - int countByQuery(@Param("query") SearchGroupsQuery query); + int countByQuery(@Param("query") SearchPermissionQuery query); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java index e79dc9ecfde..6bc72474a47 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java @@ -19,10 +19,14 @@ */ package org.sonar.db.qualitygate; +import java.util.List; import javax.annotation.Nullable; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.Pagination; +import org.sonar.db.user.SearchPermissionQuery; +import org.sonar.db.user.SearchUserMembershipDto; import org.sonar.db.user.UserDto; public class QualityGateUserPermissionsDao implements Dao { @@ -52,6 +56,14 @@ public class QualityGateUserPermissionsDao implements Dao { mapper(dbSession).insert(dto, system2.now()); } + public List<SearchUserMembershipDto> selectByQuery(DbSession dbSession, SearchPermissionQuery query, Pagination pagination) { + return mapper(dbSession).selectByQuery(query, pagination); + } + + public int countByQuery(DbSession dbSession, SearchPermissionQuery query) { + return mapper(dbSession).countByQuery(query); + } + private static QualityGateUserPermissionsMapper mapper(DbSession dbSession) { return dbSession.getMapper(QualityGateUserPermissionsMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java index b67289f69dc..3f28de143eb 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java @@ -19,7 +19,11 @@ */ package org.sonar.db.qualitygate; +import java.util.List; import org.apache.ibatis.annotations.Param; +import org.sonar.db.Pagination; +import org.sonar.db.user.SearchPermissionQuery; +import org.sonar.db.user.SearchUserMembershipDto; public interface QualityGateUserPermissionsMapper { @@ -27,4 +31,7 @@ public interface QualityGateUserPermissionsMapper { void insert(@Param("dto") QualityGateUserPermissionsDto dto, @Param("now") long now); + List<SearchUserMembershipDto> selectByQuery(@Param("query") SearchPermissionQuery query, @Param("pagination") Pagination pagination); + + int countByQuery(@Param("query") SearchPermissionQuery query); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/SearchQualityGateGroupsQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/SearchQualityGatePermissionQuery.java index 7bfeef9bc2a..c0a556606dc 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/SearchQualityGateGroupsQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/SearchQualityGatePermissionQuery.java @@ -20,20 +20,22 @@ package org.sonar.db.qualitygate; import java.util.Locale; -import org.sonar.db.user.SearchGroupsQuery; +import org.sonar.db.user.SearchPermissionQuery; +import static java.util.Objects.requireNonNull; import static org.sonar.db.DaoUtils.buildLikeValue; import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER; -public class SearchQualityGateGroupsQuery extends SearchGroupsQuery { +public class SearchQualityGatePermissionQuery extends SearchPermissionQuery { private final String qualityGateUuid; - public SearchQualityGateGroupsQuery(Builder builder) { + public SearchQualityGatePermissionQuery(Builder builder) { this.qualityGateUuid = builder.qualityGate.getUuid(); this.query = builder.getQuery(); this.membership = builder.getMembership(); - this.querySqlLowercase = query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER).toLowerCase(Locale.ENGLISH); + this.querySql = query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER); + this.querySqlLowercase = querySql == null ? null : querySql.toLowerCase(Locale.ENGLISH); } public String getQualityGateUuid() { @@ -44,7 +46,7 @@ public class SearchQualityGateGroupsQuery extends SearchGroupsQuery { return new Builder(); } - public static class Builder extends SearchGroupsQuery.Builder<Builder> { + public static class Builder extends SearchPermissionQuery.Builder<Builder> { private QualityGateDto qualityGate; public Builder setQualityGate(QualityGateDto qualityGate) { @@ -52,9 +54,10 @@ public class SearchQualityGateGroupsQuery extends SearchGroupsQuery { return this; } - public SearchQualityGateGroupsQuery build() { + public SearchQualityGatePermissionQuery build() { + requireNonNull(qualityGate, "Quality gate cant be null."); initMembership(); - return new SearchQualityGateGroupsQuery(this); + return new SearchQualityGatePermissionQuery(this); } } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java index 8b324e9c898..484a406dd0d 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsDao.java @@ -51,11 +51,11 @@ public class QProfileEditGroupsDao implements Dao { .isEmpty(); } - public int countByQuery(DbSession dbSession, SearchQualityProfileGroupsQuery query) { + public int countByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query) { return mapper(dbSession).countByQuery(query); } - public List<SearchGroupMembershipDto> selectByQuery(DbSession dbSession, SearchQualityProfileGroupsQuery query, Pagination pagination) { + public List<SearchGroupMembershipDto> selectByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query, Pagination pagination) { return mapper(dbSession).selectByQuery(query, pagination); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java index d8cb1e723e6..e849f4c984b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditGroupsMapper.java @@ -29,9 +29,9 @@ public interface QProfileEditGroupsMapper { List<QProfileEditGroupsDto> selectByQProfileAndGroups(@Param("qProfileUuid") String qProfileUuid, @Param("groupUuids") List<String> groupUuids); - int countByQuery(@Param("query") SearchQualityProfileGroupsQuery query); + int countByQuery(@Param("query") SearchQualityProfilePermissionQuery query); - List<SearchGroupMembershipDto> selectByQuery(@Param("query") SearchQualityProfileGroupsQuery query, @Param("pagination") Pagination pagination); + List<SearchGroupMembershipDto> selectByQuery(@Param("query") SearchQualityProfilePermissionQuery query, @Param("pagination") Pagination pagination); List<String> selectQProfileUuidsByGroups(@Param("groupUuids") List<String> groupUuids); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java index 3b66fbeb452..911867520a1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java @@ -24,6 +24,7 @@ import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.Pagination; +import org.sonar.db.user.SearchUserMembershipDto; import org.sonar.db.user.UserDto; import static org.sonar.core.util.stream.MoreCollectors.toList; @@ -41,15 +42,15 @@ public class QProfileEditUsersDao implements Dao { return mapper(dbSession).selectByQProfileAndUser(profile.getKee(), user.getUuid()) != null; } - public int countByQuery(DbSession dbSession, SearchUsersQuery query) { + public int countByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query) { return mapper(dbSession).countByQuery(query); } - public List<UserMembershipDto> selectByQuery(DbSession dbSession, SearchUsersQuery query, Pagination pagination) { + public List<SearchUserMembershipDto> selectByQuery(DbSession dbSession, SearchQualityProfilePermissionQuery query, Pagination pagination) { return mapper(dbSession).selectByQuery(query, pagination); } - public List<String> selectQProfileUuidsByUser(DbSession dbSession,UserDto userDto) { + public List<String> selectQProfileUuidsByUser(DbSession dbSession, UserDto userDto) { return mapper(dbSession).selectQProfileUuidsByUser(userDto.getUuid()); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java index e3b24df061b..fdd09a5cc8b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java @@ -23,14 +23,15 @@ import java.util.Collection; import java.util.List; import org.apache.ibatis.annotations.Param; import org.sonar.db.Pagination; +import org.sonar.db.user.SearchUserMembershipDto; public interface QProfileEditUsersMapper { QProfileEditUsersDto selectByQProfileAndUser(@Param("qProfileUuid") String qProfileUuid, @Param("userUuid") String userUuid); - int countByQuery(@Param("query") SearchUsersQuery query); + int countByQuery(@Param("query") SearchQualityProfilePermissionQuery query); - List<UserMembershipDto> selectByQuery(@Param("query") SearchUsersQuery query, @Param("pagination") Pagination pagination); + List<SearchUserMembershipDto> selectByQuery(@Param("query") SearchQualityProfilePermissionQuery query, @Param("pagination") Pagination pagination); List<String> selectQProfileUuidsByUser(@Param("userUuid") String userUuid); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchQualityProfileGroupsQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchQualityProfilePermissionQuery.java index cdbe85eae89..49e7a0e9d7c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchQualityProfileGroupsQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchQualityProfilePermissionQuery.java @@ -20,20 +20,22 @@ package org.sonar.db.qualityprofile; import java.util.Locale; -import org.sonar.db.user.SearchGroupsQuery; +import org.sonar.db.user.SearchPermissionQuery; +import static java.util.Objects.requireNonNull; import static org.sonar.db.DaoUtils.buildLikeValue; import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER; -public class SearchQualityProfileGroupsQuery extends SearchGroupsQuery { +public class SearchQualityProfilePermissionQuery extends SearchPermissionQuery { private final String qProfileUuid; - public SearchQualityProfileGroupsQuery(Builder builder) { + public SearchQualityProfilePermissionQuery(Builder builder) { this.qProfileUuid = builder.profile.getKee(); this.query = builder.getQuery(); this.membership = builder.getMembership(); - this.querySqlLowercase = query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER).toLowerCase(Locale.ENGLISH); + this.querySql = query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER); + this.querySqlLowercase = querySql == null ? null : querySql.toLowerCase(Locale.ENGLISH); } public String getQProfileUuid() { @@ -44,7 +46,7 @@ public class SearchQualityProfileGroupsQuery extends SearchGroupsQuery { return new Builder(); } - public static class Builder extends SearchGroupsQuery.Builder<Builder> { + public static class Builder extends SearchPermissionQuery.Builder<Builder> { private QProfileDto profile; public Builder setProfile(QProfileDto profile) { @@ -52,9 +54,10 @@ public class SearchQualityProfileGroupsQuery extends SearchGroupsQuery { return this; } - public SearchQualityProfileGroupsQuery build() { + public SearchQualityProfilePermissionQuery build() { + requireNonNull(profile, "Quality profile cant be null."); initMembership(); - return new SearchQualityProfileGroupsQuery(this); + return new SearchQualityProfilePermissionQuery(this); } } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchUsersQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchUsersQuery.java deleted file mode 100644 index 7a60fc34e93..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/SearchUsersQuery.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2021 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.db.qualityprofile; - -import com.google.common.collect.ImmutableSet; -import java.util.Locale; -import java.util.Set; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; - -import static com.google.common.base.MoreObjects.firstNonNull; -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Objects.requireNonNull; -import static org.sonar.db.DaoUtils.buildLikeValue; -import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER; - -public class SearchUsersQuery { - - public static final String ANY = "ANY"; - public static final String IN = "IN"; - public static final String OUT = "OUT"; - public static final Set<String> AVAILABLE_MEMBERSHIPS = ImmutableSet.of(ANY, IN, OUT); - - private final String qProfileUuid; - private final String query; - private final String membership; - - // for internal use in MyBatis - final String querySql; - final String querySqlLowercase; - - private SearchUsersQuery(Builder builder) { - this.qProfileUuid = builder.profile.getKee(); - this.query = builder.query; - this.membership = builder.membership; - this.querySql = query == null ? null : buildLikeValue(query, BEFORE_AND_AFTER); - this.querySqlLowercase = querySql == null ? null : querySql.toLowerCase(Locale.ENGLISH); - } - - public String getQProfileUuid() { - return qProfileUuid; - } - - public String getMembership() { - return membership; - } - - @CheckForNull - public String getQuery() { - return query; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private QProfileDto profile; - private String query; - private String membership; - - private Builder() { - } - - public Builder setProfile(QProfileDto profile) { - this.profile = profile; - return this; - } - - public Builder setMembership(@Nullable String membership) { - this.membership = membership; - return this; - } - - public Builder setQuery(@Nullable String s) { - this.query = StringUtils.defaultIfBlank(s, null); - return this; - } - - private void initMembership() { - membership = firstNonNull(membership, ANY); - checkArgument(AVAILABLE_MEMBERSHIPS.contains(membership), - "Membership is not valid (got " + membership + "). Availables values are " + AVAILABLE_MEMBERSHIPS); - } - - public SearchUsersQuery build() { - requireNonNull(profile, "Quality profile cant be null."); - initMembership(); - return new SearchUsersQuery(this); - } - } -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchGroupsQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchPermissionQuery.java index fd45b7beacd..5670acf270c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchGroupsQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchPermissionQuery.java @@ -27,7 +27,7 @@ import org.apache.commons.lang.StringUtils; import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; -public abstract class SearchGroupsQuery { +public abstract class SearchPermissionQuery { public static final String ANY = "ANY"; public static final String IN = "IN"; @@ -38,6 +38,7 @@ public abstract class SearchGroupsQuery { protected String membership; // for internal use in MyBatis + protected String querySql; protected String querySqlLowercase; public String getMembership() { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/UserMembershipDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchUserMembershipDto.java index b33ecd5e9f6..68629141cc2 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/UserMembershipDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/SearchUserMembershipDto.java @@ -17,19 +17,23 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.db.qualityprofile; +package org.sonar.db.user; -public class UserMembershipDto { +public class SearchUserMembershipDto { private String userUuid; // Set by MyBatis private String uuid; + public SearchUserMembershipDto() { + // Do nothing + } + public String getUserUuid() { return userUuid; } - public UserMembershipDto setUserUuid(String userUuid) { + public SearchUserMembershipDto setUserUuid(String userUuid) { this.userUuid = userUuid; return this; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml index 411e29b1904..f4ce29b422c 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml @@ -32,6 +32,65 @@ ) </insert> + <select id="countByQuery" resultType="int"> + select count(u.uuid) + <include refid="sqlSelectByQuery"/> + </select> + + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto"> + SELECT u.uuid as userUuid, u.name as name, qup.uuid as uuid + <include refid="sqlSelectByQuery"/> + ORDER BY u.name ASC + LIMIT #{pagination.pageSize,jdbcType=INTEGER} + OFFSET #{pagination.offset,jdbcType=INTEGER} + </select> + + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto" + databaseId="mssql"> + select * from ( + select row_number() over(order by u.name asc) as number, + u.uuid as userUuid, u.name as name, qup.uuid as uuid + <include refid="sqlSelectByQuery"/> + ) as query + where + query.number between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER} + order by query.name asc + </select> + + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto" + databaseId="oracle"> + select * from ( + select rownum as rn, t.* from ( + select u.uuid as userUuid, u.name as name, qup.uuid as uuid + <include refid="sqlSelectByQuery"/> + order by u.name ASC + ) t + ) t + where + t.rn between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER} + </select> + + <sql id="sqlSelectByQuery"> + FROM users u + LEFT JOIN qgate_user_permissions qup ON qup.user_uuid=u.uuid AND + qup.quality_gate_uuid=#{query.qualityGateUuid, jdbcType=VARCHAR} + <where> + <choose> + <when test="query.getMembership() == 'IN'"> + AND qup.uuid IS NOT NULL + </when> + <when test="query.getMembership() == 'OUT'"> + AND qup.uuid IS NULL + </when> + </choose> + <if test="query.getQuery() != null"> + AND ( + lower(u.name) like #{query.querySqlLowercase} ESCAPE '/' + or u.login like #{query.querySql} ESCAPE '/') + </if> + AND u.active=${_true} + </where> + </sql> </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml index 67daf154734..74b3b512804 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml @@ -23,7 +23,7 @@ <include refid="sqlSelectByQuery" /> </select> - <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.qualityprofile.UserMembershipDto"> + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto"> SELECT u.uuid as userUuid, u.name as name, qeu.uuid as uuid <include refid="sqlSelectByQuery"/> ORDER BY u.name ASC @@ -31,24 +31,26 @@ OFFSET #{pagination.offset,jdbcType=INTEGER} </select> - <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.qualityprofile.UserMembershipDto" databaseId="mssql"> + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto" + databaseId="mssql"> select * from ( select row_number() over(order by u.name asc) as number, - u.uuid as userUuid, u.name as name, qeu.uuid as uuid - <include refid="sqlSelectByQuery" /> + u.uuid as userUuid, u.name as name, qeu.uuid as uuid + <include refid="sqlSelectByQuery"/> ) as query where query.number between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER} order by query.name asc </select> - <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.qualityprofile.UserMembershipDto" databaseId="oracle"> + <select id="selectByQuery" parameterType="map" resultType="org.sonar.db.user.SearchUserMembershipDto" + databaseId="oracle"> select * from ( - select rownum as rn, t.* from ( - select u.uuid as userUuid, u.name as name, qeu.uuid as uuid - <include refid="sqlSelectByQuery"/> - order by u.name ASC - ) t + select rownum as rn, t.* from ( + select u.uuid as userUuid, u.name as name, qeu.uuid as uuid + <include refid="sqlSelectByQuery"/> + order by u.name ASC + ) t ) t where t.rn between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java index 0acdca11c79..1f8d10b9a86 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateGroupPermissionsDaoTest.java @@ -35,10 +35,10 @@ import org.sonar.db.user.SearchGroupMembershipDto; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; -import static org.sonar.db.qualitygate.SearchQualityGateGroupsQuery.builder; -import static org.sonar.db.user.SearchGroupsQuery.ANY; -import static org.sonar.db.user.SearchGroupsQuery.IN; -import static org.sonar.db.user.SearchGroupsQuery.OUT; +import static org.sonar.db.qualitygate.SearchQualityGatePermissionQuery.builder; +import static org.sonar.db.user.SearchPermissionQuery.ANY; +import static org.sonar.db.user.SearchPermissionQuery.IN; +import static org.sonar.db.user.SearchPermissionQuery.OUT; public class QualityGateGroupPermissionsDaoTest { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java index 1ce212780c9..406db12f17b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java @@ -24,10 +24,17 @@ import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.Pagination; +import org.sonar.db.user.SearchUserMembershipDto; import org.sonar.db.user.UserDbTester; import org.sonar.db.user.UserDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.db.qualitygate.SearchQualityGatePermissionQuery.builder; +import static org.sonar.db.user.SearchPermissionQuery.ANY; +import static org.sonar.db.user.SearchPermissionQuery.IN; +import static org.sonar.db.user.SearchPermissionQuery.OUT; public class QualityGateUserPermissionsDaoTest { @Rule @@ -54,14 +61,150 @@ public class QualityGateUserPermissionsDaoTest { @Test public void exist() { - UserDto user = userDbTester.insertUser(); + UserDto user1 = userDbTester.insertUser(); + UserDto user2 = userDbTester.insertUser(); QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate(); - QualityGateUserPermissionsDto qualityGateUserPermissions = new QualityGateUserPermissionsDto("uuid", user.getUuid(), qualityGate.getUuid()); + QualityGateUserPermissionsDto qualityGateUserPermissions = new QualityGateUserPermissionsDto("uuid", user1.getUuid(), qualityGate.getUuid()); underTest.insert(dbSession, qualityGateUserPermissions); dbSession.commit(); - assertThat(underTest.exists(dbSession, qualityGate.getUuid(), user.getUuid())).isTrue(); - assertThat(underTest.exists(dbSession, qualityGate, user)).isTrue(); + assertThat(underTest.exists(dbSession, qualityGate.getUuid(), user1.getUuid())).isTrue(); + assertThat(underTest.exists(dbSession, qualityGate, user1)).isTrue(); + assertThat(underTest.exists(dbSession, qualityGate.getUuid(), user2.getUuid())).isFalse(); + assertThat(underTest.exists(dbSession, qualityGate, user2)).isFalse(); + } + + @Test + public void exist_can_handle_null_param_and_return_false() { + assertThat(underTest.exists(dbSession, "uuid", null)).isFalse(); + assertThat(underTest.exists(dbSession, null, "uuid")).isFalse(); + } + + @Test + public void countByQuery() { + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + UserDto user1 = db.users().insertUser(); + UserDto user2 = db.users().insertUser(); + UserDto user3 = db.users().insertUser(); + db.qualityGates().addUserPermission(qualityGate, user1); + db.qualityGates().addUserPermission(qualityGate, user2); + + assertThat(underTest.countByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(ANY).build())) + .isEqualTo(3); + + assertThat(underTest.countByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(IN).build())) + .isEqualTo(2); + + assertThat(underTest.countByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(OUT).build())) + .isEqualTo(1); + } + + @Test + public void selectByQuery() { + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + UserDto user1 = db.users().insertUser(); + UserDto user2 = db.users().insertUser(); + UserDto user3 = db.users().insertUser(); + db.qualityGates().addUserPermission(qualityGate, user1); + db.qualityGates().addUserPermission(qualityGate, user2); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(ANY).build(), Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) + .containsExactlyInAnyOrder( + tuple(user1.getUuid(), true), + tuple(user2.getUuid(), true), + tuple(user3.getUuid(), false)); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(IN).build(), + Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) + .containsExactlyInAnyOrder(tuple(user1.getUuid(), true), tuple(user2.getUuid(), true)); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(OUT).build(), + Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) + .containsExactlyInAnyOrder(tuple(user3.getUuid(), false)); + } + + @Test + public void selectByQuery_search_by_name_or_login() { + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + UserDto user1 = db.users().insertUser(u -> u.setLogin("user1").setName("John Doe")); + UserDto user2 = db.users().insertUser(u -> u.setLogin("user2").setName("John Smith")); + UserDto user3 = db.users().insertUser(u -> u.setLogin("user3").setName("Jane Doe")); + db.qualityGates().addUserPermission(qualityGate, user1); + db.qualityGates().addUserPermission(qualityGate, user2); + db.qualityGates().addUserPermission(qualityGate, user3); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(IN) + .setQuery("user2").build(), + Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactlyInAnyOrder(user2.getUuid()); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(IN) + .setQuery("joh").build(), + Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid()); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(IN) + .setQuery("Doe").build(), + Pagination.all())) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactlyInAnyOrder(user1.getUuid(), user3.getUuid()); + } + + @Test + public void selectByQuery_with_paging() { + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + UserDto user1 = db.users().insertUser(u -> u.setName("user1")); + UserDto user2 = db.users().insertUser(u -> u.setName("user2")); + UserDto user3 = db.users().insertUser(u -> u.setName("user3")); + db.qualityGates().addUserPermission(qualityGate, user1); + db.qualityGates().addUserPermission(qualityGate, user2); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(ANY) + .build(), + Pagination.forPage(1).andSize(1))) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactly(user1.getUuid()); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(ANY) + .build(), + Pagination.forPage(3).andSize(1))) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactly(user3.getUuid()); + + assertThat(underTest.selectByQuery(db.getSession(), builder() + .setQualityGate(qualityGate) + .setMembership(ANY) + .build(), + Pagination.forPage(1).andSize(10))) + .extracting(SearchUserMembershipDto::getUserUuid) + .containsExactly(user1.getUuid(), user2.getUuid(), user3.getUuid()); } }
\ No newline at end of file diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java index 02ef1d812c0..b90b8e20795 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditGroupsDaoTest.java @@ -34,10 +34,10 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; -import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.ANY; -import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.IN; -import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.OUT; -import static org.sonar.db.qualityprofile.SearchQualityProfileGroupsQuery.builder; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.ANY; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.IN; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.OUT; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.builder; public class QProfileEditGroupsDaoTest { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java index 4ccf9cb8789..c68922dd5a7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java @@ -27,16 +27,17 @@ import org.sonar.api.impl.utils.TestSystem2; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.Pagination; +import org.sonar.db.user.SearchUserMembershipDto; import org.sonar.db.user.UserDto; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; -import static org.sonar.db.qualityprofile.SearchUsersQuery.ANY; -import static org.sonar.db.qualityprofile.SearchUsersQuery.IN; -import static org.sonar.db.qualityprofile.SearchUsersQuery.OUT; -import static org.sonar.db.qualityprofile.SearchUsersQuery.builder; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.ANY; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.IN; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.OUT; +import static org.sonar.db.qualityprofile.SearchQualityProfilePermissionQuery.builder; import static org.sonar.test.ExceptionCauseMatcher.hasType; public class QProfileEditUsersDaoTest { @@ -103,24 +104,24 @@ public class QProfileEditUsersDaoTest { assertThat(underTest.selectByQuery(db.getSession(), builder() .setProfile(profile) .setMembership(ANY).build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid, UserMembershipDto::isSelected) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) .containsExactlyInAnyOrder( tuple(user1.getUuid(), true), tuple(user2.getUuid(), true), tuple(user3.getUuid(), false)); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(IN).build(), + .setProfile(profile) + .setMembership(IN).build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid, UserMembershipDto::isSelected) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) .containsExactlyInAnyOrder(tuple(user1.getUuid(), true), tuple(user2.getUuid(), true)); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(OUT).build(), + .setProfile(profile) + .setMembership(OUT).build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid, UserMembershipDto::isSelected) + .extracting(SearchUserMembershipDto::getUserUuid, SearchUserMembershipDto::isSelected) .containsExactlyInAnyOrder(tuple(user3.getUuid(), false)); } @@ -135,27 +136,27 @@ public class QProfileEditUsersDaoTest { db.qualityProfiles().addUserPermission(profile, user3); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(IN) - .setQuery("user2").build(), + .setProfile(profile) + .setMembership(IN) + .setQuery("user2").build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactlyInAnyOrder(user2.getUuid()); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(IN) - .setQuery("joh").build(), + .setProfile(profile) + .setMembership(IN) + .setQuery("joh").build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid()); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(IN) - .setQuery("Doe").build(), + .setProfile(profile) + .setMembership(IN) + .setQuery("Doe").build(), Pagination.all())) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactlyInAnyOrder(user1.getUuid(), user3.getUuid()); } @@ -169,27 +170,27 @@ public class QProfileEditUsersDaoTest { db.qualityProfiles().addUserPermission(profile, user2); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(ANY) - .build(), + .setProfile(profile) + .setMembership(ANY) + .build(), Pagination.forPage(1).andSize(1))) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactly(user1.getUuid()); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(ANY) - .build(), + .setProfile(profile) + .setMembership(ANY) + .build(), Pagination.forPage(3).andSize(1))) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactly(user3.getUuid()); assertThat(underTest.selectByQuery(db.getSession(), builder() - .setProfile(profile) - .setMembership(ANY) - .build(), + .setProfile(profile) + .setMembership(ANY) + .build(), Pagination.forPage(1).andSize(10))) - .extracting(UserMembershipDto::getUserUuid) + .extracting(SearchUserMembershipDto::getUserUuid) .containsExactly(user1.getUuid(), user2.getUuid(), user3.getUuid()); } |