3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.v2.api.group.controller;
22 import java.util.List;
23 import org.sonar.db.DbClient;
24 import org.sonar.db.DbSession;
25 import org.sonar.server.common.SearchResults;
26 import org.sonar.server.common.group.service.GroupInformation;
27 import org.sonar.server.common.group.service.GroupSearchRequest;
28 import org.sonar.server.common.group.service.GroupService;
29 import org.sonar.server.common.management.ManagedInstanceChecker;
30 import org.sonar.server.exceptions.NotFoundException;
31 import org.sonar.server.user.UserSession;
32 import org.sonar.server.v2.api.group.request.GroupCreateRestRequest;
33 import org.sonar.server.v2.api.group.request.GroupUpdateRestRequest;
34 import org.sonar.server.v2.api.group.request.GroupsSearchRestRequest;
35 import org.sonar.server.v2.api.group.response.GroupsSearchRestResponse;
36 import org.sonar.server.v2.api.group.response.GroupRestResponse;
37 import org.sonar.server.v2.api.model.RestPage;
38 import org.sonar.server.v2.api.response.PageRestResponse;
40 public class DefaultGroupController implements GroupController {
42 private static final String GROUP_NOT_FOUND_MESSAGE = "Group '%s' not found";
43 private final GroupService groupService;
44 private final DbClient dbClient;
45 private final UserSession userSession;
46 private final ManagedInstanceChecker managedInstanceChecker;
48 public DefaultGroupController(UserSession userSession, DbClient dbClient, GroupService groupService, ManagedInstanceChecker managedInstanceChecker) {
49 this.groupService = groupService;
50 this.dbClient = dbClient;
51 this.userSession = userSession;
52 this.managedInstanceChecker = managedInstanceChecker;
56 public GroupsSearchRestResponse search(GroupsSearchRestRequest groupsSearchRestRequest, RestPage restPage) {
57 userSession.checkLoggedIn().checkIsSystemAdministrator();
58 try (DbSession dbSession = dbClient.openSession(false)) {
59 GroupSearchRequest groupSearchRequest = new GroupSearchRequest(groupsSearchRestRequest.q(), groupsSearchRestRequest.managed(), restPage.pageIndex(), restPage.pageSize());
60 SearchResults<GroupInformation> searchResults = groupService.search(dbSession, groupSearchRequest);
61 List<GroupRestResponse> groupRestResponses = toGroupRestResponses(searchResults);
62 return new GroupsSearchRestResponse(groupRestResponses, new PageRestResponse(restPage.pageIndex(), restPage.pageSize(), searchResults.total()));
66 private static List<GroupRestResponse> toGroupRestResponses(SearchResults<GroupInformation> searchResults) {
67 return searchResults.searchResults().stream()
68 .map(DefaultGroupController::toRestGroup)
73 public GroupRestResponse fetchGroup(String id) {
74 userSession.checkLoggedIn().checkIsSystemAdministrator();
75 try (DbSession session = dbClient.openSession(false)) {
76 GroupInformation groupInformation = findGroupInformationOrThrow(id, session);
77 return toRestGroup(groupInformation);
82 public void deleteGroup(String id) {
83 throwIfNotAllowedToModifyGroups();
84 try (DbSession session = dbClient.openSession(false)) {
85 GroupInformation group = findGroupInformationOrThrow(id, session);
86 groupService.delete(session, group.groupDto());
92 public GroupRestResponse updateGroup(String id, GroupUpdateRestRequest updateRequest) {
93 throwIfNotAllowedToModifyGroups();
94 try (DbSession session = dbClient.openSession(false)) {
95 GroupInformation group = findGroupInformationOrThrow(id, session);
96 GroupInformation updatedGroup = groupService.updateGroup(
99 updateRequest.getName().orElse(group.groupDto().getName()),
100 updateRequest.getDescription().orElse(group.groupDto().getDescription()));
102 return toRestGroup(updatedGroup);
106 private GroupInformation findGroupInformationOrThrow(String id, DbSession session) {
107 return groupService.findGroupByUuid(session, id)
108 .orElseThrow(() -> new NotFoundException(String.format(GROUP_NOT_FOUND_MESSAGE, id)));
112 public GroupRestResponse create(GroupCreateRestRequest request) {
113 throwIfNotAllowedToModifyGroups();
114 try (DbSession session = dbClient.openSession(false)) {
115 GroupInformation createdGroup = groupService.createGroup(session, request.name(), request.description());
117 return toRestGroup(createdGroup);
121 private void throwIfNotAllowedToModifyGroups() {
122 userSession.checkIsSystemAdministrator();
123 managedInstanceChecker.throwIfInstanceIsManaged();
126 private static GroupRestResponse toRestGroup(GroupInformation group) {
127 return new GroupRestResponse(group.groupDto().getUuid(), group.groupDto().getName(), group.groupDto().getDescription(), group.isManaged(), group.isDefault());