]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8279 organization in WS api/permissions/set_default_template
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:09:24 +0000 (23:09 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:16:40 +0000 (23:16 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SetDefaultTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/SetDefaultTemplateWsRequest.java

index 71d1fd0f161a7dddb2239080837da393913278e0..824a0a6ae7ece08b5f2810ea47316310dac15f20 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.permission.ws.template;
 
+import java.util.Optional;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.ResourceTypes;
@@ -35,12 +36,13 @@ import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.client.permission.SetDefaultTemplateWsRequest;
 
 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateQualifier;
 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
 import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
 import static org.sonar.server.ws.WsParameterBuilder.createRootQualifierParameter;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
@@ -84,11 +86,10 @@ public class SetDefaultTemplateAction implements PermissionsWsAction {
   }
 
   private void doHandle(SetDefaultTemplateWsRequest request) {
-    checkGlobalAdminUser(userSession);
-
-    String qualifier = request.getQualifier();
     try (DbSession dbSession = dbClient.openSession(false)) {
-      PermissionTemplateDto template = getTemplate(dbSession, request);
+      String qualifier = request.getQualifier();
+      PermissionTemplateDto template = findTemplate(dbSession, request);
+      checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.empty());
       validateQualifier(qualifier, resourceTypes);
       setDefaultTemplateUuid(dbSession, template.getUuid(), qualifier);
       dbSession.commit();
@@ -99,11 +100,13 @@ public class SetDefaultTemplateAction implements PermissionsWsAction {
     return new SetDefaultTemplateWsRequest()
       .setQualifier(request.param(PARAM_QUALIFIER))
       .setTemplateId(request.param(PARAM_TEMPLATE_ID))
+      .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
       .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
   }
 
-  private PermissionTemplateDto getTemplate(DbSession dbSession, SetDefaultTemplateWsRequest request) {
-    return wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
+  private PermissionTemplateDto findTemplate(DbSession dbSession, SetDefaultTemplateWsRequest request) {
+    return wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(),
+      request.getOrganization(), request.getTemplateName()));
   }
 
   private void setDefaultTemplateUuid(DbSession dbSession, String templateUuid, String qualifier) {
index ceb66fe2fb65deed71813b1abf9bbbd8bef41585..1eefe8d05c1c140def8b1404b19f7d77612fbe6a 100644 (file)
@@ -68,9 +68,11 @@ public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaul
     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
     persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
-    userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+    loginAsAdminOnDefaultOrganization();
 
-    template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
+    template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto()
+      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+      .setUuid("permission-template-uuid"));
     db.commit();
   }
 
index f8218e9c78773030821d1ad2f9bde103e0cc38f0..1ec97aa2d4e5b34e1ae268033c502a8615eb7613 100644 (file)
@@ -25,6 +25,7 @@ import javax.annotation.Nullable;
 public class SetDefaultTemplateWsRequest {
   private String qualifier;
   private String templateId;
+  private String organization;
   private String templateName;
 
   @CheckForNull
@@ -47,6 +48,16 @@ public class SetDefaultTemplateWsRequest {
     return this;
   }
 
+  @CheckForNull
+  public String getOrganization() {
+    return organization;
+  }
+
+  public SetDefaultTemplateWsRequest setOrganization(@Nullable String s) {
+    this.organization = s;
+    return this;
+  }
+
   @CheckForNull
   public String getTemplateName() {
     return templateName;