Skip to content

Commit

Permalink
Create WS Request Group class
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Aug 24, 2015
1 parent ce39ae2 commit 5032edb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 38 deletions.
Expand Up @@ -36,8 +36,6 @@
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.PermissionRequestValidator.validateNotAnyoneAndAdminPermission; import static org.sonar.server.permission.PermissionRequestValidator.validateNotAnyoneAndAdminPermission;
import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPermission; import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_NAME;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION; import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY; import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter; import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
Expand Down Expand Up @@ -79,19 +77,18 @@ public void handle(Request wsRequest, Response wsResponse) throws Exception {


String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
Long groupIdParam = wsRequest.paramAsLong(PARAM_GROUP_ID); WsGroup group = WsGroup.fromRequest(wsRequest);
String groupName = wsRequest.param(PARAM_GROUP_NAME);


DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { try {
validateProjectPermission(permission); validateProjectPermission(permission);
validateNotAnyoneAndAdminPermission(permission, groupName); validateNotAnyoneAndAdminPermission(permission, group.name());


PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey); PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey);
GroupDto group = dependenciesFinder.getGroup(dbSession, groupIdParam, groupName); GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group);


if (!groupAlreadyAdded(dbSession, template.getId(), group, permission)) { if (!groupAlreadyAdded(dbSession, template.getId(), groupDto, permission)) {
Long groupId = group == null ? null : group.getId(); Long groupId = groupDto == null ? null : groupDto.getId();
dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), groupId, permission); dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), groupId, permission);
} }
} finally { } finally {
Expand Down
Expand Up @@ -22,7 +22,6 @@


import com.google.common.base.Optional; import com.google.common.base.Optional;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
Expand All @@ -35,7 +34,6 @@
import static org.sonar.api.security.DefaultGroups.ANYONE; import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.security.DefaultGroups.isAnyone; import static org.sonar.api.security.DefaultGroups.isAnyone;
import static org.sonar.server.ws.WsUtils.checkFound; import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkRequest;


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


String getGroupName(DbSession dbSession, PermissionRequest request) { String getGroupName(DbSession dbSession, PermissionRequest request) {
GroupDto group = getGroup(dbSession, request.groupId(), request.groupName()); GroupDto group = getGroup(dbSession, request.group());


return group == null ? ANYONE : group.getName(); return group == null ? ANYONE : group.getName();
} }
Expand All @@ -68,25 +66,27 @@ String getGroupName(DbSession dbSession, PermissionRequest request) {
* @return null if it's the anyone group * @return null if it's the anyone group
*/ */
@CheckForNull @CheckForNull
GroupDto getGroup(DbSession dbSession, @Nullable Long groupId, @Nullable String groupName) { GroupDto getGroup(DbSession dbSession, WsGroup group) {
checkRequest(groupId != null ^ groupName != null, "Group name or group id must be provided, not both."); Long groupId = group.id();
String groupName = group.name();

if (isAnyone(groupName)) { if (isAnyone(groupName)) {
return null; return null;
} }


GroupDto group = null; GroupDto groupDto = null;


if (groupId != null) { if (groupId != null) {
group = checkFound(dbClient.groupDao().selectById(dbSession, groupId), groupDto = checkFound(dbClient.groupDao().selectById(dbSession, groupId),
format("Group with id '%d' is not found", groupId)); format("Group with id '%d' is not found", groupId));
} }


if (groupName != null) { if (groupName != null) {
group = checkFound(dbClient.groupDao().selectByName(dbSession, groupName), groupDto = checkFound(dbClient.groupDao().selectByName(dbSession, groupName),
format("Group with name '%s' is not found", groupName)); format("Group with name '%s' is not found", groupName));
} }


return group; return groupDto;
} }


UserDto getUser(DbSession dbSession, String userLogin) { UserDto getUser(DbSession dbSession, String userLogin) {
Expand Down
Expand Up @@ -28,21 +28,18 @@
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.SELECTED; import static org.sonar.api.server.ws.WebService.Param.SELECTED;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID; import static org.sonar.server.permission.PermissionRequestValidator.validateGlobalPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_NAME; import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION; import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_KEY; import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_KEY;
import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_UUID; import static org.sonar.server.permission.ws.Parameters.PARAM_PROJECT_UUID;
import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN; import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
import static org.sonar.server.permission.PermissionRequestValidator.validateGlobalPermission;
import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.checkRequest;


class PermissionRequest { class PermissionRequest {
private final String permission; private final String permission;
private final String userLogin; private final String userLogin;
private final Long groupId; private final WsGroup group;
private final String groupName;
private final String projectUuid; private final String projectUuid;
private final String projectKey; private final String projectKey;
private final boolean hasProject; private final boolean hasProject;
Expand All @@ -54,8 +51,7 @@ class PermissionRequest {
private PermissionRequest(Builder builder) { private PermissionRequest(Builder builder) {
permission = builder.permission; permission = builder.permission;
userLogin = builder.userLogin; userLogin = builder.userLogin;
groupId = builder.groupId; group = builder.group;
groupName = builder.groupName;
projectUuid = builder.projectUuid; projectUuid = builder.projectUuid;
projectKey = builder.projectKey; projectKey = builder.projectKey;
hasProject = builder.hasProject; hasProject = builder.hasProject;
Expand All @@ -76,8 +72,7 @@ static class Builder {
private String permission; private String permission;
private String userLogin; private String userLogin;


private Long groupId; private WsGroup group;
private String groupName;
private String projectUuid; private String projectUuid;
private String projectKey; private String projectKey;
private boolean hasProject; private boolean hasProject;
Expand Down Expand Up @@ -148,11 +143,7 @@ private void setUserLogin(Request request) {


private void setGroup(Request request) { private void setGroup(Request request) {
if (withGroup) { if (withGroup) {
Long groupIdParam = request.paramAsLong(PARAM_GROUP_ID); this.group = WsGroup.fromRequest(request);
String groupNameParam = request.param(PARAM_GROUP_NAME);
checkRequest(groupIdParam != null ^ groupNameParam != null, "Group name or group id must be provided, not both.");
this.groupId = groupIdParam;
this.groupName = groupNameParam;
} }
} }


Expand Down Expand Up @@ -187,12 +178,8 @@ String userLogin() {
return userLogin; return userLogin;
} }


Long groupId() { WsGroup group() {
return groupId; return group;
}

String groupName() {
return groupName;
} }


String projectUuid() { String projectUuid() {
Expand Down
@@ -0,0 +1,61 @@
/*
* 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 javax.annotation.Nullable;
import org.sonar.api.server.ws.Request;

import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_NAME;
import static org.sonar.server.ws.WsUtils.checkRequest;

/**
* Group from a WS request. Guaranties the group id or the group name is provided, not both.
*/
class WsGroup {

private final Long id;
private final String name;

private WsGroup(Long id, String name) {
checkRequest(id != null ^ name != null, "Group name or group id must be provided, not both.");

this.id = id;
this.name = name;
}

static WsGroup fromRequest(Request wsRequest) {
Long id = wsRequest.paramAsLong(PARAM_GROUP_ID);
String name = wsRequest.param(PARAM_GROUP_NAME);

return new WsGroup(id, name);
}

@Nullable
Long id() {
return this.id;
}

@Nullable
String name() {
return this.name;
}
}

0 comments on commit 5032edb

Please sign in to comment.