3 * Copyright (C) 2009-2021 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.qualitygate.ws;
22 import java.util.Objects;
23 import javax.annotation.Nullable;
24 import org.sonar.db.DbClient;
25 import org.sonar.db.DbSession;
26 import org.sonar.db.project.ProjectDto;
27 import org.sonar.db.qualitygate.QualityGateConditionDto;
28 import org.sonar.db.qualitygate.QualityGateDto;
29 import org.sonar.server.component.ComponentFinder;
30 import org.sonar.server.user.UserSession;
31 import org.sonarqube.ws.Qualitygates;
33 import static com.google.common.base.Preconditions.checkArgument;
34 import static org.sonar.api.web.UserRole.ADMIN;
35 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES;
36 import static org.sonar.server.exceptions.NotFoundException.checkFound;
37 import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
39 public class QualityGatesWsSupport {
41 private final DbClient dbClient;
42 private final UserSession userSession;
43 private final ComponentFinder componentFinder;
45 public QualityGatesWsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
46 this.dbClient = dbClient;
47 this.userSession = userSession;
48 this.componentFinder = componentFinder;
51 public QualityGateDto getByUuid(DbSession dbSession, String qualityGateUuid) {
53 dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid),
54 "No quality gate has been found for id %s", qualityGateUuid);
57 public QualityGateDto getByName(DbSession dbSession, String qualityGateName) {
58 return checkFound(dbClient.qualityGateDao().selectByName(dbSession, qualityGateName),
59 "No quality gate has been found for name %s", qualityGateName);
62 QualityGateConditionDto getCondition(DbSession dbSession, String uuid) {
63 return checkFound(dbClient.gateConditionDao().selectByUuid(uuid, dbSession), "No quality gate condition with uuid '%s'", uuid);
66 boolean isQualityGateAdmin() {
67 return userSession.hasPermission(ADMINISTER_QUALITY_GATES);
70 Qualitygates.Actions getActions(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate) {
71 boolean isDefault = defaultQualityGate != null && Objects.equals(defaultQualityGate.getUuid(), qualityGate.getUuid());
72 boolean isBuiltIn = qualityGate.isBuiltIn();
73 boolean isQualityGateAdmin = isQualityGateAdmin();
74 return Qualitygates.Actions.newBuilder()
75 .setCopy(isQualityGateAdmin)
76 .setRename(!isBuiltIn && isQualityGateAdmin)
77 .setManageConditions(!isBuiltIn && isQualityGateAdmin)
78 .setDelete(!isDefault && !isBuiltIn && isQualityGateAdmin)
79 .setSetAsDefault(!isDefault && isQualityGateAdmin)
80 .setAssociateProjects(!isDefault && isQualityGateAdmin)
84 void checkCanEdit(QualityGateDto qualityGate) {
85 checkNotBuiltIn(qualityGate);
86 userSession.checkPermission(ADMINISTER_QUALITY_GATES);
89 void checkCanAdminProject(ProjectDto project) {
90 if (userSession.hasPermission(ADMINISTER_QUALITY_GATES)
91 || userSession.hasProjectPermission(ADMIN, project)) {
94 throw insufficientPrivilegesException();
97 ProjectDto getProject(DbSession dbSession, String projectKey) {
98 return componentFinder.getProjectByKey(dbSession, projectKey);
101 private static void checkNotBuiltIn(QualityGateDto qualityGate) {
102 checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName());