]> source.dussan.org Git - sonarqube.git/blob
c48519d43154842c6e23fee66cfb7ea15486278c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.qualitygate.ws;
21
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;
32
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;
38
39 public class QualityGatesWsSupport {
40
41   private final DbClient dbClient;
42   private final UserSession userSession;
43   private final ComponentFinder componentFinder;
44
45   public QualityGatesWsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
46     this.dbClient = dbClient;
47     this.userSession = userSession;
48     this.componentFinder = componentFinder;
49   }
50
51   public QualityGateDto getByUuid(DbSession dbSession, String qualityGateUuid) {
52     return checkFound(
53       dbClient.qualityGateDao().selectByUuid(dbSession, qualityGateUuid),
54       "No quality gate has been found for id %s", qualityGateUuid);
55   }
56
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);
60   }
61
62   QualityGateConditionDto getCondition(DbSession dbSession, String uuid) {
63     return checkFound(dbClient.gateConditionDao().selectByUuid(uuid, dbSession), "No quality gate condition with uuid '%s'", uuid);
64   }
65
66   boolean isQualityGateAdmin() {
67     return userSession.hasPermission(ADMINISTER_QUALITY_GATES);
68   }
69
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)
81       .build();
82   }
83
84   void checkCanEdit(QualityGateDto qualityGate) {
85     checkNotBuiltIn(qualityGate);
86     userSession.checkPermission(ADMINISTER_QUALITY_GATES);
87   }
88
89   void checkCanAdminProject(ProjectDto project) {
90     if (userSession.hasPermission(ADMINISTER_QUALITY_GATES)
91       || userSession.hasProjectPermission(ADMIN, project)) {
92       return;
93     }
94     throw insufficientPrivilegesException();
95   }
96
97   ProjectDto getProject(DbSession dbSession, String projectKey) {
98     return componentFinder.getProjectByKey(dbSession, projectKey);
99   }
100
101   private static void checkNotBuiltIn(QualityGateDto qualityGate) {
102     checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName());
103   }
104 }