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.

PermissionWsSupport.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.permission.ws;
  21. import java.util.Optional;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.config.Configuration;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.component.ComponentDto;
  28. import org.sonar.db.permission.template.PermissionTemplateDto;
  29. import org.sonar.db.user.UserDto;
  30. import org.sonar.db.user.UserId;
  31. import org.sonar.db.user.UserIdDto;
  32. import org.sonar.server.component.ComponentFinder;
  33. import org.sonar.server.exceptions.NotFoundException;
  34. import org.sonar.server.permission.GroupUuidOrAnyone;
  35. import org.sonar.server.permission.ws.template.WsTemplateRef;
  36. import org.sonar.server.user.UserSession;
  37. import org.sonar.server.usergroups.ws.GroupWsRef;
  38. import org.sonar.server.usergroups.ws.GroupWsSupport;
  39. import org.sonarqube.ws.client.permission.PermissionsWsParameters;
  40. import static com.google.common.base.Preconditions.checkNotNull;
  41. import static java.lang.String.format;
  42. import static java.util.Optional.ofNullable;
  43. import static org.sonar.server.exceptions.NotFoundException.checkFound;
  44. import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
  45. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
  46. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
  47. public class PermissionWsSupport {
  48. private final DbClient dbClient;
  49. private final ComponentFinder componentFinder;
  50. private final GroupWsSupport groupWsSupport;
  51. private final Configuration configuration;
  52. public PermissionWsSupport(DbClient dbClient, Configuration configuration, ComponentFinder componentFinder, GroupWsSupport groupWsSupport) {
  53. this.dbClient = dbClient;
  54. this.configuration = configuration;
  55. this.componentFinder = componentFinder;
  56. this.groupWsSupport = groupWsSupport;
  57. }
  58. public void checkPermissionManagementAccess(UserSession userSession, @Nullable ComponentDto project) {
  59. checkProjectAdmin(userSession, configuration, project);
  60. }
  61. public Optional<ComponentDto> findProject(DbSession dbSession, Request request) {
  62. String uuid = request.param(PermissionsWsParameters.PARAM_PROJECT_ID);
  63. String key = request.param(PermissionsWsParameters.PARAM_PROJECT_KEY);
  64. if (uuid != null || key != null) {
  65. ProjectWsRef ref = ProjectWsRef.newWsProjectRef(uuid, key);
  66. return Optional.of(componentFinder.getRootComponentByUuidOrKey(dbSession, ref.uuid(), ref.key()));
  67. }
  68. return Optional.empty();
  69. }
  70. public ComponentDto getRootComponentOrModule(DbSession dbSession, ProjectWsRef projectRef) {
  71. return componentFinder.getRootComponentByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key());
  72. }
  73. public GroupUuidOrAnyone findGroup(DbSession dbSession, Request request) {
  74. String groupUuid = request.param(PARAM_GROUP_ID);
  75. String groupName = request.param(PARAM_GROUP_NAME);
  76. GroupWsRef groupRef = GroupWsRef.create(groupUuid, groupName);
  77. return groupWsSupport.findGroupOrAnyone(dbSession, groupRef);
  78. }
  79. public UserId findUser(DbSession dbSession, String login) {
  80. UserDto dto = ofNullable(dbClient.userDao().selectActiveUserByLogin(dbSession, login))
  81. .orElseThrow(() -> new NotFoundException(format("User with login '%s' is not found'", login)));
  82. return new UserIdDto(dto.getUuid(), dto.getLogin());
  83. }
  84. public PermissionTemplateDto findTemplate(DbSession dbSession, WsTemplateRef ref) {
  85. String uuid = ref.uuid();
  86. String name = ref.name();
  87. if (uuid != null) {
  88. return checkFound(
  89. dbClient.permissionTemplateDao().selectByUuid(dbSession, uuid),
  90. "Permission template with id '%s' is not found", uuid);
  91. } else {
  92. checkNotNull(name);
  93. return checkFound(
  94. dbClient.permissionTemplateDao().selectByName(dbSession, name),
  95. "Permission template with name '%s' is not found (case insensitive)", name);
  96. }
  97. }
  98. }