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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.permission.ws;
  21. import java.util.Optional;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.db.DbClient;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.component.ComponentDto;
  26. import org.sonar.db.permission.template.PermissionTemplateDto;
  27. import org.sonar.db.user.UserDto;
  28. import org.sonar.server.component.ComponentFinder;
  29. import org.sonar.server.permission.GroupUuidOrAnyone;
  30. import org.sonar.server.permission.ProjectUuid;
  31. import org.sonar.server.permission.UserId;
  32. import org.sonar.server.permission.ws.template.WsTemplateRef;
  33. import org.sonar.server.usergroups.ws.GroupWsRef;
  34. import org.sonar.server.usergroups.ws.GroupWsSupport;
  35. import org.sonarqube.ws.client.permission.PermissionsWsParameters;
  36. import static org.sonar.server.exceptions.NotFoundException.checkFound;
  37. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
  38. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
  39. public class PermissionWsSupport {
  40. private final DbClient dbClient;
  41. private final ComponentFinder componentFinder;
  42. private final GroupWsSupport groupWsSupport;
  43. public PermissionWsSupport(DbClient dbClient, ComponentFinder componentFinder, GroupWsSupport groupWsSupport) {
  44. this.dbClient = dbClient;
  45. this.componentFinder = componentFinder;
  46. this.groupWsSupport = groupWsSupport;
  47. }
  48. public Optional<ProjectUuid> findProjectUuid(DbSession dbSession, Request request) {
  49. return findProject(dbSession, request)
  50. .map(ProjectUuid::new);
  51. }
  52. public Optional<ComponentDto> findProject(DbSession dbSession, Request request) {
  53. String uuid = request.param(PermissionsWsParameters.PARAM_PROJECT_ID);
  54. String key = request.param(PermissionsWsParameters.PARAM_PROJECT_KEY);
  55. if (uuid != null || key != null) {
  56. ProjectWsRef ref = ProjectWsRef.newWsProjectRef(uuid, key);
  57. return Optional.of(componentFinder.getRootComponentByUuidOrKey(dbSession, ref.uuid(), ref.key()));
  58. }
  59. return Optional.empty();
  60. }
  61. public ComponentDto getRootComponentOrModule(DbSession dbSession, ProjectWsRef projectRef) {
  62. return componentFinder.getRootComponentByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key());
  63. }
  64. public GroupUuidOrAnyone findGroup(DbSession dbSession, Request request) {
  65. String groupUuid = request.param(PARAM_GROUP_ID);
  66. String groupName = request.param(PARAM_GROUP_NAME);
  67. GroupWsRef groupRef = GroupWsRef.create(groupUuid, groupName);
  68. return groupWsSupport.findGroupOrAnyone(dbSession, groupRef);
  69. }
  70. public UserId findUser(DbSession dbSession, String login) {
  71. UserDto dto = dbClient.userDao().selectActiveUserByLogin(dbSession, login);
  72. checkFound(dto, "User with login '%s' is not found'", login);
  73. return new UserId(dto.getUuid(), dto.getLogin());
  74. }
  75. public PermissionTemplateDto findTemplate(DbSession dbSession, WsTemplateRef ref) {
  76. if (ref.uuid() != null) {
  77. return checkFound(
  78. dbClient.permissionTemplateDao().selectByUuid(dbSession, ref.uuid()),
  79. "Permission template with id '%s' is not found", ref.uuid());
  80. } else {
  81. return checkFound(
  82. dbClient.permissionTemplateDao().selectByName(dbSession, ref.name()),
  83. "Permission template with name '%s' is not found (case insensitive)", ref.name());
  84. }
  85. }
  86. }