aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-webapi
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2022-08-26 15:21:27 -0500
committersonartech <sonartech@sonarsource.com>2022-09-02 20:02:50 +0000
commit742a75691f5dac7fe78a58cb0d45d3f4cdeeef6e (patch)
tree91257810b3ccfe222a885a2ee89c7de5c27a2852 /server/sonar-webserver-webapi
parentf22f405e8303f879c76280ffaff189b8a03de68b (diff)
downloadsonarqube-742a75691f5dac7fe78a58cb0d45d3f4cdeeef6e.tar.gz
sonarqube-742a75691f5dac7fe78a58cb0d45d3f4cdeeef6e.zip
SONAR-17246 Add parameter 'deactivated' to 'users/search' WS
Diffstat (limited to 'server/sonar-webserver-webapi')
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java27
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SearchActionTest.java23
2 files changed, 44 insertions, 6 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java
index 3e623378f71..b06645d7748 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SearchAction.java
@@ -61,7 +61,7 @@ import static org.sonarqube.ws.Users.SearchWsResponse.User;
import static org.sonarqube.ws.Users.SearchWsResponse.newBuilder;
public class SearchAction implements UsersWsAction {
-
+ private static final String DEACTIVATED_PARAM = "deactivated";
private static final int MAX_PAGE_SIZE = 500;
private final UserSession userSession;
@@ -79,7 +79,7 @@ public class SearchAction implements UsersWsAction {
@Override
public void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction("search")
- .setDescription("Get a list of active users. <br/>" +
+ .setDescription("Get a list of users. By default, only active users are returned.<br/>" +
"The following fields are only returned when user has Administer System permission or for logged-in in user :" +
"<ul>" +
" <li>'email'</li>" +
@@ -92,6 +92,7 @@ public class SearchAction implements UsersWsAction {
"Field 'lastConnectionDate' is only updated every hour, so it may not be accurate, for instance when a user authenticates many times in less than one hour.")
.setSince("3.6")
.setChangelog(
+ new Change("9.7", "New parameter 'deactivated' to optionally search for deactivated users"),
new Change("7.7", "New field 'lastConnectionDate' is added to response"),
new Change("7.4", "External identity is only returned to system administrators"),
new Change("6.4", "Paging response fields moved to a Paging object"),
@@ -116,6 +117,12 @@ public class SearchAction implements UsersWsAction {
" <em>exactly matches</em> the search query." +
" </li>" +
"</ul>");
+ action.createParam(DEACTIVATED_PARAM)
+ .setSince("9.7")
+ .setDescription("Return deactivated users instead of active users")
+ .setRequired(false)
+ .setDefaultValue(false)
+ .setBooleanPossibleValues();
}
@Override
@@ -126,7 +133,7 @@ public class SearchAction implements UsersWsAction {
private Users.SearchWsResponse doHandle(SearchRequest request) {
SearchOptions options = new SearchOptions().setPage(request.getPage(), request.getPageSize());
- SearchResult<UserDoc> result = userIndex.search(UserQuery.builder().setTextQuery(request.getQuery()).build(), options);
+ SearchResult<UserDoc> result = userIndex.search(UserQuery.builder().setActive(!request.isDeactivated()).setTextQuery(request.getQuery()).build(), options);
try (DbSession dbSession = dbClient.openSession(false)) {
List<String> logins = result.getDocs().stream().map(UserDoc::login).collect(toList());
Multimap<String, String> groupsByLogin = dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, logins);
@@ -177,21 +184,23 @@ public class SearchAction implements UsersWsAction {
checkArgument(pageSize <= MAX_PAGE_SIZE, "The '%s' parameter must be less than %s", PAGE_SIZE, MAX_PAGE_SIZE);
return SearchRequest.builder()
.setQuery(request.param(TEXT_QUERY))
+ .setDeactivated(request.mandatoryParamAsBoolean(DEACTIVATED_PARAM))
.setPage(request.mandatoryParamAsInt(PAGE))
.setPageSize(pageSize)
.build();
}
private static class SearchRequest {
-
private final Integer page;
private final Integer pageSize;
private final String query;
+ private final boolean deactivated;
private SearchRequest(Builder builder) {
this.page = builder.page;
this.pageSize = builder.pageSize;
this.query = builder.query;
+ this.deactivated = builder.deactivated;
}
@CheckForNull
@@ -209,6 +218,10 @@ public class SearchAction implements UsersWsAction {
return query;
}
+ public boolean isDeactivated() {
+ return deactivated;
+ }
+
public static Builder builder() {
return new Builder();
}
@@ -218,6 +231,7 @@ public class SearchAction implements UsersWsAction {
private Integer page;
private Integer pageSize;
private String query;
+ private boolean deactivated;
private Builder() {
// enforce factory method use
@@ -238,6 +252,11 @@ public class SearchAction implements UsersWsAction {
return this;
}
+ public Builder setDeactivated(boolean deactivated) {
+ this.deactivated = deactivated;
+ return this;
+ }
+
public SearchRequest build() {
return new SearchRequest(this);
}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SearchActionTest.java
index 8694485af5f..bd8961d7bfa 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SearchActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/SearchActionTest.java
@@ -62,9 +62,11 @@ public class SearchActionTest {
private WsActionTester ws = new WsActionTester(new SearchAction(userSession, index, db.getDbClient(), new AvatarResolverImpl()));
@Test
- public void search_for_all_users() {
+ public void search_for_all_active_users() {
UserDto user1 = db.users().insertUser();
UserDto user2 = db.users().insertUser();
+ UserDto user3 = db.users().insertUser(u -> u.setActive(false));
+
userIndexer.indexAll();
userSession.logIn();
@@ -79,6 +81,23 @@ public class SearchActionTest {
}
@Test
+ public void search_deactivated_users() {
+ UserDto user1 = db.users().insertUser(u -> u.setActive(false));
+ UserDto user2 = db.users().insertUser(u -> u.setActive(true));
+ userIndexer.indexAll();
+ userSession.logIn();
+
+ SearchWsResponse response = ws.newRequest()
+ .setParam("deactivated", "true")
+ .executeProtobuf(SearchWsResponse.class);
+
+ assertThat(response.getUsersList())
+ .extracting(User::getLogin, User::getName)
+ .containsExactlyInAnyOrder(
+ tuple(user1.getLogin(), user1.getName()));
+ }
+
+ @Test
public void search_with_query() {
userSession.logIn();
UserDto user = db.users().insertUser(u -> u
@@ -378,7 +397,7 @@ public class SearchActionTest {
assertThat(action).isNotNull();
assertThat(action.isPost()).isFalse();
assertThat(action.responseExampleAsString()).isNotEmpty();
- assertThat(action.params()).hasSize(3);
+ assertThat(action.params()).hasSize(4);
}
}