*/
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;
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;
}
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();
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) {
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();
}