}
}
- public PermissionTemplateDto getPermissionTemplate(Long templateId) {
- PermissionTemplateDto permissionTemplateDto = permissionDao.selectTemplateById(templateId);
+ public PermissionTemplateDto getPermissionTemplate(String templateKey) {
+ PermissionTemplateDto permissionTemplateDto = permissionDao.selectTemplateByKey(templateKey);
if(permissionTemplateDto == null) {
- throw new IllegalArgumentException("Could not retrieve permission template with id " + templateId);
+ throw new IllegalArgumentException("Could not retrieve permission template with key " + templateKey);
}
PermissionTemplateDto templateWithPermissions = permissionDao.selectPermissionTemplate(permissionTemplateDto.getName());
if(templateWithPermissions == null) {
- throw new IllegalArgumentException("Could not retrieve permissions for template with id " + templateId);
+ throw new IllegalArgumentException("Could not retrieve permissions for template with key " + templateKey);
}
return templateWithPermissions;
}
- public void applyPermissionTemplate(Long templateId, Long resourceId) {
+ public void applyPermissionTemplate(String templateKey, Long resourceId) {
SqlSession session = myBatis.openSession();
try {
removeAllPermissions(resourceId, session);
- PermissionTemplateDto permissionTemplate = getPermissionTemplate(templateId);
+ PermissionTemplateDto permissionTemplate = getPermissionTemplate(templateKey);
List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
if(usersPermissions != null) {
for (PermissionTemplateUserDto userPermission : usersPermissions) {
}
@CheckForNull
- public PermissionTemplateDto selectTemplateById(Long templateId) {
+ public PermissionTemplateDto selectTemplateByKey(String templateKey) {
SqlSession session = myBatis.openSession();
try {
PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class);
- return mapper.selectById(templateId);
+ return mapper.selectByKey(templateKey);
} finally {
MyBatis.closeQuietly(session);
}
PermissionTemplateDto selectByName(String templateName);
- PermissionTemplateDto selectById(Long templateId);
+ PermissionTemplateDto selectByKey(String templateKey);
PermissionTemplateDto selectTemplateUsersPermissions(String templateName);
}
private PermissionTemplateDto getPermissionTemplate(String qualifier) {
- String qualifierTemplateId = settings.getString("sonar.permission.template." + qualifier + ".default");
- if(!StringUtils.isBlank(qualifierTemplateId)) {
- Long templateId = Long.parseLong(qualifierTemplateId);
- return permissionFacade.getPermissionTemplate(templateId);
+ String qualifierTemplateKey = settings.getString("sonar.permission.template." + qualifier + ".default");
+ if(!StringUtils.isBlank(qualifierTemplateKey)) {
+ return permissionFacade.getPermissionTemplate(qualifierTemplateKey);
}
- String defaultTemplateId = settings.getString("sonar.permission.template.default");
- if(StringUtils.isBlank(defaultTemplateId)) {
+ String defaultTemplateKey = settings.getString("sonar.permission.template.default");
+ if(StringUtils.isBlank(defaultTemplateKey)) {
throw new IllegalStateException("At least one default permission template should be defined");
}
- Long templateId = Long.parseLong(defaultTemplateId);
- return permissionFacade.getPermissionTemplate(templateId);
+ return permissionFacade.getPermissionTemplate(defaultTemplateKey);
}
}
</choose>
</delete>
- <select id="selectById" parameterType="Long" resultType="PermissionTemplate">
- SELECT id, name, description, created_at AS createdAt, updated_at AS updatedAt
+ <select id="selectByKey" parameterType="String" resultType="PermissionTemplate">
+ SELECT id, name, kee, description, created_at AS createdAt, updated_at AS updatedAt
FROM permission_templates
- WHERE id = #{templateId}
+ WHERE kee = #{kee}
</select>
<select id="selectByName" parameterType="String" resultType="PermissionTemplate">
public void should_apply_permission_template() throws Exception {
setupData("should_apply_permission_template");
- permissionFacade.applyPermissionTemplate(1L, 123L);
+ permissionFacade.applyPermissionTemplate("default_20130101_010203", 123L);
checkTable("should_apply_permission_template", "group_roles", "group_id", "resource_id", "role");
checkTable("should_apply_permission_template", "user_roles", "group_id", "resource_id", "role");
public void should_select_permission_template_by_id() throws Exception {
setupData("selectPermissionTemplate");
- PermissionTemplateDto permissionTemplate = permissionDao.selectTemplateById(1L);
+ PermissionTemplateDto permissionTemplate = permissionDao.selectTemplateByKey("my_template_20130102_030405");
assertThat(permissionTemplate).isNotNull();
assertThat(permissionTemplate.getId()).isEqualTo(1L);
public void grantDefaultRoles_qualifier_independent() {
setupData("grantDefaultRoles");
- settings.setProperty("sonar.permission.template.default", "1");
+ settings.setProperty("sonar.permission.template.default", "default_template_20130101_010203");
permissions.grantDefaultRoles(project);
public void grantDefaultRoles_qualifier_specific() {
setupData("grantDefaultRolesProject");
- settings.setProperty("sonar.permission.template.default", "1");
- settings.setProperty("sonar.permission.template.TRK.default", "2");
+ settings.setProperty("sonar.permission.template.default", "default_20130101_010203");
+ settings.setProperty("sonar.permission.template.TRK.default", "default_for_trk_20130101_010203");
permissions.grantDefaultRoles(project);
public void grantDefaultRoles_unknown_group() {
setupData("grantDefaultRoles_unknown_group");
- settings.setProperty("sonar.permission.template.TRK.default", "1");
+ settings.setProperty("sonar.permission.template.TRK.default", "default_template_20130101_010203");
permissions.grantDefaultRoles(project);
checkTables("grantDefaultRoles_unknown_group", "group_roles");
public void grantDefaultRoles_users() {
setupData("grantDefaultRoles_users");
- settings.setProperty("sonar.permission.template.TRK.default", "1");
+ settings.setProperty("sonar.permission.template.TRK.default", "default_for_trk_20130101_010203");
permissions.grantDefaultRoles(project);
checkTables("grantDefaultRoles_users", "user_roles");
default_template = PermissionTemplate.create(
:name => 'Default template',
+ :kee => 'default_template',
:description => 'This permission template will be used as default when no other permission configuration is available')
sonar_admins_group = Group.find_by_name('sonar-administrators')
qualifier_template = PermissionTemplate.create(
:name => "Default template for #{ROOT_QUALIFIERS[qualifier]}",
+ :kee => "default_template_for_#{ROOT_QUALIFIERS[qualifier].downcase}",
:description => "This template has been automatically created using the previously configured default permissions for #{ROOT_QUALIFIERS[qualifier]}")
properties.each do |property|