diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-26 10:08:25 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-27 09:50:00 +0200 |
commit | 534f06f13130dda4c572a4192ae6e8b0b9c48b30 (patch) | |
tree | feeb74935bced02914f120f4601f6bafed3360b5 | |
parent | 61050e0245725fc9fdca23825d49ccf3cfa2bba2 (diff) | |
download | sonarqube-534f06f13130dda4c572a4192ae6e8b0b9c48b30.tar.gz sonarqube-534f06f13130dda4c572a4192ae6e8b0b9c48b30.zip |
SONAR-6497 WS permissions/update/template update a permission template
31 files changed, 1053 insertions, 86 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java index 0fed05602ab..cff70f7e089 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionFinder.java @@ -152,7 +152,7 @@ public class PermissionFinder { } private Long templateId(String templateKey) { - PermissionTemplateDto dto = permissionTemplateDao.selectTemplateByKey(templateKey); + PermissionTemplateDto dto = permissionTemplateDao.selectByKey(templateKey); if (dto == null) { throw new NotFoundException(String.format("Template '%s' does not exist", templateKey)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionRequestValidator.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionRequestValidator.java index 66292b48b24..d7c256abdd8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionRequestValidator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionRequestValidator.java @@ -25,9 +25,6 @@ import java.util.regex.PatternSyntaxException; import javax.annotation.Nullable; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.permission.ProjectPermissions; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.server.exceptions.BadRequestException; import static com.google.common.base.Strings.isNullOrEmpty; @@ -69,12 +66,4 @@ public class PermissionRequestValidator { throw new BadRequestException(format("The 'projectPattern' parameter must be a valid Java regular expression. '%s' was passed", projectPattern)); } } - - public static void validateTemplateNameForUpdate(DbSession dbSession, DbClient dbClient, String templateName, long templateId) { - checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK); - - PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName); - checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == templateId, - format(MSG_TEMPLATE_WITH_SAME_NAME, templateName)); - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java index 6962215643a..8ae7d6eaaac 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java @@ -44,7 +44,6 @@ import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjec import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK; import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME; import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPattern; -import static org.sonar.server.permission.PermissionRequestValidator.validateTemplateNameForUpdate; import static org.sonar.server.ws.WsUtils.checkRequest; /** @@ -123,7 +122,7 @@ public class PermissionTemplateService { try { checkGlobalAdminUser(userSession); - validateTemplateNameForUpdate(dbSession, dbClient, newName, templateId); + validateTemplateNameForUpdate(dbSession, newName, templateId); validateProjectPattern(projectPattern); permissionTemplateDao.update(templateId, newName, newDescription, projectPattern); } finally { @@ -221,4 +220,16 @@ public class PermissionTemplateService { PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName); checkRequest(permissionTemplateWithSameName == null, format(MSG_TEMPLATE_WITH_SAME_NAME, templateName)); } + + /** + * @deprecated since 5.2 + */ + @Deprecated + private void validateTemplateNameForUpdate(DbSession dbSession, String templateName, long templateId) { + checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK); + + PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName); + checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == templateId, + format(MSG_TEMPLATE_WITH_SAME_NAME, templateName)); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java index 28063e8bdaf..5b19b520787 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java @@ -87,7 +87,7 @@ abstract class PermissionTemplateUpdater { } private Long getTemplateId(String key) { - PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().selectTemplateByKey(key); + PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().selectByKey(key); if (permissionTemplateDto == null) { throw new BadRequestException("Unknown template: " + key); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupToTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupToTemplateAction.java index c579546a7c2..9c4bbf1b31a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupToTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupToTemplateAction.java @@ -37,7 +37,7 @@ import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobal import static org.sonar.server.permission.PermissionRequestValidator.validateNotAnyoneAndAdminPermission; 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_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY; import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter; import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter; import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter; @@ -75,7 +75,7 @@ public class AddGroupToTemplateAction implements PermissionsWsAction { public void handle(Request wsRequest, Response wsResponse) throws Exception { checkGlobalAdminUser(userSession); - String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); + String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY); String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); WsGroup group = WsGroup.fromRequest(wsRequest); @@ -84,7 +84,7 @@ public class AddGroupToTemplateAction implements PermissionsWsAction { validateProjectPermission(permission); validateNotAnyoneAndAdminPermission(permission, group.name()); - PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateKey); GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group); if (!groupAlreadyAdded(dbSession, template.getId(), groupDto, permission)) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserToTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserToTemplateAction.java index 510bbac1f52..22443cfb09a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserToTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserToTemplateAction.java @@ -38,7 +38,7 @@ import static org.sonar.db.user.GroupMembershipQuery.IN; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; 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_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY; import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN; import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter; import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameter; @@ -74,14 +74,14 @@ public class AddUserToTemplateAction implements PermissionsWsAction { public void handle(Request wsRequest, Response wsResponse) throws Exception { checkGlobalAdminUser(userSession); - String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); + String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY); String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); final String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN); DbSession dbSession = dbClient.openSession(false); try { validateProjectPermission(permission); - PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateKey); UserDto user = dependenciesFinder.getUser(dbSession, userLogin); if (!isUserAlreadyAdded(dbSession, template.getId(), userLogin, permission)) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/CreateTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/CreateTemplateAction.java index 1036ad990a2..024b825e5b2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/CreateTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/CreateTemplateAction.java @@ -31,7 +31,6 @@ import org.sonar.server.user.UserSession; import org.sonarqube.ws.Permissions; import org.sonarqube.ws.Permissions.PermissionTemplate; -import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK; import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME; @@ -39,7 +38,10 @@ import static org.sonar.server.permission.PermissionRequestValidator.validatePro import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION; import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME; import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN; +import static org.sonar.server.permission.ws.Parameters.createTemplateDescriptionParameter; +import static org.sonar.server.permission.ws.Parameters.createTemplateProjectKeyPatternParameter; import static org.sonar.server.permission.ws.PermissionTemplateDtoBuilder.create; +import static org.sonar.server.permission.ws.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -69,13 +71,8 @@ public class CreateTemplateAction implements PermissionsWsAction { .setDescription("Name") .setExampleValue("Financial Service Permissions"); - action.createParam(PARAM_TEMPLATE_PATTERN) - .setDescription("Project key pattern. Must be a valid Java regular expression") - .setExampleValue(".*\\.finance\\..*"); - - action.createParam(PARAM_TEMPLATE_DESCRIPTION) - .setDescription("Description") - .setExampleValue("Permissions for all projects related to the financial service"); + createTemplateProjectKeyPatternParameter(action); + createTemplateDescriptionParameter(action); } @Override @@ -117,18 +114,8 @@ public class CreateTemplateAction implements PermissionsWsAction { return template; } - private Permissions.CreatePermissionTemplateResponse buildResponse(PermissionTemplateDto permissionTemplate) { - PermissionTemplate.Builder permissionTemplateBuilder = PermissionTemplate.newBuilder() - .setKey(permissionTemplate.getKee()) - .setName(permissionTemplate.getName()) - .setCreatedAt(formatDateTime(permissionTemplate.getCreatedAt())) - .setUpdatedAt(formatDateTime(permissionTemplate.getUpdatedAt())); - if (permissionTemplate.getDescription() != null) { - permissionTemplateBuilder.setDescription(permissionTemplate.getDescription()); - } - if (permissionTemplate.getKeyPattern() != null) { - permissionTemplateBuilder.setProjectPattern(permissionTemplate.getKeyPattern()); - } + private Permissions.CreatePermissionTemplateResponse buildResponse(PermissionTemplateDto permissionTemplateDto) { + PermissionTemplate permissionTemplateBuilder = toPermissionTemplateResponse(permissionTemplateDto); return Permissions.CreatePermissionTemplateResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java index a06aa8e19ab..7d11fac90c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java @@ -32,7 +32,8 @@ class Parameters { static final String PARAM_PROJECT_UUID = "projectId"; static final String PARAM_PROJECT_KEY = "projectKey"; static final String PARAM_USER_LOGIN = "login"; - static final String PARAM_TEMPLATE_KEY = "templateKey"; + static final String PARAM_LONG_TEMPLATE_KEY = "templateKey"; + static final String PARAM_TEMPLATE_KEY = "key"; static final String PARAM_TEMPLATE_NAME = "name"; static final String PARAM_TEMPLATE_DESCRIPTION = "description"; static final String PARAM_TEMPLATE_PATTERN = "projectPattern"; @@ -98,9 +99,21 @@ class Parameters { } static void createTemplateKeyParameter(NewAction action) { - action.createParam(PARAM_TEMPLATE_KEY) + action.createParam(PARAM_LONG_TEMPLATE_KEY) .setRequired(true) .setDescription("Template key") .setExampleValue("developer_template_20150820_170218"); } + + static void createTemplateProjectKeyPatternParameter(NewAction action) { + action.createParam(PARAM_TEMPLATE_PATTERN) + .setDescription("Project key pattern. Must be a valid Java regular expression") + .setExampleValue(".*\\.finance\\..*"); + } + + static void createTemplateDescriptionParameter(NewAction action) { + action.createParam(PARAM_TEMPLATE_DESCRIPTION) + .setDescription("Description") + .setExampleValue("Permissions for all projects related to the financial service"); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java index 5ec9803450f..a6a4e862d8c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java @@ -95,8 +95,8 @@ public class PermissionDependenciesFinder { format("User with login '%s' is not found'", userLogin)); } - PermissionTemplateDto getTemplate(String templateKey) { - return checkFound(dbClient.permissionTemplateDao().selectTemplateByKey(templateKey), + PermissionTemplateDto getTemplate(DbSession dbSession, String templateKey) { + return checkFound(dbClient.permissionTemplateDao().selectByKey(dbSession, templateKey), format("Permission template with key '%s' is not found", templateKey)); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionTemplateDtoToPermissionTemplateResponse.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionTemplateDtoToPermissionTemplateResponse.java new file mode 100644 index 00000000000..443f3ea8d94 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionTemplateDtoToPermissionTemplateResponse.java @@ -0,0 +1,53 @@ +/* + * 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 com.google.common.base.Function; +import javax.annotation.Nonnull; +import org.sonar.api.utils.DateUtils; +import org.sonar.db.permission.PermissionTemplateDto; +import org.sonarqube.ws.Permissions.PermissionTemplate; + +class PermissionTemplateDtoToPermissionTemplateResponse { + + static PermissionTemplate toPermissionTemplateResponse(PermissionTemplateDto dto) { + return Singleton.INSTANCE.apply(dto); + } + + private enum Singleton implements Function<PermissionTemplateDto, PermissionTemplate> { + INSTANCE; + @Override + public PermissionTemplate apply(@Nonnull PermissionTemplateDto permissionTemplate) { + PermissionTemplate.Builder permissionTemplateBuilder = PermissionTemplate.newBuilder() + .setKey(permissionTemplate.getKee()) + .setName(permissionTemplate.getName()) + .setCreatedAt(DateUtils.formatDateTime(permissionTemplate.getCreatedAt())) + .setUpdatedAt(DateUtils.formatDateTime(permissionTemplate.getUpdatedAt())); + if (permissionTemplate.getDescription() != null) { + permissionTemplateBuilder.setDescription(permissionTemplate.getDescription()); + } + if (permissionTemplate.getKeyPattern() != null) { + permissionTemplateBuilder.setProjectPattern(permissionTemplate.getKeyPattern()); + } + return permissionTemplateBuilder.build(); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java index 48b3b12a733..2ff2cfcd9d7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionsWsModule.java @@ -42,6 +42,7 @@ public class PermissionsWsModule extends Module { RemoveUserFromTemplateAction.class, AddGroupToTemplateAction.class, RemoveGroupFromTemplateAction.class, - CreateTemplateAction.class); + CreateTemplateAction.class, + UpdateTemplateAction.class); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateAction.java index 79147e7cf33..3a625cb0b53 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateAction.java @@ -32,7 +32,7 @@ import org.sonar.server.user.UserSession; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; 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_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY; import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter; import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter; import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter; @@ -70,7 +70,7 @@ public class RemoveGroupFromTemplateAction implements PermissionsWsAction { public void handle(Request wsRequest, Response wsResponse) throws Exception { checkGlobalAdminUser(userSession); - String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); + String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY); String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); WsGroup group = WsGroup.fromRequest(wsRequest); @@ -78,7 +78,7 @@ public class RemoveGroupFromTemplateAction implements PermissionsWsAction { try { validateProjectPermission(permission); - PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateKey); GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group); Long groupId = groupDto == null ? null : groupDto.getId(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserFromTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserFromTemplateAction.java index 0c0c9742436..4d0474b6af3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserFromTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserFromTemplateAction.java @@ -32,7 +32,7 @@ import org.sonar.server.user.UserSession; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; 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_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY; import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN; import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter; import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameter; @@ -68,14 +68,14 @@ public class RemoveUserFromTemplateAction implements PermissionsWsAction { public void handle(Request wsRequest, Response wsResponse) throws Exception { checkGlobalAdminUser(userSession); - String templateKey = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); + String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY); String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN); DbSession dbSession = dbClient.openSession(false); try { validateProjectPermission(permission); - PermissionTemplateDto template = dependenciesFinder.getTemplate(templateKey); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, templateKey); UserDto user = dependenciesFinder.getUser(dbSession, userLogin); dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getId(), user.getId(), permission); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UpdateTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UpdateTemplateAction.java new file mode 100644 index 00000000000..bab5ce1c3ad --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/UpdateTemplateAction.java @@ -0,0 +1,142 @@ +/* + * 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 java.util.Date; +import javax.annotation.Nullable; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.permission.PermissionTemplateDto; +import org.sonar.server.user.UserSession; +import org.sonarqube.ws.Permissions.PermissionTemplate; +import org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse; + +import static com.google.common.base.Objects.firstNonNull; +import static java.lang.String.format; +import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; +import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK; +import static org.sonar.server.permission.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME; +import static org.sonar.server.permission.PermissionRequestValidator.validateProjectPattern; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN; +import static org.sonar.server.permission.ws.Parameters.createTemplateDescriptionParameter; +import static org.sonar.server.permission.ws.Parameters.createTemplateProjectKeyPatternParameter; +import static org.sonar.server.permission.ws.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse; +import static org.sonar.server.ws.WsUtils.checkRequest; +import static org.sonar.server.ws.WsUtils.writeProtobuf; + +public class UpdateTemplateAction implements PermissionsWsAction { + private final DbClient dbClient; + private final UserSession userSession; + private final System2 system; + private final PermissionDependenciesFinder finder; + + public UpdateTemplateAction(DbClient dbClient, UserSession userSession, System2 system, PermissionDependenciesFinder finder) { + this.dbClient = dbClient; + this.userSession = userSession; + this.system = system; + this.finder = finder; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction("update_template") + .setDescription("Update a permission template.<br />" + + "It requires administration permissions to access.") + .setResponseExample(getClass().getResource("update_template-example.json")) + .setSince("5.2") + .setPost(true) + .setHandler(this); + + action.createParam(PARAM_TEMPLATE_KEY) + .setRequired(true) + .setDescription("Key") + .setExampleValue("af8cb8cc-1e78-4c4e-8c00-ee8e814009a5"); + + action.createParam(PARAM_TEMPLATE_NAME) + .setDescription("Name") + .setExampleValue("Financial Service Permissions"); + + createTemplateProjectKeyPatternParameter(action); + createTemplateDescriptionParameter(action); + } + + @Override + public void handle(Request wsRequest, Response wsResponse) throws Exception { + checkGlobalAdminUser(userSession); + + String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY); + String nameParam = wsRequest.param(PARAM_TEMPLATE_NAME); + String descriptionParam = wsRequest.param(PARAM_TEMPLATE_DESCRIPTION); + String projectPatternParam = wsRequest.param(PARAM_TEMPLATE_PATTERN); + + DbSession dbSession = dbClient.openSession(false); + try { + PermissionTemplateDto templateToUpdate = getAndBuildTemplateToUpdate(dbSession, key, nameParam, descriptionParam, projectPatternParam); + validateTemplate(dbSession, templateToUpdate); + PermissionTemplateDto updatedTemplate = updateTemplate(dbSession, templateToUpdate); + + UpdatePermissionTemplateResponse response = buildResponse(updatedTemplate); + writeProtobuf(response, wsRequest, wsResponse); + } finally { + dbClient.closeSession(dbSession); + } + } + + private void validateTemplate(DbSession dbSession, PermissionTemplateDto templateToUpdate) { + validateTemplateNameForUpdate(dbSession, templateToUpdate.getName(), templateToUpdate.getId()); + validateProjectPattern(templateToUpdate.getKeyPattern()); + } + + private PermissionTemplateDto getAndBuildTemplateToUpdate(DbSession dbSession, String key, @Nullable String newName, @Nullable String newDescription, + @Nullable String newProjectKeyPattern) { + PermissionTemplateDto templateToUpdate = finder.getTemplate(dbSession, key); + templateToUpdate.setName(firstNonNull(newName, templateToUpdate.getName())); + templateToUpdate.setDescription(firstNonNull(newDescription, templateToUpdate.getDescription())); + templateToUpdate.setKeyPattern(firstNonNull(newProjectKeyPattern, templateToUpdate.getKeyPattern())); + templateToUpdate.setUpdatedAt(new Date(system.now())); + + return templateToUpdate; + } + + private PermissionTemplateDto updateTemplate(DbSession dbSession, PermissionTemplateDto templateToUpdate) { + return dbClient.permissionTemplateDao().update(dbSession, templateToUpdate); + } + + private UpdatePermissionTemplateResponse buildResponse(PermissionTemplateDto permissionTemplate) { + PermissionTemplate permissionTemplateBuilder = toPermissionTemplateResponse(permissionTemplate); + return UpdatePermissionTemplateResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build(); + } + + private void validateTemplateNameForUpdate(DbSession dbSession, String templateName, long templateId) { + checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK); + + PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName); + checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == templateId, + format(MSG_TEMPLATE_WITH_SAME_NAME, templateName)); + } +} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/create_template-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/create_template-example.json index 51c149190cc..922c0ea3049 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/create_template-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/create_template-example.json @@ -3,6 +3,7 @@ "key": "af8cb8cc-1e78-4c4e-8c00-ee8e814009a5", "name": "Finance", "description": "Permissions for financial related projects", + "projectPattern": ".*\\.finance\\..*", "createdAt": "2015-08-25T16:18:48+0200", "updatedAt": "2015-08-25T16:18:48+0200" } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/update_template-example.json b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/update_template-example.json new file mode 100644 index 00000000000..922c0ea3049 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/permission/ws/update_template-example.json @@ -0,0 +1,10 @@ +{ + "permissionTemplate": { + "key": "af8cb8cc-1e78-4c4e-8c00-ee8e814009a5", + "name": "Finance", + "description": "Permissions for financial related projects", + "projectPattern": ".*\\.finance\\..*", + "createdAt": "2015-08-25T16:18:48+0200", + "updatedAt": "2015-08-25T16:18:48+0200" + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java index b504a8d74c6..b3592ca78a2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionFinderTest.java @@ -264,7 +264,7 @@ public class PermissionFinderTest { @Test public void find_users_from_permission_template() { - when(permissionTemplateDao.selectTemplateByKey(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setKee("my_template")); + when(permissionTemplateDao.selectByKey(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setKee("my_template")); when(permissionTemplateDao.selectUsers(any(DbSession.class), any(PermissionQuery.class), anyLong(), anyInt(), anyInt())).thenReturn( newArrayList(new UserWithPermissionDto().setName("user1").setPermission("user")) @@ -279,7 +279,7 @@ public class PermissionFinderTest { @Test public void fail_to_find_users_from_permission_template_when_template_not_found() { - when(permissionTemplateDao.selectTemplateByKey(anyString())).thenReturn(null); + when(permissionTemplateDao.selectByKey(anyString())).thenReturn(null); try { underTest.findUsersWithPermissionTemplate(PermissionQuery.builder().permission("user").template("Unknown").build()); @@ -291,7 +291,7 @@ public class PermissionFinderTest { @Test public void find_groups_from_permission_template() { - when(permissionTemplateDao.selectTemplateByKey(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setKee("my_template")); + when(permissionTemplateDao.selectByKey(anyString())).thenReturn(new PermissionTemplateDto().setId(1L).setKee("my_template")); when(permissionTemplateDao.selectGroups(any(DbSession.class), any(PermissionQuery.class), anyLong())).thenReturn( newArrayList(new GroupWithPermissionDto().setName("users").setPermission("user")) @@ -305,7 +305,7 @@ public class PermissionFinderTest { @Test public void fail_to_find_groups_from_permission_template_when_template_not_found() { - when(permissionTemplateDao.selectTemplateByKey(anyString())).thenReturn(null); + when(permissionTemplateDao.selectByKey(anyString())).thenReturn(null); try { underTest.findGroupsWithPermissionTemplate(PermissionQuery.builder().permission("user").template("Unknown").build()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java index a3cff58ecb7..e4ceb1b7180 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java @@ -147,7 +147,7 @@ public class PermissionTemplateServiceTest { @Test public void should_delete_permission_template() { - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.deletePermissionTemplate(1L); @@ -271,7 +271,7 @@ public class PermissionTemplateServiceTest { public void should_add_user_permission() { UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user"); when(userDao.selectActiveUserByLogin("user")).thenReturn(userDto); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.addUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "user"); @@ -283,7 +283,7 @@ public class PermissionTemplateServiceTest { expected.expect(BadRequestException.class); expected.expectMessage("Unknown user:"); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); when(userDao.selectActiveUserByLogin("unknown")).thenReturn(null); underTest.addUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "unknown"); @@ -293,7 +293,7 @@ public class PermissionTemplateServiceTest { public void should_remove_user_permission() { UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user"); when(userDao.selectActiveUserByLogin("user")).thenReturn(userDto); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.removeUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "user"); @@ -304,7 +304,7 @@ public class PermissionTemplateServiceTest { public void should_add_group_permission() { GroupDto groupDto = new GroupDto().setId(1L).setName("group"); when(groupDao.selectByName(any(DbSession.class), eq("group"))).thenReturn(groupDto); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "group"); @@ -316,7 +316,7 @@ public class PermissionTemplateServiceTest { expected.expect(BadRequestException.class); expected.expectMessage("Unknown group:"); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); when(groupDao.selectByName(any(DbSession.class), eq("unknown"))).thenReturn(null); underTest.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "unknown"); @@ -326,7 +326,7 @@ public class PermissionTemplateServiceTest { public void should_remove_group_permission() { GroupDto groupDto = new GroupDto().setId(1L).setName("group"); when(groupDao.selectByName(any(DbSession.class), eq("group"))).thenReturn(groupDto); - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.removeGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "group"); @@ -335,7 +335,7 @@ public class PermissionTemplateServiceTest { @Test public void should_add_permission_to_anyone_group() { - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "Anyone"); @@ -345,7 +345,7 @@ public class PermissionTemplateServiceTest { @Test public void should_remove_permission_from_anyone_group() { - when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); + when(permissionTemplateDao.selectByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE); underTest.removeGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "Anyone"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java index 424e0eb9f8c..270bf15228e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateUpdaterTest.java @@ -76,7 +76,7 @@ public class PermissionTemplateUpdaterTest { public void should_execute_on_valid_parameters() { final PermissionTemplateDao permissionTemplateDao = mock(PermissionTemplateDao.class); - when(permissionTemplateDao.selectTemplateByKey("my_template")).thenReturn(new PermissionTemplateDto().setId(1L)); + when(permissionTemplateDao.selectByKey("my_template")).thenReturn(new PermissionTemplateDto().setId(1L)); when(dbClient.permissionTemplateDao()).thenReturn(permissionTemplateDao); PermissionTemplateUpdater updater = @@ -97,7 +97,7 @@ public class PermissionTemplateUpdaterTest { expected.expectMessage("Unknown template:"); final PermissionTemplateDao permissionTemplateDao = mock(PermissionTemplateDao.class); - when(permissionTemplateDao.selectTemplateByKey("my_template")).thenReturn(null); + when(permissionTemplateDao.selectByKey("my_template")).thenReturn(null); PermissionTemplateUpdater updater = new PermissionTemplateUpdater(dbClient, userSessionRule, "my_template", UserRole.USER, "user") { @@ -113,7 +113,7 @@ public class PermissionTemplateUpdaterTest { expected.expect(BadRequestException.class); final PermissionTemplateDao permissionTemplateDao = mock(PermissionTemplateDao.class); - when(permissionTemplateDao.selectTemplateByKey("my_template")).thenReturn(new PermissionTemplateDto().setId(1L)); + when(permissionTemplateDao.selectByKey("my_template")).thenReturn(new PermissionTemplateDto().setId(1L)); when(dbClient.permissionTemplateDao()).thenReturn(permissionTemplateDao); PermissionTemplateUpdater updater = diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupToTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupToTemplateActionTest.java index ba439ba42bf..4b4d7320639 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupToTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupToTemplateActionTest.java @@ -60,7 +60,7 @@ import static org.sonar.db.user.GroupMembershipQuery.IN; import static org.sonar.db.user.GroupTesting.newGroupDto; import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID; 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_LONG_TEMPLATE_KEY; @Category(DbTests.class) public class AddGroupToTemplateActionTest { @@ -103,7 +103,7 @@ public class AddGroupToTemplateActionTest { @Test public void add_with_group_id() { ws.newRequest() - .setParam(PARAM_TEMPLATE_KEY, permissionTemplate.getKee()) + .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee()) .setParam(PARAM_PERMISSION, CODEVIEWER) .setParam(PARAM_GROUP_ID, String.valueOf(group.getId())) .execute(); @@ -200,7 +200,7 @@ public class AddGroupToTemplateActionTest { request.setParam(Parameters.PARAM_GROUP_NAME, groupName); } if (templateKey != null) { - request.setParam(PARAM_TEMPLATE_KEY, templateKey); + request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey); } if (permission != null) { request.setParam(Parameters.PARAM_PERMISSION, permission); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserToTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserToTemplateActionTest.java index 4cb43afecb8..a5b3bf1f66c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserToTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserToTemplateActionTest.java @@ -168,7 +168,7 @@ public class AddUserToTemplateActionTest { request.setParam(Parameters.PARAM_USER_LOGIN, userLogin); } if (templateKey != null) { - request.setParam(Parameters.PARAM_TEMPLATE_KEY, templateKey); + request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey); } if (permission != null) { request.setParam(Parameters.PARAM_PERMISSION, permission); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java index fe1c69d1367..59edfb47928 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionsWsModuleTest.java @@ -30,6 +30,6 @@ public class PermissionsWsModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new PermissionsWsModule().configure(container); - assertThat(container.size()).isEqualTo(19); + assertThat(container.size()).isEqualTo(20); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateActionTest.java index 5ac574c8752..54f1c514302 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateActionTest.java @@ -57,7 +57,7 @@ import static org.sonar.db.user.GroupMembershipQuery.IN; import static org.sonar.db.user.GroupTesting.newGroupDto; import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID; 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_LONG_TEMPLATE_KEY; @Category(DbTests.class) public class RemoveGroupFromTemplateActionTest { @@ -108,7 +108,7 @@ public class RemoveGroupFromTemplateActionTest { commit(); ws.newRequest() - .setParam(PARAM_TEMPLATE_KEY, permissionTemplate.getKee()) + .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee()) .setParam(PARAM_PERMISSION, PERMISSION) .setParam(PARAM_GROUP_ID, String.valueOf(group.getId())) .execute(); @@ -200,7 +200,7 @@ public class RemoveGroupFromTemplateActionTest { request.setParam(Parameters.PARAM_GROUP_NAME, groupName); } if (templateKey != null) { - request.setParam(PARAM_TEMPLATE_KEY, templateKey); + request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey); } if (permission != null) { request.setParam(Parameters.PARAM_PERMISSION, permission); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserFromTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserFromTemplateActionTest.java index aa181255977..7c8b3b383ba 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserFromTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserFromTemplateActionTest.java @@ -192,7 +192,7 @@ public class RemoveUserFromTemplateActionTest { request.setParam(Parameters.PARAM_USER_LOGIN, userLogin); } if (templateKey != null) { - request.setParam(Parameters.PARAM_TEMPLATE_KEY, templateKey); + request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey); } if (permission != null) { request.setParam(Parameters.PARAM_PERMISSION, permission); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UpdateTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UpdateTemplateActionTest.java new file mode 100644 index 00000000000..7cb9d82e3f1 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/UpdateTemplateActionTest.java @@ -0,0 +1,222 @@ +/* + * 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 java.util.Date; +import javax.annotation.Nullable; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.permission.PermissionTemplateDto; +import org.sonar.server.component.ComponentFinder; +import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.ForbiddenException; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.exceptions.UnauthorizedException; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.TestRequest; +import org.sonar.server.ws.TestResponse; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME; +import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN; +import static org.sonar.test.JsonAssert.assertJson; + +public class UpdateTemplateActionTest { + + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + @Rule + public UserSessionRule userSession = UserSessionRule.standalone(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + WsActionTester ws; + DbClient dbClient; + DbSession dbSession; + + PermissionTemplateDto templateDto; + + @Before + public void setUp() { + userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + System2 system = mock(System2.class); + when(system.now()).thenReturn(1_440_512_328_743L); + + dbClient = db.getDbClient(); + dbSession = db.getSession(); + PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)); + + ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder)); + + templateDto = insertTemplate(newPermissionTemplateDto() + .setName("Permission Template Name") + .setDescription("Permission Template Description") + .setKeyPattern(".*\\.pattern\\..*") + .setCreatedAt(new Date(1_000_000_000_000L)) + .setUpdatedAt(new Date(1_000_000_000_000L))); + commit(); + } + + @Test + public void update_all_permission_template_fields() { + TestResponse result = newRequest(templateDto.getKee(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*"); + + assertJson(result.getInput()).isSimilarTo(getClass().getResource("UpdateTemplateActionTest/update_template.json")); + PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance"); + assertThat(finance.getName()).isEqualTo("Finance"); + assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects"); + assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*"); + assertThat(finance.getKee()).isEqualTo(templateDto.getKee()); + assertThat(finance.getCreatedAt()).isEqualTo(templateDto.getCreatedAt()); + assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L); + } + + @Test + public void update_with_the_same_values() { + newRequest(templateDto.getKee(), templateDto.getName(), templateDto.getDescription(), templateDto.getKeyPattern()); + + PermissionTemplateDto updatedTemplate = dbClient.permissionTemplateDao().selectByKey(dbSession, templateDto.getKee()); + assertThat(updatedTemplate.getName()).isEqualTo(templateDto.getName()); + assertThat(updatedTemplate.getDescription()).isEqualTo(templateDto.getDescription()); + assertThat(updatedTemplate.getKeyPattern()).isEqualTo(templateDto.getKeyPattern()); + } + + @Test + public void update_name_only() { + newRequest(templateDto.getKee(), "Finance", null, null); + + PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance"); + assertThat(finance.getName()).isEqualTo("Finance"); + assertThat(finance.getDescription()).isEqualTo(templateDto.getDescription()); + assertThat(finance.getKeyPattern()).isEqualTo(templateDto.getKeyPattern()); + } + + @Test + public void fail_if_key_is_not_found() { + expectedException.expect(NotFoundException.class); + expectedException.expectMessage("Permission template with key 'unknown-key' is not found"); + + newRequest("unknown-key", null, null, null); + } + + @Test + public void fail_if_name_already_exists_in_another_template() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("A template with the name 'My Template' already exists (case insensitive)."); + + insertTemplate(newPermissionTemplateDto() + .setName("My Template") + .setKee("my-key") + .setCreatedAt(new Date(12345789L)) + .setUpdatedAt(new Date(12345789L))); + commit(); + + newRequest(templateDto.getKee(), "My Template", null, null); + } + + @Test + public void fail_if_key_is_not_provided() { + expectedException.expect(IllegalArgumentException.class); + + newRequest(null, "Finance", null, null); + } + + @Test + public void fail_if_name_empty() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("The template name must not be blank"); + + newRequest(templateDto.getKee(), "", null, null); + } + + @Test + public void fail_if_regexp_if_not_valid() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("The 'projectPattern' parameter must be a valid Java regular expression. '[azerty' was passed"); + + newRequest(templateDto.getKee(), "Finance", null, "[azerty"); + } + + @Test + public void fail_if_name_already_exists_in_database_case_insensitive() { + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("A template with the name 'Finance' already exists (case insensitive)."); + insertTemplate(newPermissionTemplateDto().setName("finance")); + commit(); + + newRequest(templateDto.getKee(), "Finance", null, null); + } + + @Test + public void fail_if_not_logged_in() { + expectedException.expect(UnauthorizedException.class); + userSession.anonymous(); + + newRequest(templateDto.getKee(), "Finance", null, null); + } + + @Test + public void fail_if_not_admin() { + expectedException.expect(ForbiddenException.class); + userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); + + newRequest(templateDto.getKee(), "Finance", null, null); + } + + private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) { + return dbClient.permissionTemplateDao().insert(dbSession, template); + } + + private void commit() { + dbSession.commit(); + } + + private TestResponse newRequest(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) { + TestRequest request = ws.newRequest(); + if (key != null) { + request.setParam(PARAM_TEMPLATE_KEY, key); + } + if (name != null) { + request.setParam(PARAM_TEMPLATE_NAME, name); + } + if (description != null) { + request.setParam(PARAM_TEMPLATE_DESCRIPTION, description); + } + if (projectPattern != null) { + request.setParam(PARAM_TEMPLATE_PATTERN, projectPattern); + } + + return request.execute(); + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/CreateTemplateActionTest/create_template.json b/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/CreateTemplateActionTest/create_template.json index 064d02f9ecf..47e35540fde 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/CreateTemplateActionTest/create_template.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/CreateTemplateActionTest/create_template.json @@ -2,6 +2,7 @@ "permissionTemplate": { "name": "Finance", "description": "Permissions for financially related projects", + "projectPattern": ".*\\.finance\\..*", "createdAt": "2015-08-25T16:18:48+0200", "updatedAt": "2015-08-25T16:18:48+0200" } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/UpdateTemplateActionTest/update_template.json b/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/UpdateTemplateActionTest/update_template.json new file mode 100644 index 00000000000..2010e558892 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/permission/ws/UpdateTemplateActionTest/update_template.json @@ -0,0 +1,9 @@ +{ + "permissionTemplate": { + "name": "Finance", + "description": "Permissions for financially related projects", + "projectPattern": ".*\\.finance\\..*", + "createdAt": "2001-09-09T03:46:40+0200", + "updatedAt": "2015-08-25T16:18:48+0200" + } +} diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java index 23164c14160..535d98648fd 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java @@ -127,15 +127,15 @@ public class PermissionTemplateDao implements Dao { } @CheckForNull - public PermissionTemplateDto selectTemplateByKey(DbSession session, String templateKey) { + public PermissionTemplateDto selectByKey(DbSession session, String templateKey) { return mapper(session).selectByKey(templateKey); } @CheckForNull - public PermissionTemplateDto selectTemplateByKey(String templateKey) { + public PermissionTemplateDto selectByKey(String templateKey) { DbSession session = myBatis.openSession(false); try { - return selectTemplateByKey(session, templateKey); + return selectByKey(session, templateKey); } finally { MyBatis.closeQuietly(session); } @@ -243,9 +243,11 @@ public class PermissionTemplateDao implements Dao { } } - public void update(DbSession session, PermissionTemplateDto permissionTemplate) { + public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) { mapper(session).update(permissionTemplate); session.commit(); + + return permissionTemplate; } /** @@ -342,7 +344,7 @@ public class PermissionTemplateDao implements Dao { */ @VisibleForTesting PermissionTemplateDto selectPermissionTemplateWithPermissions(DbSession session, String templateKey) { - PermissionTemplateDto permissionTemplateDto = selectTemplateByKey(session, templateKey); + PermissionTemplateDto permissionTemplateDto = selectByKey(session, templateKey); if (permissionTemplateDto == null) { throw new IllegalArgumentException("Could not retrieve permission template with key " + templateKey); } diff --git a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java index 0a301480170..3c672eb5082 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java @@ -124,7 +124,7 @@ public class PermissionTemplateDaoTest { public void should_select_permission_template_by_key() { dbTester.prepareDbUnit(getClass(), "selectPermissionTemplate.xml"); - PermissionTemplateDto permissionTemplate = underTest.selectTemplateByKey("my_template_20130102_030405"); + PermissionTemplateDto permissionTemplate = underTest.selectByKey("my_template_20130102_030405"); assertThat(permissionTemplate).isNotNull(); assertThat(permissionTemplate.getId()).isEqualTo(1L); @@ -235,7 +235,7 @@ public class PermissionTemplateDaoTest { PermissionTemplateDto permissionTemplateDto = new PermissionTemplateDto().setName("Test template").setKee("test_template"); PermissionTemplateDto templateWithPermissions = new PermissionTemplateDto().setKee("test_template"); underTest = mock(PermissionTemplateDao.class); - when(underTest.selectTemplateByKey(dbTester.getSession(), "test_template")).thenReturn(permissionTemplateDto); + when(underTest.selectByKey(dbTester.getSession(), "test_template")).thenReturn(permissionTemplateDto); when(underTest.selectPermissionTemplate(dbTester.getSession(), "test_template")).thenReturn(templateWithPermissions); when(underTest.selectPermissionTemplateWithPermissions(dbTester.getSession(), "test_template")).thenCallRealMethod(); diff --git a/sonar-ws/src/main/gen-java/org/sonarqube/ws/Permissions.java b/sonar-ws/src/main/gen-java/org/sonarqube/ws/Permissions.java index 6dbd19e54b2..81820590c3d 100644 --- a/sonar-ws/src/main/gen-java/org/sonarqube/ws/Permissions.java +++ b/sonar-ws/src/main/gen-java/org/sonarqube/ws/Permissions.java @@ -9422,6 +9422,514 @@ public final class Permissions { // @@protoc_insertion_point(class_scope:sonarqube.ws.permissions.CreatePermissionTemplateResponse) } + public interface UpdatePermissionTemplateResponseOrBuilder extends + // @@protoc_insertion_point(interface_extends:sonarqube.ws.permissions.UpdatePermissionTemplateResponse) + com.google.protobuf.MessageOrBuilder { + + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + boolean hasPermissionTemplate(); + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + org.sonarqube.ws.Permissions.PermissionTemplate getPermissionTemplate(); + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder getPermissionTemplateOrBuilder(); + } + /** + * Protobuf type {@code sonarqube.ws.permissions.UpdatePermissionTemplateResponse} + */ + public static final class UpdatePermissionTemplateResponse extends + com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:sonarqube.ws.permissions.UpdatePermissionTemplateResponse) + UpdatePermissionTemplateResponseOrBuilder { + // Use UpdatePermissionTemplateResponse.newBuilder() to construct. + private UpdatePermissionTemplateResponse(com.google.protobuf.GeneratedMessage.Builder<?> builder) { + super(builder); + this.unknownFields = builder.getUnknownFields(); + } + private UpdatePermissionTemplateResponse(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } + + private static final UpdatePermissionTemplateResponse defaultInstance; + public static UpdatePermissionTemplateResponse getDefaultInstance() { + return defaultInstance; + } + + public UpdatePermissionTemplateResponse getDefaultInstanceForType() { + return defaultInstance; + } + + private final com.google.protobuf.UnknownFieldSet unknownFields; + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private UpdatePermissionTemplateResponse( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + initFields(); + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!parseUnknownField(input, unknownFields, + extensionRegistry, tag)) { + done = true; + } + break; + } + case 10: { + org.sonarqube.ws.Permissions.PermissionTemplate.Builder subBuilder = null; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + subBuilder = permissionTemplate_.toBuilder(); + } + permissionTemplate_ = input.readMessage(org.sonarqube.ws.Permissions.PermissionTemplate.PARSER, extensionRegistry); + if (subBuilder != null) { + subBuilder.mergeFrom(permissionTemplate_); + permissionTemplate_ = subBuilder.buildPartial(); + } + bitField0_ |= 0x00000001; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e.getMessage()).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.sonarqube.ws.Permissions.internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.sonarqube.ws.Permissions.internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.class, org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.Builder.class); + } + + public static com.google.protobuf.Parser<UpdatePermissionTemplateResponse> PARSER = + new com.google.protobuf.AbstractParser<UpdatePermissionTemplateResponse>() { + public UpdatePermissionTemplateResponse parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new UpdatePermissionTemplateResponse(input, extensionRegistry); + } + }; + + @java.lang.Override + public com.google.protobuf.Parser<UpdatePermissionTemplateResponse> getParserForType() { + return PARSER; + } + + private int bitField0_; + public static final int PERMISSIONTEMPLATE_FIELD_NUMBER = 1; + private org.sonarqube.ws.Permissions.PermissionTemplate permissionTemplate_; + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public boolean hasPermissionTemplate() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public org.sonarqube.ws.Permissions.PermissionTemplate getPermissionTemplate() { + return permissionTemplate_; + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder getPermissionTemplateOrBuilder() { + return permissionTemplate_; + } + + private void initFields() { + permissionTemplate_ = org.sonarqube.ws.Permissions.PermissionTemplate.getDefaultInstance(); + } + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + getSerializedSize(); + if (((bitField0_ & 0x00000001) == 0x00000001)) { + output.writeMessage(1, permissionTemplate_); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) return size; + + size = 0; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + size += com.google.protobuf.CodedOutputStream + .computeMessageSize(1, permissionTemplate_); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + @java.lang.Override + protected java.lang.Object writeReplace() + throws java.io.ObjectStreamException { + return super.writeReplace(); + } + + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom(java.io.InputStream input) + throws java.io.IOException { + return PARSER.parseFrom(input); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return PARSER.parseDelimitedFrom(input); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseDelimitedFrom(input, extensionRegistry); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return PARSER.parseFrom(input); + } + public static org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + + public static Builder newBuilder() { return Builder.create(); } + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder(org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse prototype) { + return newBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { return newBuilder(this); } + + @java.lang.Override + protected Builder newBuilderForType( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code sonarqube.ws.permissions.UpdatePermissionTemplateResponse} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder<Builder> implements + // @@protoc_insertion_point(builder_implements:sonarqube.ws.permissions.UpdatePermissionTemplateResponse) + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponseOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.sonarqube.ws.Permissions.internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.sonarqube.ws.Permissions.internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.class, org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.Builder.class); + } + + // Construct using org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + com.google.protobuf.GeneratedMessage.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + getPermissionTemplateFieldBuilder(); + } + } + private static Builder create() { + return new Builder(); + } + + public Builder clear() { + super.clear(); + if (permissionTemplateBuilder_ == null) { + permissionTemplate_ = org.sonarqube.ws.Permissions.PermissionTemplate.getDefaultInstance(); + } else { + permissionTemplateBuilder_.clear(); + } + bitField0_ = (bitField0_ & ~0x00000001); + return this; + } + + public Builder clone() { + return create().mergeFrom(buildPartial()); + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return org.sonarqube.ws.Permissions.internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor; + } + + public org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse getDefaultInstanceForType() { + return org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.getDefaultInstance(); + } + + public org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse build() { + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse buildPartial() { + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse result = new org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((from_bitField0_ & 0x00000001) == 0x00000001)) { + to_bitField0_ |= 0x00000001; + } + if (permissionTemplateBuilder_ == null) { + result.permissionTemplate_ = permissionTemplate_; + } else { + result.permissionTemplate_ = permissionTemplateBuilder_.build(); + } + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse) { + return mergeFrom((org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse other) { + if (other == org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse.getDefaultInstance()) return this; + if (other.hasPermissionTemplate()) { + mergePermissionTemplate(other.getPermissionTemplate()); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse) e.getUnfinishedMessage(); + throw e; + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + private int bitField0_; + + private org.sonarqube.ws.Permissions.PermissionTemplate permissionTemplate_ = org.sonarqube.ws.Permissions.PermissionTemplate.getDefaultInstance(); + private com.google.protobuf.SingleFieldBuilder< + org.sonarqube.ws.Permissions.PermissionTemplate, org.sonarqube.ws.Permissions.PermissionTemplate.Builder, org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder> permissionTemplateBuilder_; + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public boolean hasPermissionTemplate() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public org.sonarqube.ws.Permissions.PermissionTemplate getPermissionTemplate() { + if (permissionTemplateBuilder_ == null) { + return permissionTemplate_; + } else { + return permissionTemplateBuilder_.getMessage(); + } + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public Builder setPermissionTemplate(org.sonarqube.ws.Permissions.PermissionTemplate value) { + if (permissionTemplateBuilder_ == null) { + if (value == null) { + throw new NullPointerException(); + } + permissionTemplate_ = value; + onChanged(); + } else { + permissionTemplateBuilder_.setMessage(value); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public Builder setPermissionTemplate( + org.sonarqube.ws.Permissions.PermissionTemplate.Builder builderForValue) { + if (permissionTemplateBuilder_ == null) { + permissionTemplate_ = builderForValue.build(); + onChanged(); + } else { + permissionTemplateBuilder_.setMessage(builderForValue.build()); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public Builder mergePermissionTemplate(org.sonarqube.ws.Permissions.PermissionTemplate value) { + if (permissionTemplateBuilder_ == null) { + if (((bitField0_ & 0x00000001) == 0x00000001) && + permissionTemplate_ != org.sonarqube.ws.Permissions.PermissionTemplate.getDefaultInstance()) { + permissionTemplate_ = + org.sonarqube.ws.Permissions.PermissionTemplate.newBuilder(permissionTemplate_).mergeFrom(value).buildPartial(); + } else { + permissionTemplate_ = value; + } + onChanged(); + } else { + permissionTemplateBuilder_.mergeFrom(value); + } + bitField0_ |= 0x00000001; + return this; + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public Builder clearPermissionTemplate() { + if (permissionTemplateBuilder_ == null) { + permissionTemplate_ = org.sonarqube.ws.Permissions.PermissionTemplate.getDefaultInstance(); + onChanged(); + } else { + permissionTemplateBuilder_.clear(); + } + bitField0_ = (bitField0_ & ~0x00000001); + return this; + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public org.sonarqube.ws.Permissions.PermissionTemplate.Builder getPermissionTemplateBuilder() { + bitField0_ |= 0x00000001; + onChanged(); + return getPermissionTemplateFieldBuilder().getBuilder(); + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + public org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder getPermissionTemplateOrBuilder() { + if (permissionTemplateBuilder_ != null) { + return permissionTemplateBuilder_.getMessageOrBuilder(); + } else { + return permissionTemplate_; + } + } + /** + * <code>optional .sonarqube.ws.permissions.PermissionTemplate permissionTemplate = 1;</code> + */ + private com.google.protobuf.SingleFieldBuilder< + org.sonarqube.ws.Permissions.PermissionTemplate, org.sonarqube.ws.Permissions.PermissionTemplate.Builder, org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder> + getPermissionTemplateFieldBuilder() { + if (permissionTemplateBuilder_ == null) { + permissionTemplateBuilder_ = new com.google.protobuf.SingleFieldBuilder< + org.sonarqube.ws.Permissions.PermissionTemplate, org.sonarqube.ws.Permissions.PermissionTemplate.Builder, org.sonarqube.ws.Permissions.PermissionTemplateOrBuilder>( + getPermissionTemplate(), + getParentForChildren(), + isClean()); + permissionTemplate_ = null; + } + return permissionTemplateBuilder_; + } + + // @@protoc_insertion_point(builder_scope:sonarqube.ws.permissions.UpdatePermissionTemplateResponse) + } + + static { + defaultInstance = new UpdatePermissionTemplateResponse(true); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:sonarqube.ws.permissions.UpdatePermissionTemplateResponse) + } + private static final com.google.protobuf.Descriptors.Descriptor internal_static_sonarqube_ws_permissions_UsersResponse_descriptor; private static @@ -9472,6 +9980,11 @@ public final class Permissions { private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_sonarqube_ws_permissions_CreatePermissionTemplateResponse_fieldAccessorTable; + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { @@ -9512,8 +10025,11 @@ public final class Permissions { "reatedAt\030\005 \001(\t\022\021\n\tupdatedAt\030\006 \001(\t\"l\n Cre" + "atePermissionTemplateResponse\022H\n\022permiss", "ionTemplate\030\001 \001(\0132,.sonarqube.ws.permiss" + - "ions.PermissionTemplateB!\n\020org.sonarqube" + - ".wsB\013PermissionsH\001" + "ions.PermissionTemplate\"l\n UpdatePermiss" + + "ionTemplateResponse\022H\n\022permissionTemplat" + + "e\030\001 \001(\0132,.sonarqube.ws.permissions.Permi" + + "ssionTemplateB!\n\020org.sonarqube.wsB\013Permi" + + "ssionsH\001" }; com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { @@ -9588,6 +10104,12 @@ public final class Permissions { com.google.protobuf.GeneratedMessage.FieldAccessorTable( internal_static_sonarqube_ws_permissions_CreatePermissionTemplateResponse_descriptor, new java.lang.String[] { "PermissionTemplate", }); + internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor = + getDescriptor().getMessageTypes().get(7); + internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_sonarqube_ws_permissions_UpdatePermissionTemplateResponse_descriptor, + new java.lang.String[] { "PermissionTemplate", }); org.sonarqube.ws.Common.getDescriptor(); } diff --git a/sonar-ws/src/main/protobuf/ws-permissions.proto b/sonar-ws/src/main/protobuf/ws-permissions.proto index 411c8473ad9..1384be88aef 100644 --- a/sonar-ws/src/main/protobuf/ws-permissions.proto +++ b/sonar-ws/src/main/protobuf/ws-permissions.proto @@ -96,3 +96,7 @@ message PermissionTemplate { message CreatePermissionTemplateResponse { optional PermissionTemplate permissionTemplate = 1; } + +message UpdatePermissionTemplateResponse { + optional PermissionTemplate permissionTemplate = 1; +} |