3 * Copyright (C) 2009-2017 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.permission.ws.template;
22 import java.util.List;
23 import org.sonar.api.i18n.I18n;
24 import org.sonar.api.resources.Qualifiers;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.Request;
27 import org.sonar.api.server.ws.Response;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.api.server.ws.WebService.Param;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.component.ComponentQuery;
34 import org.sonar.db.permission.template.PermissionTemplateDto;
35 import org.sonar.server.permission.PermissionTemplateService;
36 import org.sonar.server.permission.ws.PermissionWsSupport;
37 import org.sonar.server.permission.ws.PermissionsWsAction;
38 import org.sonar.server.user.UserSession;
39 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
41 import static org.sonar.core.util.Protobuf.setNullable;
42 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
43 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
44 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
45 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifiersParameter;
46 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
51 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
53 public class BulkApplyTemplateAction implements PermissionsWsAction {
55 private final DbClient dbClient;
56 private final UserSession userSession;
57 private final PermissionTemplateService permissionTemplateService;
58 private final PermissionWsSupport wsSupport;
59 private final I18n i18n;
60 private final ResourceTypes resourceTypes;
62 public BulkApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionTemplateService permissionTemplateService, PermissionWsSupport wsSupport, I18n i18n,
63 ResourceTypes resourceTypes) {
64 this.dbClient = dbClient;
65 this.userSession = userSession;
66 this.permissionTemplateService = permissionTemplateService;
67 this.wsSupport = wsSupport;
69 this.resourceTypes = resourceTypes;
73 public void define(WebService.NewController context) {
74 WebService.NewAction action = context.createAction("bulk_apply_template")
75 .setDescription("Apply a permission template to several projects.<br />" +
76 "The template id or name must be provided.<br />" +
77 "Requires the following permission: 'Administer System'.")
82 action.createParam(Param.TEXT_QUERY)
83 .setDescription("Limit search to: <ul>" +
84 "<li>project names that contain the supplied string</li>" +
85 "<li>project keys that are exactly the same as the supplied string</li>" +
87 .setExampleValue("apac");
89 createRootQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes))
90 .setDefaultValue(Qualifiers.PROJECT)
91 .setDeprecatedKey(PARAM_QUALIFIER, "6.6");
93 createTemplateParameters(action);
97 public void handle(Request request, Response response) throws Exception {
98 doHandle(toBulkApplyTemplateWsRequest(request));
102 private void doHandle(BulkApplyTemplateWsRequest request) {
103 try (DbSession dbSession = dbClient.openSession(false)) {
104 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
105 request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
106 checkGlobalAdmin(userSession, template.getOrganizationUuid());
108 ComponentQuery componentQuery = buildDbQuery(request);
109 List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, template.getOrganizationUuid(), componentQuery, 0, Integer.MAX_VALUE);
111 permissionTemplateService.applyAndCommit(dbSession, template, projects);
115 private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
116 return new BulkApplyTemplateWsRequest()
117 .setOrganization(request.param(PARAM_ORGANIZATION))
118 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
119 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
120 .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS))
121 .setQuery(request.param(Param.TEXT_QUERY));
124 private static ComponentQuery buildDbQuery(BulkApplyTemplateWsRequest request) {
125 ComponentQuery.Builder dbQuery = ComponentQuery.builder()
126 .setNameOrKeyQuery(request.getQuery());
127 setNullable(request.getQualifiers(), l -> dbQuery.setQualifiers(l.toArray(new String[0])));
129 return dbQuery.build();