3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.
21 package org.sonar.server.permission.ws.template;
23 import com.google.common.collect.Collections2;
24 import java.util.List;
25 import javax.annotation.Nullable;
26 import org.sonar.api.i18n.I18n;
27 import org.sonar.api.resources.ResourceType;
28 import org.sonar.api.resources.ResourceTypes;
29 import org.sonar.api.server.ws.Request;
30 import org.sonar.api.server.ws.Response;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.api.server.ws.WebService.Param;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.component.ComponentQuery;
37 import org.sonar.db.permission.template.PermissionTemplateDto;
38 import org.sonar.server.permission.PermissionTemplateService;
39 import org.sonar.server.permission.ws.PermissionWsSupport;
40 import org.sonar.server.permission.ws.PermissionsWsAction;
41 import org.sonar.server.user.UserSession;
42 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
44 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
45 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
46 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
47 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
48 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
54 public class BulkApplyTemplateAction implements PermissionsWsAction {
56 private final DbClient dbClient;
57 private final UserSession userSession;
58 private final PermissionTemplateService permissionTemplateService;
59 private final PermissionWsSupport wsSupport;
60 private final I18n i18n;
61 private final ResourceTypes resourceTypes;
63 public BulkApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionTemplateService permissionTemplateService, PermissionWsSupport wsSupport, I18n i18n,
64 ResourceTypes resourceTypes) {
65 this.dbClient = dbClient;
66 this.userSession = userSession;
67 this.permissionTemplateService = permissionTemplateService;
68 this.wsSupport = wsSupport;
70 this.resourceTypes = resourceTypes;
73 private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
74 return new BulkApplyTemplateWsRequest()
75 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
76 .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
77 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
78 .setQualifier(request.param(PARAM_QUALIFIER))
79 .setQuery(request.param(Param.TEXT_QUERY));
83 public void define(WebService.NewController context) {
84 WebService.NewAction action = context.createAction("bulk_apply_template")
85 .setDescription("Apply a permission template to several projects.<br />" +
86 "The template id or name must be provided.<br />" +
87 "Requires the following permission: 'Administer System'.")
92 action.createParam(Param.TEXT_QUERY)
93 .setDescription("Limit search to: <ul>" +
94 "<li>project names that contain the supplied string</li>" +
95 "<li>project keys that are exactly the same as the supplied string</li>" +
97 .setExampleValue("apac");
98 createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes));
99 createTemplateParameters(action);
103 public void handle(Request request, Response response) throws Exception {
104 doHandle(toBulkApplyTemplateWsRequest(request));
105 response.noContent();
108 private void doHandle(BulkApplyTemplateWsRequest request) {
109 try (DbSession dbSession = dbClient.openSession(false)) {
110 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
111 request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
112 ComponentQuery componentQuery = ComponentQuery.builder()
113 .setNameOrKeyQuery(request.getQuery())
114 .setQualifiers(qualifiers(request.getQualifier()))
116 List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, componentQuery, 0, Integer.MAX_VALUE);
118 checkGlobalAdmin(userSession, template.getOrganizationUuid());
119 permissionTemplateService.apply(dbSession, template, projects);
123 private String[] qualifiers(@Nullable String qualifier) {
124 return qualifier == null
125 ? Collections2.transform(resourceTypes.getRoots(), ResourceType::getQualifier).toArray(new String[resourceTypes.getRoots().size()])
126 : (new String[] {qualifier});