From 856a11e8266edf43d70d3781d30ac017435685bb Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Thu, 13 Jun 2019 12:24:59 +0200 Subject: [PATCH] SONAR-11560 fix api/permissions/users sorting --- .../sonar/db/permission/PermissionQuery.java | 13 ++++++ .../db/permission/UserPermissionDao.java | 11 ++++- .../db/permission/UserPermissionMapper.java | 5 +++ .../db/permission/UserPermissionMapper.xml | 22 ++++++++++ .../db/permission/PermissionQueryTest.java | 2 + .../db/permission/UserPermissionDaoTest.java | 42 +++++++++++++++++++ .../server/permission/ws/GroupsAction.java | 1 + .../server/permission/ws/UsersAction.java | 16 +++---- .../server/permission/ws/users-example.json | 9 +++- .../server/permission/ws/UsersActionTest.java | 6 ++- 10 files changed, 116 insertions(+), 11 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/PermissionQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/PermissionQuery.java index 4e346ffef52..8eaab6b2b18 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/PermissionQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/PermissionQuery.java @@ -48,6 +48,7 @@ public class PermissionQuery { private final String permission; // filter on project, else filter org permissions private final String componentUuid; + private final Long componentId; private final String template; // filter on login, email or name of users or groups @@ -67,6 +68,7 @@ public class PermissionQuery { this.permission = builder.permission; this.withAtLeastOnePermission = builder.withAtLeastOnePermission; this.componentUuid = builder.componentUuid; + this.componentId = builder.componentId; this.template = builder.template; this.searchQuery = builder.searchQuery; this.searchQueryToSql = builder.searchQuery == null ? null : buildLikeValue(builder.searchQuery, WildcardPosition.BEFORE_AND_AFTER); @@ -99,6 +101,11 @@ public class PermissionQuery { return componentUuid; } + @CheckForNull + public Long getComponentId() { + return componentId; + } + @CheckForNull public String getSearchQuery() { return searchQuery; @@ -130,6 +137,7 @@ public class PermissionQuery { private String permission; private String organizationUuid; private String componentUuid; + private Long componentId; private String template; private String searchQuery; private boolean withAtLeastOnePermission; @@ -157,6 +165,11 @@ public class PermissionQuery { return this; } + public Builder setComponentId(Long componentId) { + this.componentId = componentId; + return this; + } + public Builder setOrganizationUuid(String organizationUuid) { this.organizationUuid = organizationUuid; return this; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java index c4556579a95..f29f4ba3f97 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java @@ -51,7 +51,16 @@ public class UserPermissionDao implements Dao { } public List selectUserIdsByQuery(DbSession dbSession, PermissionQuery query) { - return mapper(dbSession).selectUserIdsByQuery(query) + return paginate(mapper(dbSession).selectUserIdsByQuery(query), query); + } + + public List selectUserIdsByQueryAndScope(DbSession dbSession, PermissionQuery query) { + return paginate(mapper(dbSession).selectUserIdsByQueryAndScope(query), query); + } + + + private static List paginate(List results, PermissionQuery query) { + return results .stream() // Pagination is done in Java because it's too complex to use SQL pagination in Oracle and MsSQL with the distinct .skip(query.getPageOffset()) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java index 9156aeb6a5a..164673412d9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionMapper.java @@ -30,6 +30,11 @@ public interface UserPermissionMapper { List selectUserIdsByQuery(@Param("query") PermissionQuery query); + /** + * Fetch user ids based on permission query and only in a specific scope (global permissions only, organization permissions only or project permissions only) + */ + List selectUserIdsByQueryAndScope(@Param("query") PermissionQuery query); + /** * Count the number of distinct users returned by {@link #selectUserIdsByQuery(PermissionQuery)} * {@link PermissionQuery#getPageOffset()} and {@link PermissionQuery#getPageSize()} are ignored. diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml index 62453effbc5..71d6b929116 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml @@ -27,6 +27,28 @@ order by case when (count(ur.role) > 0) then 1 else 2 end asc, lower(u.name) asc + +