package org.sonar.server.permission;
+import org.apache.commons.lang.StringUtils;
import org.picocontainer.annotations.Nullable;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.util.RubyUtils;
public class ApplyPermissionTemplateQuery {
- private static final String TEMPLATE_ID = "template_id";
+ private static final String TEMPLATE_KEY = "template_key";
private static final String COMPONENTS_KEY = "components";
- private final Long templateId;
+ private final String templateKey;
private List<String> selectedComponents;
- private ApplyPermissionTemplateQuery(@Nullable Long templateId) {
- this.templateId= templateId;
+ private ApplyPermissionTemplateQuery(@Nullable String templateKey) {
+ this.templateKey = templateKey;
}
public static ApplyPermissionTemplateQuery buildFromParams(Map<String, Object> params) {
- ApplyPermissionTemplateQuery query = new ApplyPermissionTemplateQuery(RubyUtils.toLong(params.get(TEMPLATE_ID)));
+ ApplyPermissionTemplateQuery query = new ApplyPermissionTemplateQuery((String)params.get(TEMPLATE_KEY));
query.setSelectedComponents(RubyUtils.toStrings(params.get(COMPONENTS_KEY)));
return query;
}
- public Long getTemplateId() {
- return templateId;
+ public String getTemplateKey() {
+ return templateKey;
}
public List<String> getSelectedComponents() {
}
public void validate() {
- if(templateId == null) {
+ if(StringUtils.isBlank(templateKey)) {
throw new BadRequestException("Permission template is mandatory");
}
if(selectedComponents == null || selectedComponents.isEmpty()) {
ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
query.validate();
for (String component : query.getSelectedComponents()) {
- applyPermissionTemplate(query.getTemplateId(), component);
+ applyPermissionTemplate(query.getTemplateKey(), component);
}
}
- private void applyPermissionTemplate(Long templateId, String componentId) {
- permissionFacade.applyPermissionTemplate(templateId, Long.parseLong(componentId));
+ private void applyPermissionTemplate(String templateKey, String componentId) {
+ permissionFacade.applyPermissionTemplate(templateKey, Long.parseLong(componentId));
}
private void changePermission(String permissionChange, Map<String, Object> params) {
public void should_populate_with_params() throws Exception {
Map<String, Object> params = Maps.newHashMap();
- params.put("template_id", "1");
+ params.put("template_key", "my_template_key");
params.put("components", Lists.newArrayList("1", "2", "3"));
ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
- assertThat(query.getTemplateId()).isEqualTo(1L);
+ assertThat(query.getTemplateKey()).isEqualTo("my_template_key");
assertThat(query.getSelectedComponents()).containsOnly("1", "2", "3");
}
throwable.expectMessage("Permission template is mandatory");
Map<String, Object> params = Maps.newHashMap();
- params.put("template_id", "");
+ params.put("template_key", "");
params.put("components", Lists.newArrayList("1", "2", "3"));
ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
throwable.expectMessage("Please provide at least one entry to which the permission template should be applied");
Map<String, Object> params = Maps.newHashMap();
- params.put("template_id", "1");
+ params.put("template_key", "my_template_key");
params.put("components", Collections.EMPTY_LIST);
ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.buildFromParams(params);
@Test
public void should_apply_permission_template() throws Exception {
params = Maps.newHashMap();
- params.put("template_id", "1");
+ params.put("template_key", "my_template_key");
params.put("components", "1,2,3");
service.applyPermissionTemplate(params);
- verify(permissionFacade).applyPermissionTemplate(1L, 1L);
- verify(permissionFacade).applyPermissionTemplate(1L, 2L);
- verify(permissionFacade).applyPermissionTemplate(1L, 3L);
+ verify(permissionFacade).applyPermissionTemplate("my_template_key", 1L);
+ verify(permissionFacade).applyPermissionTemplate("my_template_key", 2L);
+ verify(permissionFacade).applyPermissionTemplate("my_template_key", 3L);
}
protected static class MatchesUserRole extends BaseMatcher<UserRoleDto> {