import static org.sonar.server.permission.ws.PermissionRequestValidator.validateNotAnyoneAndAdminPermission;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
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;
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+ String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
WsGroup group = WsGroup.fromRequest(wsRequest);
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
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.createTemplateKeyParameterToAddRemoveUserOrGroup;
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+ String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
final String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
import org.sonarqube.ws.Permissions.PermissionTemplate;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
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.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateTemplateNameFormat;
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;
}
}
- private void validateTemplateNameForCreation(DbSession dbSession, String templateName) {
- checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+ private void validateTemplateNameForCreation(DbSession dbSession, String name) {
+ validateTemplateNameFormat(name);
- PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName);
+ PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, name);
checkRequest(permissionTemplateWithSameName == null, String.format
- (MSG_TEMPLATE_WITH_SAME_NAME, templateName));
+ (MSG_TEMPLATE_WITH_SAME_NAME, name));
}
private PermissionTemplateDto insertTemplate(DbSession dbSession, String name, String description, String projectPattern) {
import org.sonar.server.user.UserSession;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameter;
public class DeleteTemplateAction implements PermissionsWsAction {
@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY);
+ String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID);
DbSession dbSession = dbClient.openSession(false);
try {
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_LONG_TEMPLATE_KEY = "templateKey";
- static final String PARAM_TEMPLATE_KEY = "key";
+ static final String PARAM_LONG_TEMPLATE_ID = "templateId";
+
+ static final String PARAM_TEMPLATE_ID = "id";
static final String PARAM_TEMPLATE_NAME = "name";
static final String PARAM_TEMPLATE_DESCRIPTION = "description";
- static final String PARAM_TEMPLATE_PATTERN = "projectPattern";
+ static final String PARAM_TEMPLATE_PATTERN = "projectKeyPattern";
private static final String PERMISSION_PARAM_DESCRIPTION = String.format("Permission" +
"<ul>" +
}
static void createTemplateKeyParameterToAddRemoveUserOrGroup(NewAction action) {
- action.createParam(PARAM_LONG_TEMPLATE_KEY)
+ action.createParam(PARAM_LONG_TEMPLATE_ID)
.setRequired(true)
.setDescription("Template key")
.setExampleValue("developer_template_20150820_170218");
}
static void createTemplateKeyParameter(NewAction action) {
- action.createParam(PARAM_TEMPLATE_KEY)
+ action.createParam(PARAM_TEMPLATE_ID)
.setRequired(true)
.setDescription("Key")
.setExampleValue("af8cb8cc-1e78-4c4e-8c00-ee8e814009a5");
package org.sonar.server.permission.ws;
+import com.google.common.base.CharMatcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.annotation.Nullable;
format("It is not possible to add the '%s' permission to the '%s' group.", permission, groupName));
}
+ public static void validateTemplateNameFormat(String name) {
+ String nameWithoutWhitespaces = CharMatcher.WHITESPACE.removeFrom(name);
+ checkRequest(!nameWithoutWhitespaces.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+ }
+
public static void validateProjectPattern(@Nullable String projectPattern) {
if (isNullOrEmpty(projectPattern)) {
return;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
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;
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+ String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
WsGroup group = WsGroup.fromRequest(wsRequest);
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
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.createTemplateKeyParameterToAddRemoveUserOrGroup;
public void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+ String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
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.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
-import static org.sonar.server.permission.ws.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_ID;
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.createTemplateKeyParameter;
import static org.sonar.server.permission.ws.Parameters.createTemplateProjectKeyPatternParameter;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateTemplateNameFormat;
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 void handle(Request wsRequest, Response wsResponse) throws Exception {
checkGlobalAdminUser(userSession);
- String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY);
+ String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID);
String nameParam = wsRequest.param(PARAM_TEMPLATE_NAME);
String descriptionParam = wsRequest.param(PARAM_TEMPLATE_DESCRIPTION);
String projectPatternParam = wsRequest.param(PARAM_TEMPLATE_PATTERN);
return UpdatePermissionTemplateResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build();
}
- private void validateTemplateNameForUpdate(DbSession dbSession, String templateName, long templateId) {
- checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+ private void validateTemplateNameForUpdate(DbSession dbSession, String name, long id) {
+ validateTemplateNameFormat(name);
- PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName);
- checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == templateId,
- format(MSG_TEMPLATE_WITH_SAME_NAME, templateName));
+ PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, name);
+ checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == id,
+ format(MSG_TEMPLATE_WITH_SAME_NAME, name));
}
}
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_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
@Category(DbTests.class)
@Test
public void add_with_group_id() {
ws.newRequest()
- .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee())
+ .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
.setParam(PARAM_PERMISSION, CODEVIEWER)
.setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
.execute();
request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
}
if (templateKey != null) {
- request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey);
+ request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
}
if (templateKey != null) {
- request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey);
+ request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
@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");
+ expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
newRequest("Finance", null, "[azerty");
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
public class DeleteTemplateActionTest {
private TestResponse newRequest(@Nullable String key) {
TestRequest request = ws.newRequest();
if (key != null) {
- request.setParam(PARAM_TEMPLATE_KEY, key);
+ request.setParam(PARAM_TEMPLATE_ID, key);
}
TestResponse result = executeRequest(request);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.permission.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.server.exceptions.BadRequestException;
+
+import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
+
+public class PermissionRequestValidatorTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void validate_template_name() {
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage(MSG_TEMPLATE_NAME_NOT_BLANK);
+
+ PermissionRequestValidator.validateTemplateNameFormat(" \r\n");
+ }
+}
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_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
@Category(DbTests.class)
public class RemoveGroupFromTemplateActionTest {
commit();
ws.newRequest()
- .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee())
+ .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
.setParam(PARAM_PERMISSION, PERMISSION)
.setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
.execute();
request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
}
if (templateKey != null) {
- request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey);
+ request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
}
if (templateKey != null) {
- request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey);
+ request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
}
if (permission != null) {
request.setParam(Parameters.PARAM_PERMISSION, permission);
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_ID;
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;
newRequest(templateDto.getKee(), "", null, null);
}
+ @Test
+ public void fail_if_name_has_just_whitespaces() {
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("The template name must not be blank");
+
+ newRequest(templateDto.getKee(), " \r\n", 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");
+ expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
newRequest(templateDto.getKee(), "Finance", null, "[azerty");
}
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);
+ request.setParam(PARAM_TEMPLATE_ID, key);
}
if (name != null) {
request.setParam(PARAM_TEMPLATE_NAME, name);