Skip to content

Commit

Permalink
SONAR-6494 WS permissions/apply_template apply a permission template …
Browse files Browse the repository at this point in the history
…to a project
  • Loading branch information
teryk committed Sep 2, 2015
1 parent dfc395c commit f8559bc
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 8 deletions.
@@ -0,0 +1,82 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.permission.ws;

import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.PermissionTemplateDto;
import org.sonar.server.permission.ApplyPermissionTemplateQuery;
import org.sonar.server.permission.PermissionService;

import static java.util.Collections.singletonList;
import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID_EXPLICIT;
import static org.sonar.server.permission.ws.Parameters.createProjectParameter;
import static org.sonar.server.permission.ws.Parameters.createExplicitTemplateId;

public class ApplyTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
private final PermissionService permissionService;
private final PermissionDependenciesFinder finder;

public ApplyTemplateAction(DbClient dbClient, PermissionService permissionService, PermissionDependenciesFinder finder) {
this.dbClient = dbClient;
this.permissionService = permissionService;
this.finder = finder;
}

@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("apply_template")
.setDescription("Apply a permission template to one or several projects.<br />" +
"The project id or project key must be provided.<br />" +
"It requires administration permissions to access.")
.setPost(true)
.setSince("5.2")
.setHandler(this);

createExplicitTemplateId(action);
createProjectParameter(action);
}

@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
String templateUuid = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID_EXPLICIT);

DbSession dbSession = dbClient.openSession(false);
try {
PermissionTemplateDto template = finder.getTemplate(dbSession, templateUuid);
ComponentDto project = finder.getProject(dbSession, wsRequest);

ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create(
template.getUuid(),
singletonList(project.key()));
permissionService.applyPermissionTemplate(query);
} finally {
dbClient.closeSession(dbSession);
}

wsResponse.noContent();
}
}
Expand Up @@ -35,7 +35,6 @@
import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.security.DefaultGroups.isAnyone;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkRequest;

public class PermissionDependenciesFinder {
private final DbClient dbClient;
Expand All @@ -59,10 +58,9 @@ Optional<ComponentDto> searchProject(DbSession dbSession, PermissionRequest requ
}

public ComponentDto getProject(DbSession dbSession, Request wsRequest) {
Optional<WsProjectRef> projectRef = WsProjectRef.optionalFromRequest(wsRequest);
checkRequest(projectRef.isPresent(), "The project id or the project key must be provided.");
WsProjectRef projectRef = WsProjectRef.fromRequest(wsRequest);

return componentFinder.getProjectByUuidOrKey(dbSession, projectRef.get().uuid(), projectRef.get().key());
return componentFinder.getProjectByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key());
}

String getGroupName(DbSession dbSession, PermissionRequest request) {
Expand Down
Expand Up @@ -43,6 +43,7 @@ protected void configureModule() {
CreateTemplateAction.class,
UpdateTemplateAction.class,
DeleteTemplateAction.class,
ApplyTemplateAction.class,
// utility classes
PermissionChangeBuilder.class,
SearchProjectPermissionsDataLoader.class,
Expand Down
Expand Up @@ -45,14 +45,16 @@ private WsProjectRef(Request wsRequest) {
}

static Optional<WsProjectRef> optionalFromRequest(Request wsRequest) {
if (hasNoProjectParam(wsRequest)) {
if (!hasProjectParam(wsRequest)) {
return Optional.absent();
}

return Optional.of(new WsProjectRef(wsRequest));
}

static WsProjectRef fromRequest(Request wsRequest) {
checkRequest(hasProjectParam(wsRequest), "Project id or project key must be provided, not both.");

return new WsProjectRef(wsRequest);
}

Expand All @@ -66,7 +68,7 @@ String key() {
return this.key;
}

private static boolean hasNoProjectParam(Request wsRequest) {
return !wsRequest.hasParam(PARAM_PROJECT_ID) && !wsRequest.hasParam(PARAM_PROJECT_KEY);
private static boolean hasProjectParam(Request wsRequest) {
return wsRequest.hasParam(PARAM_PROJECT_ID) || wsRequest.hasParam(PARAM_PROJECT_KEY);
}
}

0 comments on commit f8559bc

Please sign in to comment.