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.

DoPrivileged.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.Collections;
  23. import java.util.Optional;
  24. import org.sonar.db.permission.OrganizationPermission;
  25. import org.sonar.db.user.GroupDto;
  26. /**
  27. * Allow code to be executed with the highest privileges possible, as if executed by a {@link OrganizationPermission#ADMINISTER} account.
  28. * @since 4.3
  29. */
  30. public final class DoPrivileged {
  31. private DoPrivileged() {
  32. // Only static stuff
  33. }
  34. /**
  35. * Executes the task's <code>{@link Task#doPrivileged() doPrivileged}</code> method in a privileged environment.
  36. * @param task
  37. */
  38. public static void execute(Task task) {
  39. try {
  40. task.start();
  41. task.doPrivileged();
  42. } finally {
  43. task.stop();
  44. }
  45. }
  46. /**
  47. * Define a task that will be executed using the highest privileges available. The privileged section is restricted
  48. * to the execution of the {@link #doPrivileged()} method.
  49. */
  50. public abstract static class Task {
  51. private final ThreadLocalUserSession threadLocalUserSession;
  52. private UserSession oldUserSession;
  53. protected Task(ThreadLocalUserSession threadLocalUserSession) {
  54. this.threadLocalUserSession = threadLocalUserSession;
  55. }
  56. /**
  57. * Code placed in this method will be executed in a privileged environment.
  58. */
  59. protected abstract void doPrivileged();
  60. private static class PrivilegedUserSession extends AbstractUserSession {
  61. @Override
  62. public String getLogin() {
  63. return null;
  64. }
  65. @Override
  66. public String getUuid() {
  67. return null;
  68. }
  69. @Override
  70. public String getName() {
  71. return null;
  72. }
  73. @Override
  74. public Collection<GroupDto> getGroups() {
  75. return Collections.emptyList();
  76. }
  77. @Override
  78. public boolean isLoggedIn() {
  79. return false;
  80. }
  81. @Override
  82. public boolean isRoot() {
  83. return true;
  84. }
  85. @Override
  86. public Optional<IdentityProvider> getIdentityProvider() {
  87. return Optional.empty();
  88. }
  89. @Override
  90. public Optional<ExternalIdentity> getExternalIdentity() {
  91. return Optional.empty();
  92. }
  93. @Override
  94. protected boolean hasPermissionImpl(OrganizationPermission permission) {
  95. return true;
  96. }
  97. @Override
  98. protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
  99. // always root so unused
  100. throw new UnsupportedOperationException();
  101. }
  102. @Override
  103. protected boolean hasProjectUuidPermission(String permission, String projectUuid) {
  104. return true;
  105. }
  106. @Override
  107. public boolean isSystemAdministrator() {
  108. return true;
  109. }
  110. }
  111. private void start() {
  112. oldUserSession = threadLocalUserSession.hasSession() ? threadLocalUserSession.get() : null;
  113. threadLocalUserSession.set(new PrivilegedUserSession());
  114. }
  115. private void stop() {
  116. threadLocalUserSession.unload();
  117. if (oldUserSession != null) {
  118. threadLocalUserSession.set(oldUserSession);
  119. }
  120. }
  121. }
  122. }