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 java.util.Optional;
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.ComponentQuery;
37 import org.sonar.db.permission.template.PermissionTemplateDto;
38 import org.sonar.server.permission.PermissionService;
39 import org.sonar.server.permission.ProjectId;
40 import org.sonar.server.permission.ws.PermissionWsSupport;
41 import org.sonar.server.permission.ws.PermissionsWsAction;
42 import org.sonar.server.user.UserSession;
43 import org.sonarqube.ws.client.permission.BulkApplyTemplateWsRequest;
45 import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
46 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
47 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
48 import static org.sonar.server.permission.ws.template.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_ORGANIZATION_KEY;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
56 public class BulkApplyTemplateAction implements PermissionsWsAction {
58 private final DbClient dbClient;
59 private final UserSession userSession;
60 private final PermissionService permissionService;
61 private final PermissionWsSupport wsSupport;
62 private final I18n i18n;
63 private final ResourceTypes resourceTypes;
65 public BulkApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionService permissionService, PermissionWsSupport wsSupport, I18n i18n,
66 ResourceTypes resourceTypes) {
67 this.dbClient = dbClient;
68 this.userSession = userSession;
69 this.permissionService = permissionService;
70 this.wsSupport = wsSupport;
72 this.resourceTypes = resourceTypes;
76 public void define(WebService.NewController context) {
77 WebService.NewAction action = context.createAction("bulk_apply_template")
78 .setDescription("Apply a permission template to several projects.<br />" +
79 "The template id or name must be provided.<br />" +
80 "It requires administration permissions to access.")
85 action.createParam(Param.TEXT_QUERY)
86 .setDescription("Limit search to: <ul>" +
87 "<li>project names that contain the supplied string</li>" +
88 "<li>project keys that are exactly the same as the supplied string</li>" +
90 .setExampleValue("apac");
91 createRootQualifierParameter(action, newQualifierParameterContext(i18n, resourceTypes));
92 createTemplateParameters(action);
96 public void handle(Request request, Response response) throws Exception {
97 doHandle(toBulkApplyTemplateWsRequest(request));
101 private void doHandle(BulkApplyTemplateWsRequest request) {
102 try (DbSession dbSession = dbClient.openSession(false)) {
103 PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
104 request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
105 ComponentQuery componentQuery = ComponentQuery.builder()
106 .setNameOrKeyQuery(request.getQuery())
107 .setQualifiers(qualifiers(request.getQualifier()))
109 List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, componentQuery, 0, Integer.MAX_VALUE);
111 for (ComponentDto project : projects) {
112 ProjectId projectId = new ProjectId(project);
113 checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.of(projectId));
115 permissionService.apply(dbSession, template, projects);
119 private String[] qualifiers(@Nullable String qualifier) {
120 return qualifier == null
121 ? Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER).toArray(new String[resourceTypes.getRoots().size()])
122 : (new String[] {qualifier});
125 private static BulkApplyTemplateWsRequest toBulkApplyTemplateWsRequest(Request request) {
126 return new BulkApplyTemplateWsRequest()
127 .setTemplateId(request.param(PARAM_TEMPLATE_ID))
128 .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
129 .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
130 .setQualifier(request.param(PARAM_QUALIFIER))
131 .setQuery(request.param(Param.TEXT_QUERY));