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.

AnticipatedTransitionsActionValidator.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.issue.ws.anticipatedtransition;
  21. import java.util.Objects;
  22. import org.sonar.db.DbClient;
  23. import org.sonar.db.DbSession;
  24. import org.sonar.db.project.ProjectDto;
  25. import org.sonar.server.component.ComponentFinder;
  26. import org.sonar.server.exceptions.NotFoundException;
  27. import org.sonar.server.user.UserSession;
  28. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  29. import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
  30. public class AnticipatedTransitionsActionValidator {
  31. private final DbClient dbClient;
  32. private final ComponentFinder componentFinder;
  33. private final UserSession userSession;
  34. public AnticipatedTransitionsActionValidator(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
  35. this.dbClient = dbClient;
  36. this.componentFinder = componentFinder;
  37. this.userSession = userSession;
  38. }
  39. public ProjectDto validateProjectKey(String projectKey) {
  40. try (DbSession dbSession = dbClient.openSession(false)) {
  41. return componentFinder.getProjectByKey(dbSession, projectKey);
  42. } catch (NotFoundException e) {
  43. // To hide information about the existence or not of the project.
  44. throw insufficientPrivilegesException();
  45. }
  46. }
  47. public String validateUserLoggedIn() {
  48. userSession.checkLoggedIn();
  49. return userSession.getUuid();
  50. }
  51. public void validateUserHasAdministerIssuesPermission(String projectUuid) {
  52. try (DbSession dbSession = dbClient.openSession(false)) {
  53. String userUuid = Objects.requireNonNull(userSession.getUuid());
  54. if (!dbClient.authorizationDao().selectEntityPermissions(dbSession, projectUuid, userUuid).contains(ISSUE_ADMIN)){
  55. throw insufficientPrivilegesException();
  56. }
  57. }
  58. }
  59. }