aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2017-08-29 22:45:18 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-09-13 15:50:49 +0200
commitb812e849cde8fec183a7459ef92c9875f1e5e5c8 (patch)
tree7dfaa2abf54bc2fa3c2f30a4deda3ad016fe2d47
parent564c915cec585008f1f16951e42a20a30c72c929 (diff)
downloadsonarqube-b812e849cde8fec183a7459ef92c9875f1e5e5c8.tar.gz
sonarqube-b812e849cde8fec183a7459ef92c9875f1e5e5c8.zip
SONAR-9740 ability to use UserSession in safe mode
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/authentication/SafeModeUserSession.java86
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/authentication/SafeModeUserSessionTest.java48
3 files changed, 136 insertions, 0 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/authentication/SafeModeUserSession.java b/server/sonar-server/src/main/java/org/sonar/server/authentication/SafeModeUserSession.java
new file mode 100644
index 00000000000..ad49716928f
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/authentication/SafeModeUserSession.java
@@ -0,0 +1,86 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.authentication;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+import javax.annotation.CheckForNull;
+import javax.annotation.concurrent.Immutable;
+import org.sonar.db.permission.OrganizationPermission;
+import org.sonar.db.user.GroupDto;
+import org.sonar.server.user.AbstractUserSession;
+
+@Immutable
+public class SafeModeUserSession extends AbstractUserSession {
+
+ @Override
+ protected boolean hasPermissionImpl(OrganizationPermission permission, String organizationUuid) {
+ return false;
+ }
+
+ @Override
+ protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
+ return Optional.empty();
+ }
+
+ @Override
+ protected boolean hasProjectUuidPermission(String permission, String projectUuid) {
+ return false;
+ }
+
+ @CheckForNull
+ @Override
+ public String getLogin() {
+ return null;
+ }
+
+ @CheckForNull
+ @Override
+ public String getName() {
+ return null;
+ }
+
+ @CheckForNull
+ @Override
+ public Integer getUserId() {
+ return null;
+ }
+
+ @Override
+ public Collection<GroupDto> getGroups() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public boolean isLoggedIn() {
+ return false;
+ }
+
+ @Override
+ public boolean isRoot() {
+ return false;
+ }
+
+ @Override
+ public boolean isSystemAdministrator() {
+ return false;
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java
index 95ba0f135e2..40e84881ca9 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelSafeMode.java
@@ -20,6 +20,7 @@
package org.sonar.server.platform.platformlevel;
import org.sonar.server.platform.ws.SafeModeHealthActionModule;
+import org.sonar.server.authentication.SafeModeUserSession;
import org.sonar.server.organization.NoopDefaultOrganizationCache;
import org.sonar.server.platform.ServerImpl;
import org.sonar.server.platform.db.migration.AutoDbMigration;
@@ -63,6 +64,7 @@ public class PlatformLevelSafeMode extends PlatformLevel {
WebServicesWsModule.class,
// WS engine
+ SafeModeUserSession.class,
WebServiceEngine.class,
WebServiceFilter.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/SafeModeUserSessionTest.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/SafeModeUserSessionTest.java
new file mode 100644
index 00000000000..6641f1841fe
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/SafeModeUserSessionTest.java
@@ -0,0 +1,48 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.authentication;
+
+import org.junit.Test;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.permission.OrganizationPermission;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SafeModeUserSessionTest {
+
+ private SafeModeUserSession underTest = new SafeModeUserSession();
+
+ @Test
+ public void session_is_anonymous() {
+ assertThat(underTest.getLogin()).isNull();
+ assertThat(underTest.isLoggedIn()).isFalse();
+ assertThat(underTest.getName()).isNull();
+ assertThat(underTest.getUserId()).isNull();
+ assertThat(underTest.getGroups()).isEmpty();
+ }
+
+ @Test
+ public void session_has_no_permissions() {
+ assertThat(underTest.isRoot()).isFalse();
+ assertThat(underTest.isSystemAdministrator()).isFalse();
+ assertThat(underTest.hasPermissionImpl(OrganizationPermission.ADMINISTER, "foo")).isFalse();
+ assertThat(underTest.hasProjectUuidPermission(UserRole.USER, "foo")).isFalse();
+ }
+}