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 static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
24 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
25 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
26 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
27 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
28 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
29 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
30 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
32 import com.google.common.collect.Collections2;
33 import com.google.common.collect.Lists;
34 import java.util.List;
35 import javax.annotation.Nullable;
36 import org.sonar.api.i18n.I18n;
37 import org.sonar.api.resources.ResourceTypes;
38 import org.sonar.api.server.ws.Request;
39 import org.sonar.api.server.ws.Response;
40 import org.sonar.api.server.ws.WebService;
41 import org.sonar.api.server.ws.WebService.Param;
42 import org.sonar.db.DbClient;
43 import org.sonar.db.DbSession;
44 import org.sonar.db.component.ComponentDto;
45 import org.sonar.db.component.ComponentDtoFunctions;
46 import org.sonar.db.component.ComponentQuery;
47 import org.sonar.db.permission.PermissionTemplateDto;
48 import org.sonar.server.permission.ApplyPermissionTemplateQuery;
49 import org.sonar.server.permission.PermissionService;
50 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
51 import org.sonar.server.permission.ws.PermissionsWsAction;
52 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
54 public class BulkApplyTemplateAction implements PermissionsWsAction {
55 private final DbClient dbClient;
56 private final PermissionService permissionService;
57 private final PermissionDependenciesFinder finder;
58 private final I18n i18n;
59 private final ResourceTypes resourceTypes;
61 public BulkApplyTemplateAction(DbClient dbClient, PermissionService permissionService, PermissionDependenciesFinder finder, I18n i18n,
62 ResourceTypes resourceTypes) {
63 this.dbClient = dbClient;
64 this.permissionService = permissionService;
67 this.resourceTypes = resourceTypes;
71 public void define(WebService.NewController context) {
72 WebService.NewAction action = context.createAction("bulk_apply_template")
73 .setDescription("Apply a permission template to several projects.<br />" +
74 "The template id or name must be provided.<br />" +
75 "It requires administration permissions to access.")
80 action.createParam(Param.TEXT_QUERY)
81 .setDescription("Limit search to: <ul>" +
82 "<li>project names that contain the supplied string</li>" +
83 "<li>project keys that are exactly the same as the supplied string</li>" +
85 .setExampleValue("apac");
86 createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes));
87 createTemplateParameters(action);
91 public void handle(Request request, Response response) throws Exception {
92 doHandle(toBulkApplyTemplateWsRequest(request));
96 private void doHandle(BulkApplyTemplateWsRequest request) {
97 DbSession dbSession = dbClient.openSession(false);
99 PermissionTemplateDto template = finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
100 ComponentQuery componentQuery = ComponentQuery.builder()
101 .setNameOrKeyQuery(request.getQuery())
102 .setQualifiers(qualifiers(request.getQualifier()))
104 List<ComponentDto> rootComponents = dbClient.componentDao().selectByQuery(dbSession, componentQuery, 0, Integer.MAX_VALUE);
105 if (rootComponents.isEmpty()) {
109 ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create(
111 Lists.transform(rootComponents, ComponentDtoFunctions.toKey()));
112 permissionService.applyPermissionTemplate(dbSession, query);
114 dbClient.closeSession(dbSession);
118 private String[] qualifiers(@Nullable String qualifier) {
119 return qualifier == null
120 ? Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER).toArray(new String[resourceTypes.getRoots().size()])
121 : (new String[] {qualifier});
124 private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
125 return new BulkApplyTemplateWsRequest()
126 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
127 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
128 .setQualifier(request.param(PARAM_QUALIFIER))
129 .setQuery(request.param(Param.TEXT_QUERY));