import org.sonarqube.ws.WsUserGroups;
import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.server.user.UserUpdater.SONAR_USERS_GROUP_NAME;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonar.server.ws.WsUtils.checkRequest;
checkRequest(!dbClient.groupDao().selectByName(dbSession, organizationUuid, name).isPresent(), "Group '%s' already exists", name);
}
+ void checkGroupIsNotDefault(GroupDto groupDto) {
+ checkArgument(!SONAR_USERS_GROUP_NAME.equals(groupDto.getName()), "Default group '%s' cannot be used to perform this action", SONAR_USERS_GROUP_NAME);
+ }
+
static WsUserGroups.Group.Builder toProtobuf(OrganizationDto organization, GroupDto group, int membersCount) {
WsUserGroups.Group.Builder wsGroup = WsUserGroups.Group.newBuilder()
.setId(group.getId())
package org.sonar.server.usergroups.ws;
import java.util.Optional;
+import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService.NewAction;
import static org.sonar.api.user.UserGroupValidation.GROUP_NAME_MAX_LENGTH;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
+import static org.sonar.server.user.UserUpdater.SONAR_USERS_GROUP_NAME;
import static org.sonar.server.usergroups.ws.GroupWsSupport.DESCRIPTION_MAX_LENGTH;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_DESCRIPTION;
import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID;
.setHandler(this)
.setPost(true)
.setResponseExample(getClass().getResource("example-update.json"))
- .setSince("5.2");
+ .setSince("5.2")
+ .setChangelog(new Change("6.4", "The default group is no longer editable"));
action.createParam(PARAM_GROUP_ID)
.setDescription("Identifier of the group.")
action.createParam(PARAM_GROUP_NAME)
.setDescription(String.format("New optional name for the 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. If value is empty or not defined, then name is not changed.", GROUP_NAME_MAX_LENGTH))
- .setExampleValue("sonar-users");
+ "Value 'anyone' and '%s' (whatever the case) are reserved and cannot be used. If value is empty or not defined, then name is not changed.", GROUP_NAME_MAX_LENGTH,
+ SONAR_USERS_GROUP_NAME))
+ .setExampleValue("my-group");
action.createParam(PARAM_GROUP_DESCRIPTION)
.setDescription(String.format("New optional description for the group. A group description cannot be larger than %d characters. " +
Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid());
checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid());
userSession.checkPermission(ADMINISTER, org.get());
+ support.checkGroupIsNotDefault(group);
boolean changed = false;
String newName = request.param(PARAM_GROUP_NAME);
respBuilder.setGroup(toProtobuf(organization, group, membersCount));
writeProtobuf(respBuilder.build(), request, response);
}
+
}
"}");
}
- @Test
- public void update_default_group_name_also_update_default_group_property() throws Exception {
- GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), DEFAULT_GROUP_NAME_VALUE);
- loginAsAdminOnDefaultOrganization();
-
- newRequest()
- .setParam("id", group.getId().toString())
- .setParam("name", "new-name")
- .execute();
- }
-
@Test
public void require_admin_permission_on_organization() throws Exception {
GroupDto group = db.users().insertGroup();
.execute();
}
+ @Test
+ public void fail_to_update_default_group_name() throws Exception {
+ GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), DEFAULT_GROUP_NAME_VALUE);
+ loginAsAdminOnDefaultOrganization();
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Default group 'sonar-users' cannot be used to perform this action");
+
+ newRequest()
+ .setParam("id", group.getId().toString())
+ .setParam("name", "new name")
+ .execute();
+ }
+
+ @Test
+ public void fail_to_update_default_group_description() throws Exception {
+ GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), DEFAULT_GROUP_NAME_VALUE);
+ loginAsAdminOnDefaultOrganization();
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Default group 'sonar-users' cannot be used to perform this action");
+
+ newRequest()
+ .setParam("id", group.getId().toString())
+ .setParam("description", "new description")
+ .execute();
+ }
+
private TestRequest newRequest() {
return ws.newRequest();
}