import org.sonar.db.permission.PermissionTemplateDto;
import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder;
import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
-import static org.sonar.server.permission.ws.WsTemplateRef.fromRequest;
+import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
import static org.sonar.server.ws.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
public class DeleteTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
}
@Override
- public void handle(Request wsRequest, Response wsResponse) throws Exception {
+ public void handle(Request request, Response response) throws Exception {
checkGlobalAdminUser(userSession);
+ doHandle(toDeleteTemplateWsRequest(request));
+ response.noContent();
+ }
+ private void doHandle(DeleteTemplateWsRequest request) {
DbSession dbSession = dbClient.openSession(false);
try {
- PermissionTemplateDto template = finder.getTemplate(dbSession, fromRequest(wsRequest));
+ PermissionTemplateDto template = finder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
checkTemplateUuidIsNotDefault(template.getUuid());
dbClient.permissionTemplateDao().deleteById(dbSession, template.getId());
dbSession.commit();
} finally {
dbClient.closeSession(dbSession);
}
+ }
- wsResponse.noContent();
+ private static DeleteTemplateWsRequest toDeleteTemplateWsRequest(Request request) {
+ return new DeleteTemplateWsRequest()
+ .setTemplateId(request.param(PARAM_TEMPLATE_ID))
+ .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
}
private void checkTemplateUuidIsNotDefault(String key) {
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonarqube.ws.client.permission;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class DeleteTemplateWsRequest {
+ private String templateId;
+ private String templateName;
+
+ @CheckForNull
+ public String getTemplateId() {
+ return templateId;
+ }
+
+ public DeleteTemplateWsRequest setTemplateId(@Nullable String templateId) {
+ this.templateId = templateId;
+ return this;
+ }
+
+ @CheckForNull
+ public String getTemplateName() {
+ return templateName;
+ }
+
+ public DeleteTemplateWsRequest setTemplateName(@Nullable String templateName) {
+ this.templateName = templateName;
+ return this;
+ }
+}
CreateTemplateWsResponse.parser());
}
+ public void deleteTemplate(DeleteTemplateWsRequest request) {
+ wsClient.execute(newPostRequest(action("delete_template"))
+ .setParam(PARAM_TEMPLATE_ID, request.getTemplateId())
+ .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName()));
+ }
+
private static String action(String action) {
return PermissionsWsParameters.ENDPOINT + "/" + action;
}