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.

ApplyTemplateAction.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.template;
  21. import java.util.Collections;
  22. import java.util.Optional;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.resources.Qualifiers;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.entity.EntityDto;
  32. import org.sonar.db.permission.template.PermissionTemplateDto;
  33. import org.sonar.server.exceptions.NotFoundException;
  34. import org.sonar.server.common.management.ManagedInstanceChecker;
  35. import org.sonar.server.common.permission.PermissionTemplateService;
  36. import org.sonar.server.permission.ws.PermissionWsSupport;
  37. import org.sonar.server.permission.ws.PermissionsWsAction;
  38. import org.sonar.server.permission.ws.ProjectWsRef;
  39. import org.sonar.server.user.UserSession;
  40. import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
  41. import static org.sonar.server.permission.ws.WsParameters.createProjectParameters;
  42. import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
  43. import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
  44. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
  45. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
  46. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
  47. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
  48. public class ApplyTemplateAction implements PermissionsWsAction {
  49. private final DbClient dbClient;
  50. private final UserSession userSession;
  51. private final PermissionTemplateService permissionTemplateService;
  52. private final PermissionWsSupport wsSupport;
  53. private final ManagedInstanceChecker managedInstanceChecker;
  54. public ApplyTemplateAction(DbClient dbClient, UserSession userSession, PermissionTemplateService permissionTemplateService,
  55. PermissionWsSupport wsSupport, ManagedInstanceChecker managedInstanceChecker) {
  56. this.dbClient = dbClient;
  57. this.userSession = userSession;
  58. this.permissionTemplateService = permissionTemplateService;
  59. this.wsSupport = wsSupport;
  60. this.managedInstanceChecker = managedInstanceChecker;
  61. }
  62. private static ApplyTemplateRequest toApplyTemplateWsRequest(Request request) {
  63. return new ApplyTemplateRequest()
  64. .setProjectId(request.param(PARAM_PROJECT_ID))
  65. .setProjectKey(request.param(PARAM_PROJECT_KEY))
  66. .setTemplateId(request.param(PARAM_TEMPLATE_ID))
  67. .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
  68. }
  69. @Override
  70. public void define(WebService.NewController context) {
  71. WebService.NewAction action = context.createAction("apply_template")
  72. .setDescription("Apply a permission template to one project.<br>" +
  73. "The project id or project key must be provided.<br>" +
  74. "The template id or name must be provided.<br>" +
  75. "Requires the following permission: 'Administer System'.")
  76. .setPost(true)
  77. .setSince("5.2")
  78. .setHandler(this);
  79. createTemplateParameters(action);
  80. createProjectParameters(action);
  81. }
  82. @Override
  83. public void handle(Request request, Response response) throws Exception {
  84. doHandle(toApplyTemplateWsRequest(request));
  85. response.noContent();
  86. }
  87. private void doHandle(ApplyTemplateRequest request) {
  88. try (DbSession dbSession = dbClient.openSession(false)) {
  89. PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
  90. request.getTemplateId(), request.getTemplateName()));
  91. ProjectWsRef.validateUuidAndKeyPair(request.getProjectId(), request.getProjectKey());
  92. EntityDto entityDto = getEntityByKeyOrUuid(request.getProjectId(), request.getProjectKey(), dbSession);
  93. checkGlobalAdmin(userSession);
  94. if (entityDto.isProject()) {
  95. managedInstanceChecker.throwIfProjectIsManaged(dbSession, entityDto.getUuid());
  96. }
  97. permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(entityDto));
  98. }
  99. }
  100. private EntityDto getEntityByKeyOrUuid(@Nullable String uuid, @Nullable String key, DbSession dbSession) {
  101. Optional<EntityDto> entityDto = uuid != null ? dbClient.entityDao().selectByUuid(dbSession, uuid) : dbClient.entityDao().selectByKey(dbSession, key);
  102. if (entityDto.isPresent() && !Qualifiers.SUBVIEW.equals(entityDto.get().getQualifier())) {
  103. return entityDto.get();
  104. } else {
  105. throw new NotFoundException("Entity not found");
  106. }
  107. }
  108. private static class ApplyTemplateRequest {
  109. private String projectId;
  110. private String projectKey;
  111. private String templateId;
  112. private String templateName;
  113. @CheckForNull
  114. public String getProjectId() {
  115. return projectId;
  116. }
  117. public ApplyTemplateRequest setProjectId(@Nullable String projectId) {
  118. this.projectId = projectId;
  119. return this;
  120. }
  121. @CheckForNull
  122. public String getProjectKey() {
  123. return projectKey;
  124. }
  125. public ApplyTemplateRequest setProjectKey(@Nullable String projectKey) {
  126. this.projectKey = projectKey;
  127. return this;
  128. }
  129. @CheckForNull
  130. public String getTemplateId() {
  131. return templateId;
  132. }
  133. public ApplyTemplateRequest setTemplateId(@Nullable String templateId) {
  134. this.templateId = templateId;
  135. return this;
  136. }
  137. @CheckForNull
  138. public String getTemplateName() {
  139. return templateName;
  140. }
  141. public ApplyTemplateRequest setTemplateName(@Nullable String templateName) {
  142. this.templateName = templateName;
  143. return this;
  144. }
  145. }
  146. }