3 * Copyright (C) 2009-2019 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.qualityprofile.ws;
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;
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;
44 public class AddProjectAction implements QProfileWsAction {
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;
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;
61 public void define(WebService.NewController controller) {
62 NewAction action = controller.createAction(ACTION_ADD_PROJECT)
64 .setDescription("Associate a project with a quality profile.<br> " +
65 "Requires one of the following permissions:" +
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>" +
74 QProfileReference.defineParams(action, languages);
75 QProfileWsSupport.createOrganizationParam(action)
78 action.createParam(PARAM_PROJECT)
79 .setDescription("Project key")
80 .setDeprecatedKey("projectKey", "6.5")
81 .setExampleValue(KEY_PROJECT_EXAMPLE_001);
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);
90 public void handle(Request request, Response response) throws Exception {
91 userSession.checkLoggedIn();
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);
99 if (!profile.getOrganizationUuid().equals(project.getOrganizationUuid())) {
100 throw new IllegalArgumentException("Project and quality profile must have the same organization");
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);
108 } else if (!profile.getKee().equals(currentProfile.getKee())) {
109 dbClient.qualityProfileDao().updateProjectProfileAssociation(dbSession, project, profile.getKee(), currentProfile.getKee());
114 response.noContent();
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);
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)) {
129 throw insufficientPrivilegesException();