3 * Copyright (C) 2009-2023 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.github.permissions.controller;
22 import java.util.HashSet;
23 import java.util.List;
25 import javax.validation.Valid;
26 import org.sonar.server.common.github.permissions.GithubPermissionsMapping;
27 import org.sonar.server.common.github.permissions.GithubPermissionsMappingService;
28 import org.sonar.server.common.github.permissions.PermissionMappingChange;
29 import org.sonar.server.common.permission.Operation;
30 import org.sonar.server.user.UserSession;
31 import org.sonar.server.v2.api.github.permissions.model.RestGithubPermissionsMapping;
32 import org.sonar.server.v2.api.github.permissions.request.GithubPermissionMappingUpdateRequest;
33 import org.sonar.server.v2.api.github.permissions.request.PermissionMappingUpdate;
34 import org.sonar.server.v2.api.github.permissions.response.GithubPermissionsMappingRestResponse;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestBody;
38 import static org.sonar.api.web.UserRole.ADMIN;
39 import static org.sonar.api.web.UserRole.CODEVIEWER;
40 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
41 import static org.sonar.api.web.UserRole.SCAN;
42 import static org.sonar.api.web.UserRole.SECURITYHOTSPOT_ADMIN;
43 import static org.sonar.api.web.UserRole.USER;
45 public class DefaultGithubPermissionsController implements GithubPermissionsController {
47 private UserSession userSession;
48 private GithubPermissionsMappingService githubPermissionsMappingService;
50 public DefaultGithubPermissionsController(UserSession userSession, GithubPermissionsMappingService githubPermissionsMappingService) {
51 this.userSession = userSession;
52 this.githubPermissionsMappingService = githubPermissionsMappingService;
56 public GithubPermissionsMappingRestResponse fetchAll() {
57 userSession.checkIsSystemAdministrator();
58 List<GithubPermissionsMapping> permissionsMapping = githubPermissionsMappingService.getPermissionsMapping();
59 return new GithubPermissionsMappingRestResponse(toRestResources(permissionsMapping));
63 public RestGithubPermissionsMapping updateMapping(@PathVariable("githubRole") String githubRole, @Valid @RequestBody GithubPermissionMappingUpdateRequest request) {
64 userSession.checkIsSystemAdministrator();
65 PermissionMappingUpdate update = request.permissions();
66 Set<PermissionMappingChange> changes = new HashSet<>();
68 update.getUser().map(shouldAddPermission -> toPermissionMappingChange(githubRole, USER, shouldAddPermission)).applyIfDefined(changes::add);
69 update.getCodeViewer().map(shouldAddPermission -> toPermissionMappingChange(githubRole, CODEVIEWER, shouldAddPermission)).applyIfDefined(changes::add);
70 update.getIssueAdmin().map(shouldAddPermission -> toPermissionMappingChange(githubRole, ISSUE_ADMIN, shouldAddPermission)).applyIfDefined(changes::add);
71 update.getSecurityHotspotAdmin().map(shouldAddPermission -> toPermissionMappingChange(githubRole, SECURITYHOTSPOT_ADMIN, shouldAddPermission)).applyIfDefined(changes::add);
72 update.getAdmin().map(shouldAddPermission -> toPermissionMappingChange(githubRole, ADMIN, shouldAddPermission)).applyIfDefined(changes::add);
73 update.getScan().map(shouldAddPermission -> toPermissionMappingChange(githubRole, SCAN, shouldAddPermission)).applyIfDefined(changes::add);
75 githubPermissionsMappingService.updatePermissionsMappings(changes);
77 return toRestGithubPermissionMapping(githubPermissionsMappingService.getPermissionsMappingForGithubRole(githubRole));
81 private static PermissionMappingChange toPermissionMappingChange(String githubRole, String sonarqubePermission, boolean shouldAddPermission) {
82 return new PermissionMappingChange(githubRole, sonarqubePermission, shouldAddPermission ? Operation.ADD : Operation.REMOVE);
85 private static List<RestGithubPermissionsMapping> toRestResources(List<GithubPermissionsMapping> permissionsMapping) {
86 return permissionsMapping.stream()
87 .map(DefaultGithubPermissionsController::toRestGithubPermissionMapping)
91 private static RestGithubPermissionsMapping toRestGithubPermissionMapping(GithubPermissionsMapping githubPermissionsMapping) {
92 return new RestGithubPermissionsMapping(githubPermissionsMapping.roleName(), githubPermissionsMapping.roleName(), githubPermissionsMapping.permissions());