Browse Source

SONAR-10040 add length validation to Groups ws

tags/7.0-RC1
Guillaume Jambet 6 years ago
parent
commit
01c6a75c83

+ 7
- 5
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/CreateAction.java View File

@@ -31,6 +31,7 @@ import org.sonar.db.user.GroupDto;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsUserGroups;

import static java.lang.String.format;
import static org.sonar.api.user.UserGroupValidation.GROUP_NAME_MAX_LENGTH;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
import static org.sonar.server.usergroups.ws.GroupWsSupport.DESCRIPTION_MAX_LENGTH;
@@ -69,13 +70,15 @@ public class CreateAction implements UserGroupsWsAction {
.setInternal(true);

action.createParam(PARAM_GROUP_NAME)
.setDescription(String.format("Name for the new group. A group name cannot be larger than %d characters and must be unique. " +
.setRequired(true)
.setMaximumLength(GROUP_NAME_MAX_LENGTH)
.setDescription(format("Name for the new group. A group name cannot be larger than %d characters and must be unique. " +
"The value 'anyone' (whatever the case) is reserved and cannot be used.", GROUP_NAME_MAX_LENGTH))
.setExampleValue("sonar-users")
.setRequired(true);
.setExampleValue("sonar-users");

action.createParam(PARAM_GROUP_DESCRIPTION)
.setDescription(String.format("Description for the new group. A group description cannot be larger than %d characters.", DESCRIPTION_MAX_LENGTH))
.setMaximumLength(DESCRIPTION_MAX_LENGTH)
.setDescription(format("Description for the new group. A group description cannot be larger than %d characters.", DESCRIPTION_MAX_LENGTH))
.setExampleValue("Default group for new users");
}

@@ -92,7 +95,6 @@ public class CreateAction implements UserGroupsWsAction {

// validations
UserGroupValidation.validateGroupName(group.getName());
support.validateDescription(group.getDescription());
support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName());

dbClient.groupDao().insert(dbSession, group);

+ 0
- 14
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupWsSupport.java View File

@@ -20,11 +20,9 @@
package org.sonar.server.usergroups.ws;

import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.user.UserGroupValidation;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
@@ -132,18 +130,6 @@ public class GroupWsSupport {
return org.get();
}

/**
* Similar to {@link UserGroupValidation#validateGroupName(String)} but kept internal. No need to publish
* this method in public API.
* @return the same description
*/
@CheckForNull
String validateDescription(@Nullable String description) {
checkArgument(description == null || description.length() <= DESCRIPTION_MAX_LENGTH,
"Description cannot be longer than %s characters", DESCRIPTION_MAX_LENGTH);
return description;
}

void checkNameDoesNotExist(DbSession dbSession, String organizationUuid, String name) {
// There is no database constraint on column groups.name
// because MySQL cannot create a unique index

+ 3
- 1
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/UpdateAction.java View File

@@ -75,11 +75,13 @@ public class UpdateAction implements UserGroupsWsAction {
.setRequired(true);

action.createParam(PARAM_GROUP_NAME)
.setMaximumLength(GROUP_NAME_MAX_LENGTH)
.setDescription(format("New optional name for the group. A group name cannot be larger than %d characters and must be unique. " +
"Value 'anyone' (whatever the case) is reserved and cannot be used. If value is empty or not defined, then name is not changed.", GROUP_NAME_MAX_LENGTH))
.setExampleValue("my-group");

action.createParam(PARAM_GROUP_DESCRIPTION)
.setMaximumLength(DESCRIPTION_MAX_LENGTH)
.setDescription(format("New optional description for the group. A group description cannot be larger than %d characters. " +
"If value is not defined, then description is not changed.", DESCRIPTION_MAX_LENGTH))
.setExampleValue("Default group for new users");
@@ -108,7 +110,7 @@ public class UpdateAction implements UserGroupsWsAction {
String description = request.param(PARAM_GROUP_DESCRIPTION);
if (description != null) {
changed = true;
group.setDescription(support.validateDescription(description));
group.setDescription(description);
}

if (changed) {

+ 0
- 32
server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/UpdateActionTest.java View File

@@ -19,7 +19,6 @@
*/
package org.sonar.server.usergroups.ws;

import org.apache.commons.lang.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -186,21 +185,6 @@ public class UpdateActionTest {
.execute();
}

@Test
public void fail_if_name_is_too_long() throws Exception {
insertDefaultGroupOnDefaultOrganization();
GroupDto group = db.users().insertGroup();
loginAsAdminOnDefaultOrganization();

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Group name cannot be longer than 255 characters");

newRequest()
.setParam("id", group.getId().toString())
.setParam("name", StringUtils.repeat("a", 255 + 1))
.execute();
}

@Test
public void fail_if_new_name_is_anyone() throws Exception {
insertDefaultGroupOnDefaultOrganization();
@@ -234,22 +218,6 @@ public class UpdateActionTest {
.execute();
}

@Test
public void fail_if_description_is_too_long() throws Exception {
insertDefaultGroupOnDefaultOrganization();
GroupDto group = db.users().insertGroup();
loginAsAdminOnDefaultOrganization();

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Description cannot be longer than 200 characters");

newRequest()
.setParam("id", group.getId().toString())
.setParam("name", "long-group-description-is-looooooooooooong")
.setParam("description", StringUtils.repeat("a", 201))
.execute();
}

@Test
public void fail_if_unknown_group_id() throws Exception {
loginAsAdminOnDefaultOrganization();

Loading…
Cancel
Save