]> source.dussan.org Git - sonarqube.git/blob
c6148669832202c8fb3def34911c8cb7fd3c5a7b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.v2.api.github.permissions.controller;
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
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;
37
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;
44
45 public class DefaultGithubPermissionsController implements GithubPermissionsController {
46
47   private final UserSession userSession;
48   private final GithubPermissionsMappingService githubPermissionsMappingService;
49
50   public DefaultGithubPermissionsController(UserSession userSession, GithubPermissionsMappingService githubPermissionsMappingService) {
51     this.userSession = userSession;
52     this.githubPermissionsMappingService = githubPermissionsMappingService;
53   }
54
55   @Override
56   public GithubPermissionsMappingRestResponse fetchAll() {
57     userSession.checkIsSystemAdministrator();
58     List<GithubPermissionsMapping> permissionsMapping = githubPermissionsMappingService.getPermissionsMapping();
59     return new GithubPermissionsMappingRestResponse(toRestResources(permissionsMapping));
60   }
61
62   @Override
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<>();
67
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);
74
75     githubPermissionsMappingService.updatePermissionsMappings(changes);
76
77     return toRestGithubPermissionMapping(githubPermissionsMappingService.getPermissionsMappingForGithubRole(githubRole));
78
79   }
80
81   @Override
82   public void deleteMapping(String githubRole) {
83     userSession.checkIsSystemAdministrator();
84     githubPermissionsMappingService.deletePermissionMappings(githubRole);
85   }
86
87   private static PermissionMappingChange toPermissionMappingChange(String githubRole, String sonarqubePermission, boolean shouldAddPermission) {
88     return new PermissionMappingChange(githubRole, sonarqubePermission, shouldAddPermission ? Operation.ADD : Operation.REMOVE);
89   }
90
91   private static List<RestGithubPermissionsMapping> toRestResources(List<GithubPermissionsMapping> permissionsMapping) {
92     return permissionsMapping.stream()
93       .map(DefaultGithubPermissionsController::toRestGithubPermissionMapping)
94       .toList();
95   }
96
97   private static RestGithubPermissionsMapping toRestGithubPermissionMapping(GithubPermissionsMapping githubPermissionsMapping) {
98     return new RestGithubPermissionsMapping(
99       githubPermissionsMapping.roleName(),
100       githubPermissionsMapping.roleName(),
101       githubPermissionsMapping.isBaseRole(),
102       githubPermissionsMapping.permissions());
103   }
104
105 }