*/
package org.sonar.server.permission.ws.template;
+import java.util.Optional;
import java.util.Set;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
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.PermissionPrivilegeChecker.checkProjectAdmin;
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.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-// TODO move to package "template"
public class DeleteTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
private final UserSession userSession;
@Override
public void handle(Request request, Response response) throws Exception {
- checkGlobalAdminUser(userSession);
doHandle(toDeleteTemplateWsRequest(request));
response.noContent();
}
private void doHandle(DeleteTemplateWsRequest request) {
try (DbSession dbSession = dbClient.openSession(false)) {
- PermissionTemplateDto template = finder.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
+ PermissionTemplateDto template = finder.findTemplate(dbSession, newTemplateRef(
+ request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
+ checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.empty());
+
checkTemplateUuidIsNotDefault(template.getUuid());
dbClient.permissionTemplateDao().deleteById(dbSession, template.getId());
dbSession.commit();
private static DeleteTemplateWsRequest toDeleteTemplateWsRequest(Request request) {
return new DeleteTemplateWsRequest()
.setTemplateId(request.param(PARAM_TEMPLATE_ID))
+ .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
.setTemplateName(request.param(PARAM_TEMPLATE_NAME));
}
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.util.collections.Sets.newSet;
-import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplateAction> {
- private static final String TEMPLATE_UUID = "permission-template-uuid";
private static final String ACTION = "delete_template";
private DefaultPermissionTemplateFinder defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
- private PermissionTemplateDto permissionTemplate;
+ private PermissionTemplateDto template;
@Override
protected DeleteTemplateAction buildWsAction() {
@Before
public void setUp() {
- userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ loginAsAdminOnDefaultOrganization();
when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.emptySet());
- permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
+ template = insertTemplateAndAssociatedPermissions();
}
@Test
public void delete_template_in_db() throws Exception {
- WsTester.Result result = newRequest(TEMPLATE_UUID);
+ WsTester.Result result = newRequest(template.getUuid());
assertThat(result.outputAsString()).isEmpty();
- assertThat(db.getDbClient().permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(db.getSession(), TEMPLATE_UUID)).isNull();
+ assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
}
@Test
public void delete_template_by_name_case_insensitive() throws Exception {
wsTester.newPostRequest(CONTROLLER, ACTION)
- .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
+ .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
.execute();
- assertThat(db.getDbClient().permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(db.getSession(), TEMPLATE_UUID)).isNull();
+ assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
}
@Test
@Test
public void fail_if_template_is_default() throws Exception {
+ when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(template.getUuid()));
+
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("It is not possible to delete a default template");
- when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(TEMPLATE_UUID));
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
expectedException.expect(UnauthorizedException.class);
userSession.anonymous();
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
expectedException.expect(ForbiddenException.class);
userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(), new PermissionTemplateCharacteristicDto()
.setPermission(UserRole.USER)
- .setTemplateId(permissionTemplate.getId())
+ .setTemplateId(template.getId())
.setWithProjectCreator(true)
.setCreatedAt(new Date().getTime())
.setUpdatedAt(new Date().getTime()));
db.commit();
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
- assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(), asList(permissionTemplate.getId()))).isEmpty();
+ assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(), asList(template.getId()))).isEmpty();
}
- private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
- db.getDbClient().permissionTemplateDao().insert(db.getSession(), template);
+ private PermissionTemplateDto insertTemplateAndAssociatedPermissions() {
+ PermissionTemplateDto dto = addTemplateToDefaultOrganization();
UserDto user = db.getDbClient().userDao().insert(db.getSession(), UserTesting.newUserDto().setActive(true));
GroupDto group = db.getDbClient().groupDao().insert(db.getSession(), GroupTesting.newGroupDto());
- db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), template.getId(), user.getId(), UserRole.ADMIN);
- db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), template.getId(), group.getId(), UserRole.CODEVIEWER);
+ db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getId(), user.getId(), UserRole.ADMIN);
+ db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), dto.getId(), group.getId(), UserRole.CODEVIEWER);
db.commit();
-
- return template;
+ return dto;
}
private WsTester.Result newRequest(@Nullable String id) throws Exception {