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.

HotspotWsSupport.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.hotspot.ws;
  21. import java.util.Date;
  22. import org.sonar.api.issue.Issue;
  23. import org.sonar.api.rules.RuleType;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.core.issue.IssueChangeContext;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.issue.IssueDto;
  31. import org.sonar.server.exceptions.NotFoundException;
  32. import org.sonar.server.user.UserSession;
  33. import static com.google.common.base.Preconditions.checkArgument;
  34. import static java.lang.String.format;
  35. public class HotspotWsSupport {
  36. private final DbClient dbClient;
  37. private final UserSession userSession;
  38. private final System2 system2;
  39. public HotspotWsSupport(DbClient dbClient, UserSession userSession, System2 system2) {
  40. this.dbClient = dbClient;
  41. this.userSession = userSession;
  42. this.system2 = system2;
  43. }
  44. String checkLoggedIn() {
  45. return userSession.checkLoggedIn().getUuid();
  46. }
  47. IssueDto loadHotspot(DbSession dbSession, String hotspotKey) {
  48. return dbClient.issueDao().selectByKey(dbSession, hotspotKey)
  49. .filter(t -> t.getType() == RuleType.SECURITY_HOTSPOT.getDbConstant())
  50. .filter(t -> !Issue.STATUS_CLOSED.equals(t.getStatus()))
  51. .orElseThrow(() -> new NotFoundException(format("Hotspot '%s' does not exist", hotspotKey)));
  52. }
  53. ComponentDto loadAndCheckProject(DbSession dbSession, IssueDto hotspot, String userRole) {
  54. String projectUuid = hotspot.getProjectUuid();
  55. checkArgument(projectUuid != null, "Hotspot '%s' has no project", hotspot.getKee());
  56. ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, projectUuid)
  57. .orElseThrow(() -> new NotFoundException(format("Project with uuid '%s' does not exist", projectUuid)));
  58. userSession.checkComponentPermission(userRole, project);
  59. return project;
  60. }
  61. boolean canChangeStatus(ComponentDto project) {
  62. return userSession.hasComponentPermission(UserRole.SECURITYHOTSPOT_ADMIN, project);
  63. }
  64. IssueChangeContext newIssueChangeContext() {
  65. return IssueChangeContext.createUser(new Date(system2.now()), checkLoggedIn());
  66. }
  67. }