]> source.dussan.org Git - sonarqube.git/blob
7bcabfd288757f65c29749f4db6d2630d2610143
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.qualityprofile.ws;
21
22 import org.sonar.api.resources.Languages;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.api.server.ws.WebService.NewAction;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.server.component.ComponentFinder;
34 import org.sonar.server.user.UserSession;
35
36 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_08;
37 import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_UUID_AND_KEY;
38 import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
39 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
40 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ADD_PROJECT;
41 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT;
42 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID;
43
44 public class AddProjectAction implements QProfileWsAction {
45
46   private final DbClient dbClient;
47   private final UserSession userSession;
48   private final Languages languages;
49   private final ComponentFinder componentFinder;
50   private final QProfileWsSupport wsSupport;
51
52   public AddProjectAction(DbClient dbClient, UserSession userSession, Languages languages, ComponentFinder componentFinder, QProfileWsSupport wsSupport) {
53     this.dbClient = dbClient;
54     this.userSession = userSession;
55     this.languages = languages;
56     this.componentFinder = componentFinder;
57     this.wsSupport = wsSupport;
58   }
59
60   @Override
61   public void define(WebService.NewController controller) {
62     NewAction action = controller.createAction(ACTION_ADD_PROJECT)
63       .setSince("5.2")
64       .setDescription("Associate a project with a quality profile.<br> " +
65         "Requires one of the following permissions:" +
66         "<ul>" +
67         "  <li>'Administer Quality Profiles'</li>" +
68         "  <li>Edit right on the specified quality profile</li>" +
69         "  <li>Administer right on the specified project</li>" +
70         "</ul>")
71       .setPost(true)
72       .setHandler(this);
73
74     QProfileReference.defineParams(action, languages);
75     QProfileWsSupport.createOrganizationParam(action)
76       .setSince("6.4");
77
78     action.createParam(PARAM_PROJECT)
79       .setDescription("Project key")
80       .setDeprecatedKey("projectKey", "6.5")
81       .setExampleValue(KEY_PROJECT_EXAMPLE_001);
82
83     action.createParam(PARAM_PROJECT_UUID)
84       .setDescription("Project ID. Either this parameter or '%s' must be set.", PARAM_PROJECT)
85       .setDeprecatedSince("6.5")
86       .setExampleValue(UUID_EXAMPLE_08);
87   }
88
89   @Override
90   public void handle(Request request, Response response) throws Exception {
91     userSession.checkLoggedIn();
92
93     try (DbSession dbSession = dbClient.openSession(false)) {
94       ComponentDto project = loadProject(dbSession, request);
95       QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request));
96       OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
97       checkPermissions(dbSession, organization, profile, project);
98
99       if (!profile.getOrganizationUuid().equals(project.getOrganizationUuid())) {
100         throw new IllegalArgumentException("Project and quality profile must have the same organization");
101       }
102
103       QProfileDto currentProfile = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(dbSession, project, profile.getLanguage());
104       if (currentProfile == null) {
105         // project uses the default profile
106         dbClient.qualityProfileDao().insertProjectProfileAssociation(dbSession, project, profile);
107         dbSession.commit();
108       } else if (!profile.getKee().equals(currentProfile.getKee())) {
109         dbClient.qualityProfileDao().updateProjectProfileAssociation(dbSession, project, profile.getKee(), currentProfile.getKee());
110         dbSession.commit();
111       }
112     }
113
114     response.noContent();
115   }
116
117   private ComponentDto loadProject(DbSession dbSession, Request request) {
118     String projectKey = request.param(PARAM_PROJECT);
119     String projectUuid = request.param(PARAM_PROJECT_UUID);
120     return componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, PROJECT_UUID_AND_KEY);
121   }
122
123   private void checkPermissions(DbSession dbSession, OrganizationDto organization, QProfileDto profile, ComponentDto project) {
124     if (wsSupport.canEdit(dbSession, organization, profile)
125       || userSession.hasComponentPermission(UserRole.ADMIN, project)) {
126       return;
127     }
128
129     throw insufficientPrivilegesException();
130   }
131 }