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 com.google.common.collect.Lists;
25 import java.util.List;
26 import javax.annotation.Nullable;
27 import org.sonar.api.i18n.I18n;
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.ComponentDtoFunctions;
37 import org.sonar.db.component.ComponentQuery;
38 import org.sonar.db.permission.PermissionTemplateDto;
39 import org.sonar.server.permission.ApplyPermissionTemplateQuery;
40 import org.sonar.server.permission.PermissionService;
41 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
42 import org.sonar.server.permission.ws.PermissionsWsAction;
43 import org.sonar.server.user.UserSession;
44 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
46 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
47 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
48 import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
49 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
50 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
55 public class BulkApplyTemplateAction implements PermissionsWsAction {
56 private final DbClient dbClient;
57 private final PermissionService permissionService;
58 private final PermissionDependenciesFinder finder;
59 private final UserSession userSession;
60 private final I18n i18n;
61 private final ResourceTypes resourceTypes;
63 public BulkApplyTemplateAction(DbClient dbClient, PermissionService permissionService, PermissionDependenciesFinder finder, UserSession userSession, I18n i18n,
64 ResourceTypes resourceTypes) {
65 this.dbClient = dbClient;
66 this.permissionService = permissionService;
68 this.userSession = userSession;
70 this.resourceTypes = resourceTypes;
74 public void define(WebService.NewController context) {
75 WebService.NewAction action = context.createAction("bulk_apply_template")
76 .setDescription("Apply a permission template to several projects.<br />" +
77 "The template id or name must be provided.<br />" +
78 "It requires administration permissions to access.")
83 action.createParam(Param.TEXT_QUERY)
84 .setDescription("Limit search to: <ul>" +
85 "<li>project names that contain the supplied string</li>" +
86 "<li>project keys that are exactly the same as the supplied string</li>" +
88 .setExampleValue("apac");
89 createRootQualifierParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes));
90 createTemplateParameters(action);
94 public void handle(Request request, Response response) throws Exception {
95 doHandle(toBulkApplyTemplateWsRequest(request));
99 private void doHandle(BulkApplyTemplateWsRequest request) {
100 DbSession dbSession = dbClient.openSession(false);
102 PermissionTemplateDto template = finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
103 ComponentQuery componentQuery = ComponentQuery.builder()
104 .setNameOrKeyQuery(request.getQuery())
105 .setQualifiers(qualifiers(request.getQualifier()))
107 List<ComponentDto> rootComponents = dbClient.componentDao().selectByQuery(dbSession, componentQuery, 0, Integer.MAX_VALUE);
108 if (rootComponents.isEmpty()) {
112 ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create(
114 Lists.transform(rootComponents, ComponentDtoFunctions.toKey()));
115 permissionService.applyPermissionTemplate(dbSession, query);
117 dbClient.closeSession(dbSession);
121 private String[] qualifiers(@Nullable String qualifier) {
122 return qualifier == null
123 ? Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER).toArray(new String[resourceTypes.getRoots().size()])
124 : (new String[] {qualifier});
127 private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
128 return new BulkApplyTemplateWsRequest()
129 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
130 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
131 .setQualifier(request.param(PARAM_QUALIFIER))
132 .setQuery(request.param(Param.TEXT_QUERY));