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;
.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");
}
// validations
UserGroupValidation.validateGroupName(group.getName());
- support.validateDescription(group.getDescription());
support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName());
dbClient.groupDao().insert(dbSession, group);
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;
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
.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");
String description = request.param(PARAM_GROUP_DESCRIPTION);
if (description != null) {
changed = true;
- group.setDescription(support.validateDescription(description));
+ group.setDescription(description);
}
if (changed) {
*/
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;
.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();
.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();