You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestUserSessionFactory.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.user;
  21. import java.util.Collection;
  22. import java.util.Optional;
  23. import javax.annotation.Nullable;
  24. import org.sonar.db.permission.OrganizationPermission;
  25. import org.sonar.db.user.GroupDto;
  26. import org.sonar.db.user.UserDto;
  27. import static java.util.Objects.requireNonNull;
  28. /**
  29. * Simple implementation of {@link UserSessionFactory}. It creates
  30. * instances of {@link UserSession} that don't manage groups nor
  31. * permissions. Only basic user information like {@link UserSession#isLoggedIn()}
  32. * and {@link UserSession#getLogin()} are available. The methods
  33. * relying on groups or permissions throw {@link UnsupportedOperationException}.
  34. */
  35. public class TestUserSessionFactory implements UserSessionFactory {
  36. private TestUserSessionFactory() {
  37. }
  38. @Override
  39. public UserSession create(UserDto user) {
  40. return new TestUserSession(requireNonNull(user));
  41. }
  42. @Override
  43. public UserSession createAnonymous() {
  44. return new TestUserSession(null);
  45. }
  46. public static TestUserSessionFactory standalone() {
  47. return new TestUserSessionFactory();
  48. }
  49. private static class TestUserSession extends AbstractUserSession {
  50. private final UserDto user;
  51. public TestUserSession(@Nullable UserDto user) {
  52. this.user = user;
  53. }
  54. @Override
  55. public String getLogin() {
  56. return user != null ? user.getLogin() : null;
  57. }
  58. @Override
  59. public String getUuid() {
  60. return user != null ? user.getUuid() : null;
  61. }
  62. @Override
  63. public String getName() {
  64. return user != null ? user.getName() : null;
  65. }
  66. @Override
  67. public Collection<GroupDto> getGroups() {
  68. throw notImplemented();
  69. }
  70. @Override
  71. public Optional<IdentityProvider> getIdentityProvider() {
  72. throw notImplemented();
  73. }
  74. @Override
  75. public Optional<ExternalIdentity> getExternalIdentity() {
  76. throw notImplemented();
  77. }
  78. @Override
  79. public boolean isLoggedIn() {
  80. return user != null;
  81. }
  82. @Override
  83. public boolean isRoot() {
  84. throw notImplemented();
  85. }
  86. @Override
  87. protected boolean hasPermissionImpl(OrganizationPermission permission) {
  88. throw notImplemented();
  89. }
  90. @Override
  91. protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
  92. throw notImplemented();
  93. }
  94. @Override
  95. protected boolean hasProjectUuidPermission(String permission, String projectUuid) {
  96. throw notImplemented();
  97. }
  98. @Override
  99. public boolean isSystemAdministrator() {
  100. throw notImplemented();
  101. }
  102. private static RuntimeException notImplemented() {
  103. return new UnsupportedOperationException("not implemented");
  104. }
  105. }
  106. }