import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonarqube.ws.Organizations;
+import org.sonarqube.ws.WsComponents;
import org.sonarqube.ws.client.HttpException;
+import org.sonarqube.ws.client.component.ComponentsService;
import org.sonarqube.ws.client.organization.CreateWsRequest;
import org.sonarqube.ws.client.organization.OrganizationService;
import org.sonarqube.ws.client.organization.SearchWsRequest;
import org.sonarqube.ws.client.organization.UpdateWsRequest;
import util.ItUtils;
+import util.user.GroupManagement;
+import util.user.Groups;
import util.user.UserRule;
+import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
expect403HttpError(() -> fooUserOrganizationService.create(createWsRequest));
}
+ @Test
+ public void deleting_an_organization_also_deletes_group_permissions_and_projects_and_check_security() {
+ verifyNoExtraOrganization();
+
+ String orgKeyAndName = "org-key";
+ Organizations.Organization createdOrganization = adminOrganizationService.create(new CreateWsRequest.Builder()
+ .setName(orgKeyAndName)
+ .setKey(orgKeyAndName)
+ .build())
+ .getOrganization();
+ verifySingleSearchResult(createdOrganization, orgKeyAndName, null, null, null);
+
+ GroupManagement groupManagement = userRule.forOrganization(orgKeyAndName);
+
+ userRule.createUser("bob", "bob");
+ groupManagement.createGroup("grp1");
+ groupManagement.createGroup("grp2");
+ groupManagement.associateGroupsToUser("bob", "grp1", "grp2");
+ assertThat(groupManagement.getUserGroups("bob").getGroups())
+ .extracting(Groups.Group::getName)
+ .contains("grp1", "grp2");
+
+ ItUtils.runProjectAnalysis(orchestrator, "shared/xoo-sample",
+ "sonar.organization", orgKeyAndName);
+ ComponentsService componentsService = ItUtils.newAdminWsClient(orchestrator).components();
+ assertThat(searchSampleProject(componentsService).getComponentsList()).hasSize(1);
+
+ adminOrganizationService.delete(orgKeyAndName);
+
+ assertThat(searchSampleProject(componentsService).getComponentsList()).hasSize(0);
+ assertThat(groupManagement.getUserGroups("bob").getGroups())
+ .extracting(Groups.Group::getName)
+ .doesNotContain("grp1", "grp2");
+
+ verifyNoExtraOrganization();
+ }
+
+ private WsComponents.SearchWsResponse searchSampleProject(ComponentsService componentsService) {
+ return componentsService
+ .search(new org.sonarqube.ws.client.component.SearchWsRequest().setQualifiers(singletonList("TRK")).setQuery("sample"));
+ }
+
private void expect403HttpError(Runnable runnable) {
try {
runnable.run();
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 util.user;
+
+import java.util.List;
+import java.util.Optional;
+import javax.annotation.Nullable;
+
+public interface GroupManagement {
+ void createGroup(String name);
+
+ void createGroup(String name, @Nullable String description);
+
+ void removeGroups(List<String> groupNames);
+
+ void removeGroups(String... groupNames);
+
+ Optional<Groups.Group> getGroupByName(String name);
+
+ Groups getGroups();
+
+ void verifyUserGroupMembership(String userLogin, String... groups);
+
+ Groups getUserGroups(String userLogin);
+
+ void associateGroupsToUser(String userLogin, String... groups);
+}
import com.google.common.collect.FluentIterable;
import com.sonar.orchestrator.Orchestrator;
import java.util.List;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
import static org.assertj.guava.api.Assertions.assertThat;
import static util.ItUtils.newAdminWsClient;
-public class UserRule extends ExternalResource {
+public class UserRule extends ExternalResource implements GroupManagement {
public static final String ADMIN_LOGIN = "admin";
private final Orchestrator orchestrator;
private WsClient adminWsClient;
+ private final GroupManagement defaultOrganizationGroupManagement;
private UserRule(Orchestrator orchestrator) {
this.orchestrator = orchestrator;
+ this.defaultOrganizationGroupManagement = new GroupManagementImpl(null);
}
public static UserRule from(Orchestrator orchestrator) {
// User groups
// *****************
+ public GroupManagement forOrganization(String organizationKey) {
+ return new GroupManagementImpl(organizationKey);
+ }
+
+ private final class GroupManagementImpl implements GroupManagement {
+ @CheckForNull
+ private final String organizationKey;
+
+ private GroupManagementImpl(@Nullable String organizationKey) {
+ this.organizationKey = organizationKey;
+ }
+
+ @Override
+ public void createGroup(String name) {
+ createGroup(name, null);
+ }
+
+ @Override
+ public void createGroup(String name, @Nullable String description) {
+ PostRequest request = new PostRequest("api/user_groups/create")
+ .setParam("name", name)
+ .setParam("description", description);
+ addOrganizationParam(request);
+ adminWsClient().wsConnector().call(request);
+ }
+
+ private void addOrganizationParam(PostRequest request) {
+ if (organizationKey != null) {
+ request.setParam("organization", organizationKey);
+ }
+ }
+
+ private void addOrganizationParam(GetRequest request) {
+ if (organizationKey != null) {
+ request.setParam("organization", organizationKey);
+ }
+ }
+
+ @Override
+ public void removeGroups(List<String> groupNames) {
+ for (String groupName : groupNames) {
+ if (getGroupByName(groupName).isPresent()) {
+ PostRequest request = new PostRequest("api/user_groups/delete")
+ .setParam("name", groupName);
+ addOrganizationParam(request);
+ adminWsClient().wsConnector().call(request);
+ }
+ }
+ }
+
+ @Override
+ public void removeGroups(String... groupNames) {
+ removeGroups(asList(groupNames));
+ }
+
+ @Override
+ public java.util.Optional<Groups.Group> getGroupByName(String name) {
+ return getGroups().getGroups().stream().filter(new MatchGroupName(name)::apply).findFirst();
+ }
+
+ @Override
+ public Groups getGroups() {
+ GetRequest request = new GetRequest("api/user_groups/search");
+ addOrganizationParam(request);
+ WsResponse response = adminWsClient().wsConnector().call(request);
+ assertThat(response.code()).isEqualTo(200);
+ return Groups.parse(response.content());
+ }
+
+ @Override
+ public void verifyUserGroupMembership(String userLogin, String... groups) {
+ Groups userGroup = getUserGroups(userLogin);
+ List<String> userGroupName = FluentIterable.from(userGroup.getGroups()).transform(ToGroupName.INSTANCE).toList();
+ assertThat(userGroupName).containsOnly(groups);
+ }
+
+ @Override
+ public Groups getUserGroups(String userLogin) {
+ GetRequest request = new GetRequest("api/users/groups")
+ .setParam("login", userLogin)
+ .setParam("selected", "selected");
+ addOrganizationParam(request);
+ WsResponse response = adminWsClient().wsConnector().call(request);
+ assertThat(response.code()).isEqualTo(200);
+ return Groups.parse(response.content());
+ }
+
+ @Override
+ public void associateGroupsToUser(String userLogin, String... groups) {
+ for (String group : groups) {
+ PostRequest request = new PostRequest("api/user_groups/add_user")
+ .setParam("login", userLogin)
+ .setParam("name", group);
+ addOrganizationParam(request);
+ WsResponse response = adminWsClient().wsConnector().call(request);
+ assertThat(response.code()).isEqualTo(204);
+ }
+ }
+ }
+
+ @Override
public void createGroup(String name) {
- createGroup(name, null);
+ defaultOrganizationGroupManagement.createGroup(name);
}
+ @Override
public void createGroup(String name, @Nullable String description) {
- adminWsClient().wsConnector().call(
- new PostRequest("api/user_groups/create")
- .setParam("name", name)
- .setParam("description", description));
+ defaultOrganizationGroupManagement.createGroup(name, description);
}
+ @Override
public void removeGroups(List<String> groupNames) {
- for (String groupName : groupNames) {
- if (getGroupByName(groupName).isPresent()) {
- adminWsClient().wsConnector().call(
- new PostRequest("api/user_groups/delete")
- .setParam("name", groupName));
- }
- }
+ defaultOrganizationGroupManagement.removeGroups(groupNames);
}
+ @Override
public void removeGroups(String... groupNames) {
- removeGroups(asList(groupNames));
+ defaultOrganizationGroupManagement.removeGroups(groupNames);
}
- public Optional<Groups.Group> getGroupByName(String name) {
- return FluentIterable.from(getGroups().getGroups()).firstMatch(new MatchGroupName(name));
+ @Override
+ public java.util.Optional<Groups.Group> getGroupByName(String name) {
+ return defaultOrganizationGroupManagement.getGroupByName(name);
}
+ @Override
public Groups getGroups() {
- WsResponse response = adminWsClient().wsConnector().call(
- new GetRequest("api/user_groups/search"));
- assertThat(response.code()).isEqualTo(200);
- return Groups.parse(response.content());
+ return defaultOrganizationGroupManagement.getGroups();
}
+ @Override
public void verifyUserGroupMembership(String userLogin, String... groups) {
- Groups userGroup = getUserGroups(userLogin);
- List<String> userGroupName = FluentIterable.from(userGroup.getGroups()).transform(ToGroupName.INSTANCE).toList();
- assertThat(userGroupName).containsOnly(groups);
+ defaultOrganizationGroupManagement.verifyUserGroupMembership(userLogin, groups);
}
+ @Override
public Groups getUserGroups(String userLogin) {
- WsResponse response = adminWsClient().wsConnector().call(
- new GetRequest("api/users/groups")
- .setParam("login", userLogin)
- .setParam("selected", "selected"));
- assertThat(response.code()).isEqualTo(200);
- return Groups.parse(response.content());
+ return defaultOrganizationGroupManagement.getUserGroups(userLogin);
}
+ @Override
public void associateGroupsToUser(String userLogin, String... groups) {
- for (String group : groups) {
- WsResponse response = adminWsClient().wsConnector().call(
- new PostRequest("api/user_groups/add_user")
- .setParam("login", userLogin)
- .setParam("name", group));
- assertThat(response.code()).isEqualTo(204);
- }
+ defaultOrganizationGroupManagement.associateGroupsToUser(userLogin, groups);
}
private WsClient adminWsClient() {