--- /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 org.sonar.server.organization.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.property.InternalProperties;
+import org.sonar.server.user.UserSession;
+
+import static java.lang.String.valueOf;
+import static java.util.Objects.requireNonNull;
+import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+
+public class EnableSupportAction implements OrganizationsAction {
+ private static final String ACTION = "enable_support";
+
+ private final UserSession userSession;
+ private final DbClient dbClient;
+ private final DefaultOrganizationProvider defaultOrganizationProvider;
+ private final OrganizationsWsSupport support;
+
+ public EnableSupportAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, OrganizationsWsSupport support) {
+ this.userSession = userSession;
+ this.dbClient = dbClient;
+ this.defaultOrganizationProvider = defaultOrganizationProvider;
+ this.support = support;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction(ACTION)
+ .setPost(true)
+ .setDescription("Enable support of organizations.<br />" +
+ "'Administer System' permission is required. The logged-in user will be flagged as root and will be able to manage organizations and other root users.")
+ .setInternal(true)
+ .setPost(true)
+ .setSince("6.3")
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ verifySystemAdministrator();
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ verifyFeatureIsDisabled(dbSession);
+ flagCurrentUserAsRoot(dbSession);
+ enableFeature(dbSession);
+ dbSession.commit();
+ }
+ response.noContent();
+ }
+
+ private void verifySystemAdministrator() {
+ userSession.checkLoggedIn().checkOrganizationPermission(defaultOrganizationProvider.get().getUuid(), SYSTEM_ADMIN);
+ }
+
+ private void verifyFeatureIsDisabled(DbSession dbSession) {
+ if (support.isFeatureEnabled(dbSession)) {
+ throw new BadRequestException("Organizations are already enabled");
+ }
+ }
+
+ private void flagCurrentUserAsRoot(DbSession dbSession) {
+ dbClient.userDao().setRoot(dbSession, requireNonNull(userSession.getLogin()), true);
+ }
+
+ private void enableFeature(DbSession dbSession) {
+ dbClient.internalPropertiesDao().save(dbSession, InternalProperties.ORGANIZATION_ENABLED, valueOf(true));
+ }
+
+}
OrganizationsWsSupport.class,
// actions
CreateAction.class,
+ EnableSupportAction.class,
SearchAction.class,
UpdateAction.class,
DeleteAction.class,
*/
package org.sonar.server.organization.ws;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.organization.OrganizationValidation;
+import org.sonar.server.property.InternalProperties;
import org.sonarqube.ws.Organizations;
import static org.sonar.core.util.Protobuf.setNullable;
static final String PARAM_AVATAR_URL = "avatar";
private final OrganizationValidation organizationValidation;
+ private final DbClient dbClient;
- public OrganizationsWsSupport(OrganizationValidation organizationValidation) {
+ public OrganizationsWsSupport(OrganizationValidation organizationValidation, DbClient dbClient) {
this.organizationValidation = organizationValidation;
+ this.dbClient = dbClient;
}
String getAndCheckMandatoryName(Request request) {
setNullable(dto.getAvatarUrl(), builder::setAvatar);
return builder.build();
}
+
+ boolean isFeatureEnabled(DbSession dbSession) {
+ Optional<String> value = dbClient.internalPropertiesDao().selectByKey(dbSession, InternalProperties.ORGANIZATION_ENABLED);
+ return value.isPresent() && Boolean.parseBoolean(value.get());
+ }
}
*/
String DEFAULT_ORGANIZATION = "organization.default";
+ String ORGANIZATION_ENABLED = "organization.enabled";
+
/**
* Read the value of the specified property.
*
private UuidFactory uuidFactory = mock(UuidFactory.class);
private OrganizationValidation organizationValidation = new OrganizationValidationImpl();
private OrganizationCreation organizationCreation = new OrganizationCreationImpl(dbClient, system2, uuidFactory, organizationValidation, settings);
- private CreateAction underTest = new CreateAction(settings, userSession, dbClient, new OrganizationsWsSupport(organizationValidation), organizationValidation, organizationCreation);
+ private CreateAction underTest = new CreateAction(settings, userSession, dbClient, new OrganizationsWsSupport(organizationValidation, dbClient), organizationValidation, organizationCreation);
private WsActionTester wsTester = new WsActionTester(underTest);
@Test
--- /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 org.sonar.server.organization.ws;
+
+import java.net.HttpURLConnection;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.OrganizationValidationImpl;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.property.InternalProperties;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+
+public class EnableSupportActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+ private OrganizationsWsSupport support = new OrganizationsWsSupport(new OrganizationValidationImpl(), db.getDbClient());
+ private EnableSupportAction underTest = new EnableSupportAction(userSession, db.getDbClient(), defaultOrganizationProvider, support);
+ private WsActionTester tester = new WsActionTester(underTest);
+
+ @Test
+ public void enabling_support_saves_internal_property_and_flags_caller_as_root() {
+ UserDto user = db.users().insertUser();
+ db.rootFlag().verify(user.getLogin(), false);
+ logInAsSystemAdministrator(user.getLogin());
+
+ call();
+
+ assertThat(db.getDbClient().internalPropertiesDao().selectByKey(db.getSession(), InternalProperties.ORGANIZATION_ENABLED)).hasValue("true");
+ db.rootFlag().verify(user.getLogin(), true);
+ }
+
+ @Test
+ public void throw_UnauthorizedException_if_not_logged_in() {
+ userSession.anonymous();
+
+ expectedException.expect(UnauthorizedException.class);
+ expectedException.expectMessage("Authentication is required");
+
+ call();
+ }
+
+ @Test
+ public void throw_ForbiddenException_if_not_system_administrator() {
+ userSession.logIn();
+
+ expectedException.expect(ForbiddenException.class);
+ expectedException.expectMessage("Insufficient privileges");
+
+ call();
+ }
+
+ @Test
+ public void throw_BadRequestException_if_support_is_already_enabled() {
+ logInAsSystemAdministrator("foo");
+
+ call();
+
+ expectedException.expect(BadRequestException.class);
+ expectedException.expectMessage("Organizations are already enabled");
+
+ call();
+ }
+
+ @Test
+ public void test_definition() {
+ WebService.Action def = tester.getDef();
+ assertThat(def.key()).isEqualTo("enable_support");
+ assertThat(def.isPost()).isTrue();
+ assertThat(def.isInternal()).isTrue();
+ assertThat(def.params()).isEmpty();
+ }
+
+ private void logInAsSystemAdministrator(String login) {
+ userSession.logIn(login).addOrganizationPermission(db.getDefaultOrganization().getUuid(), SYSTEM_ADMIN);
+ }
+
+ private void call() {
+ TestResponse response = tester.newRequest().setMethod("POST").execute();
+ assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
+ }
+}
ComponentContainer container = new ComponentContainer();
underTest.configure(container);
assertThat(container.getPicoContainer().getComponentAdapters())
- .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7);
+ .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 8);
}
}
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private SearchAction underTest = new SearchAction(dbTester.getDbClient(), new OrganizationsWsSupport(new OrganizationValidationImpl()));
+ private SearchAction underTest = new SearchAction(dbTester.getDbClient(), new OrganizationsWsSupport(new OrganizationValidationImpl(), dbTester.getDbClient()));
private WsActionTester wsTester = new WsActionTester(underTest);
@Test
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private UpdateAction underTest = new UpdateAction(userSession, new OrganizationsWsSupport(new OrganizationValidationImpl()), dbTester.getDbClient());
+ private UpdateAction underTest = new UpdateAction(userSession, new OrganizationsWsSupport(new OrganizationValidationImpl(), dbTester.getDbClient()), dbTester.getDbClient());
private WsActionTester wsTester = new WsActionTester(underTest);
@Test