private List<String> getEligibleGroups(String role, PermissionTemplateDto permissionTemplate) {
List<String> eligibleGroups = new ArrayList<String>();
- if(permissionTemplate.getGroupsPermissions() != null) {
- for (PermissionTemplateGroupDto groupPermission : permissionTemplate.getGroupsPermissions()) {
+ List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplate.getGroupsPermissions();
+ if(groupsPermissions != null) {
+ for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
if(role.equals(groupPermission.getPermission())) {
String groupName = groupPermission.getGroupName() != null ? groupPermission.getGroupName() : DefaultGroups.ANYONE;
eligibleGroups.add(groupName);
private List<String> getEligibleUsers(String role, PermissionTemplateDto permissionTemplate) {
List<String> eligibleUsers = new ArrayList<String>();
- if(permissionTemplate.getUsersPermissions() != null) {
- for (PermissionTemplateUserDto userPermission : permissionTemplate.getUsersPermissions()) {
+ List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
+ if(usersPermissions != null) {
+ for (PermissionTemplateUserDto userPermission : usersPermissions) {
if(role.equals(userPermission.getPermission())) {
eligibleUsers.add(userPermission.getUserLogin());
}
String defaultTemplateId = settings.getString("sonar.permission.template.default");
if(StringUtils.isBlank(defaultTemplateId)) {
- throw new RuntimeException("At least one default permission template should be defined");
+ throw new IllegalStateException("At least one default permission template should be defined");
}
return getTemplateWithPermissions(defaultTemplateId);
}
private PermissionTemplateDto getTemplateWithPermissions(String templateId) {
PermissionTemplateDto permissionTemplateDto = permissionDao.selectTemplateById(Long.parseLong(templateId));
if(permissionTemplateDto == null) {
- throw new RuntimeException("Could not retrieve permission template with id " + templateId);
+ throw new IllegalArgumentException("Could not retrieve permission template with id " + templateId);
}
PermissionTemplateDto templateWithPermissions = permissionDao.selectPermissionTemplate(permissionTemplateDto.getName());
if(templateWithPermissions == null) {
- throw new RuntimeException("Could not retrieve permissions for template with id " + templateId);
+ throw new IllegalArgumentException("Could not retrieve permissions for template with id " + templateId);
}
return templateWithPermissions;
}
return usersPermissions;
}
- public PermissionTemplateDto setUsersPermissions(List<PermissionTemplateUserDto> usersPermissions) {
+ public PermissionTemplateDto setUsersPermissions(@Nullable List<PermissionTemplateUserDto> usersPermissions) {
this.usersPermissions = usersPermissions;
return this;
}
return groupsPermissions;
}
- public PermissionTemplateDto setGroupsByPermission(List<PermissionTemplateGroupDto> groupsPermissions) {
+ public PermissionTemplateDto setGroupsByPermission(@Nullable List<PermissionTemplateGroupDto> groupsPermissions) {
this.groupsPermissions = groupsPermissions;
return this;
}
package org.sonar.core.resource;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
private Settings settings;
private DefaultResourcePermissions permissions;
+ @Rule
+ public ExpectedException throwable = ExpectedException.none();
+
@Before
public void initResourcePermissions() {
settings = new Settings();
// does not exist
assertThat(permissions.hasRoles(new Project("not_found"))).isFalse();
}
+
+ @Test
+ public void should_fail_when_no_default_template_is_defined() throws Exception {
+ throwable.expect(IllegalStateException.class);
+
+ permissions.grantDefaultRoles(project);
+ }
}
}
PermissionTemplate permissionTemplate =
new PermissionTemplate(permissionTemplateDto.getId(), permissionTemplateDto.getName(), permissionTemplateDto.getDescription());
- if(permissionTemplateDto.getUsersPermissions() != null) {
- for (PermissionTemplateUserDto userPermission : permissionTemplateDto.getUsersPermissions()) {
+
+ List<PermissionTemplateUserDto> usersPermissions = permissionTemplateDto.getUsersPermissions();
+ if(usersPermissions != null) {
+ for (PermissionTemplateUserDto userPermission : usersPermissions) {
permissionTemplate.registerUserPermission(permissionTemplateDto.getId(), userPermission.getUserId(),
userPermission.getUserName(), userPermission.getUserLogin(), userPermission.getPermission());
}
}
- if(permissionTemplateDto.getGroupsPermissions() != null) {
- for (PermissionTemplateGroupDto groupPermission : permissionTemplateDto.getGroupsPermissions()) {
+
+ List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplateDto.getGroupsPermissions();
+ if(groupsPermissions != null) {
+ for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
permissionTemplate.registerGroupPermission(groupPermission.getPermission(), permissionTemplateDto.getId(),
groupPermission.getGroupId(), groupPermission.getGroupName());
}