diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-10-16 23:02:45 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-10-16 23:16:37 +0200 |
commit | 6189f1973d24799939b1d689a3242820e2494da6 (patch) | |
tree | 6dd48509b947b07a9788d9ac30fe8be85768bbea /sonar-db | |
parent | 25a69084fff50608f9996efd3ffc1245df219a84 (diff) | |
download | sonarqube-6189f1973d24799939b1d689a3242820e2494da6.tar.gz sonarqube-6189f1973d24799939b1d689a3242820e2494da6.zip |
SONAR-8272 SONAR-8273 organization in WS to apply permission templates
Diffstat (limited to 'sonar-db')
10 files changed, 47 insertions, 231 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java index 05e2a39f85c..86e0d81d921 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionRepository.java @@ -25,18 +25,19 @@ import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.config.Settings; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; -import org.sonar.db.permission.template.PermissionTemplate; import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.db.permission.template.PermissionTemplateGroupDto; import org.sonar.db.permission.template.PermissionTemplateUserDto; +import static java.util.Arrays.asList; import static org.sonar.api.security.DefaultGroups.isAnyone; /** @@ -56,35 +57,30 @@ public class PermissionRepository { this.settings = settings; } - public void applyPermissionTemplate(DbSession session, String templateUuid, ComponentDto project) { - applyPermissionTemplate(session, templateUuid, project, null); - } - - private void applyPermissionTemplate(DbSession session, String templateUuid, ComponentDto project, @Nullable Long currentUserId) { - PermissionTemplate template = dbClient.permissionTemplateDao().selectPermissionTemplateWithPermissions(session, templateUuid); + public void apply(DbSession session, PermissionTemplateDto template, ComponentDto project, @Nullable Long currentUserId) { updateProjectAuthorizationDate(session, project.getId()); dbClient.groupPermissionDao().deleteByRootComponentId(session, project.getId()); dbClient.userPermissionDao().deleteProjectPermissions(session, project.getId()); - List<PermissionTemplateUserDto> usersPermissions = template.getUserPermissions(); - String organizationUuid = template.getTemplate().getOrganizationUuid(); + List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(session, template.getId()); + String organizationUuid = template.getOrganizationUuid(); usersPermissions .forEach(up -> { UserPermissionDto dto = new UserPermissionDto(organizationUuid, up.getPermission(), up.getUserId(), project.getId()); dbClient.userPermissionDao().insert(session, dto); }); - List<PermissionTemplateGroupDto> groupsPermissions = template.getGroupPermissions(); + List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateId(session, template.getId()); groupsPermissions.forEach(gp -> { GroupPermissionDto dto = new GroupPermissionDto() - .setOrganizationUuid(template.getTemplate().getOrganizationUuid()) + .setOrganizationUuid(organizationUuid) .setGroupId(isAnyone(gp.getGroupName()) ? null : gp.getGroupId()) .setRole(gp.getPermission()) .setResourceId(project.getId()); dbClient.groupPermissionDao().insert(session, dto); }); - List<PermissionTemplateCharacteristicDto> characteristics = template.getCharacteristics(); + List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(session, asList(template.getId())); if (currentUserId != null) { Set<String> permissionsForCurrentUserAlreadyInDb = usersPermissions.stream() .filter(userPermission -> currentUserId.equals(userPermission.getUserId())) @@ -94,20 +90,13 @@ public class PermissionRepository { .filter(PermissionTemplateCharacteristicDto::getWithProjectCreator) .filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())) .forEach(c -> { - UserPermissionDto dto = new UserPermissionDto(template.getTemplate().getOrganizationUuid(), c.getPermission(), currentUserId, project.getId()); + UserPermissionDto dto = new UserPermissionDto(organizationUuid, c.getPermission(), currentUserId, project.getId()); dbClient.userPermissionDao().insert(session, dto); }); } } /** - * For each modification of permission on a project, update the authorization_updated_at to help ES reindex only relevant changes - */ - private void updateProjectAuthorizationDate(DbSession dbSession, long projectId) { - dbClient.resourceDao().updateAuthorizationDate(projectId, dbSession); - } - - /** * Warning, this method is also used by the Developer Cockpit plugin */ public void applyDefaultPermissionTemplate(DbSession session, long componentId) { @@ -116,49 +105,51 @@ public class PermissionRepository { } public void applyDefaultPermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable Long userId) { - String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(dbSession, componentDto.getKey(), componentDto.qualifier()); - applyPermissionTemplate(dbSession, applicablePermissionTemplateKey, componentDto, userId); + PermissionTemplateDto template = getApplicablePermissionTemplate(dbSession, componentDto); + if (template == null) { + throw new IllegalArgumentException("Can not retrieve default permission template"); + } + apply(dbSession, template, componentDto, userId); } /** * Return the permission template for the given componentKey. If no template key pattern match then consider default * permission template for the resource qualifier. */ - private String getApplicablePermissionTemplateKey(DbSession session, final String componentKey, String qualifier) { + @CheckForNull + private PermissionTemplateDto getApplicablePermissionTemplate(DbSession dbSession, ComponentDto component) { // FIXME performance issue here, we should not load all templates - List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(session); + List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession); List<PermissionTemplateDto> matchingTemplates = new ArrayList<>(); for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) { String keyPattern = permissionTemplateDto.getKeyPattern(); - if (StringUtils.isNotBlank(keyPattern) && componentKey.matches(keyPattern)) { + if (StringUtils.isNotBlank(keyPattern) && component.getKey().matches(keyPattern)) { matchingTemplates.add(permissionTemplateDto); } } - checkAtMostOneMatchForComponentKey(componentKey, matchingTemplates); + checkAtMostOneMatchForComponentKey(component.getKey(), matchingTemplates); if (matchingTemplates.size() == 1) { - return matchingTemplates.get(0).getUuid(); + return matchingTemplates.get(0); } - String qualifierTemplateKey = settings.getString("sonar.permission.template." + qualifier + ".default"); + String qualifierTemplateKey = settings.getString("sonar.permission.template." + component.qualifier() + ".default"); if (!StringUtils.isBlank(qualifierTemplateKey)) { - return qualifierTemplateKey; + return dbClient.permissionTemplateDao().selectByUuid(dbSession, qualifierTemplateKey); } String defaultTemplateKey = settings.getString("sonar.permission.template.default"); if (StringUtils.isBlank(defaultTemplateKey)) { throw new IllegalStateException("At least one default permission template should be defined"); } - return defaultTemplateKey; + return dbClient.permissionTemplateDao().selectByUuid(dbSession, defaultTemplateKey); } public boolean wouldUserHavePermissionWithDefaultTemplate(DbSession dbSession, @Nullable Long currentUserId, String permission, String projectKey, String qualifier) { - String templateUuid = getApplicablePermissionTemplateKey(dbSession, projectKey, qualifier); - PermissionTemplateDto template = dbClient.permissionTemplateDao().selectByUuid(dbSession, templateUuid); + PermissionTemplateDto template = getApplicablePermissionTemplate(dbSession, new ComponentDto().setKey(projectKey).setQualifier(qualifier)); if (template == null) { return false; } List<String> potentialPermissions = dbClient.permissionTemplateDao().selectPotentialPermissionsByUserIdAndTemplateId(dbSession, currentUserId, template.getId()); - return potentialPermissions.contains(permission); } @@ -178,4 +169,11 @@ public class PermissionRepository { templatesNames.toString())); } } + + /** + * For each modification of permission on a project, update the authorization_updated_at to help ES reindex only relevant changes + */ + private void updateProjectAuthorizationDate(DbSession dbSession, long projectId) { + dbClient.resourceDao().updateAuthorizationDate(projectId, dbSession); + } } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplate.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplate.java deleted file mode 100644 index 9f62d8a583f..00000000000 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplate.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program 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. - * - * This program 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.sonar.db.permission.template; - -import java.util.List; -import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; -import org.sonar.db.permission.template.PermissionTemplateDto; -import org.sonar.db.permission.template.PermissionTemplateGroupDto; -import org.sonar.db.permission.template.PermissionTemplateUserDto; - -public class PermissionTemplate { - private final PermissionTemplateDto template; - private final List<PermissionTemplateUserDto> userPermissions; - private final List<PermissionTemplateGroupDto> groupPermissions; - private final List<PermissionTemplateCharacteristicDto> characteristics; - - public PermissionTemplate(PermissionTemplateDto template, - List<PermissionTemplateUserDto> userPermissions, - List<PermissionTemplateGroupDto> groupPermissions, - List<PermissionTemplateCharacteristicDto> characteristics) { - this.template = template; - this.userPermissions = userPermissions; - this.groupPermissions = groupPermissions; - this.characteristics = characteristics; - } - - public PermissionTemplateDto getTemplate() { - return template; - } - - public List<PermissionTemplateUserDto> getUserPermissions() { - return userPermissions; - } - - public List<PermissionTemplateGroupDto> getGroupPermissions() { - return groupPermissions; - } - - public List<PermissionTemplateCharacteristicDto> getCharacteristics() { - return characteristics; - } -} diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java index 05e9ff0360f..d7be52538d7 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicDao.java @@ -30,6 +30,7 @@ import static java.util.Collections.emptyList; import static java.util.Objects.requireNonNull; public class PermissionTemplateCharacteristicDao implements Dao { + public List<PermissionTemplateCharacteristicDto> selectByTemplateIds(DbSession dbSession, List<Long> templateIds) { return templateIds.isEmpty() ? emptyList() : mapper(dbSession).selectByTemplateIds(templateIds); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java index c2bba6f8f0b..801456402b9 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.java @@ -24,8 +24,6 @@ import java.util.List; import org.apache.ibatis.annotations.Param; public interface PermissionTemplateCharacteristicMapper { - List<PermissionTemplateCharacteristicDto> selectByTemplateId(long templateId); - List<PermissionTemplateCharacteristicDto> selectByTemplateIds(@Param("templateIds") List<Long> templateId); PermissionTemplateCharacteristicDto selectByPermissionAndTemplateId(@Param("permission") String permission, @Param("templateId") long templateId); diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java index 9e234fd7796..8fda58aa11b 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java @@ -65,7 +65,7 @@ public class PermissionTemplateDao implements Dao { return executeLargeInputs(logins, l -> mapper(dbSession).selectUserPermissionsByTemplateIdAndUserLogins(templateId, l)); } - private static List<PermissionTemplateUserDto> selectUserPermissionsByTemplateId(DbSession dbSession, long templateId) { + public List<PermissionTemplateUserDto> selectUserPermissionsByTemplateId(DbSession dbSession, long templateId) { return mapper(dbSession).selectUserPermissionsByTemplateIdAndUserLogins(templateId, Collections.emptyList()); } @@ -81,7 +81,7 @@ public class PermissionTemplateDao implements Dao { return executeLargeInputs(groups, g -> mapper(dbSession).selectGroupPermissionsByTemplateIdAndGroupNames(templateId, g)); } - List<PermissionTemplateGroupDto> selectGroupPermissionsByTemplateId(DbSession dbSession, long templateId) { + public List<PermissionTemplateGroupDto> selectGroupPermissionsByTemplateId(DbSession dbSession, long templateId) { return mapper(dbSession).selectGroupPermissionsByTemplateIdAndGroupNames(templateId, Collections.emptyList()); } @@ -97,23 +97,6 @@ public class PermissionTemplateDao implements Dao { return mapper(session).selectByUuid(templateUuid); } - @CheckForNull - public PermissionTemplate selectByUuidWithUserAndGroupPermissions(DbSession session, String templateUuid) { - PermissionTemplateMapper mapper = mapper(session); - - PermissionTemplateDto template = mapper.selectByUuid(templateUuid); - if (template == null) { - return null; - } - - List<PermissionTemplateUserDto> userPermissions = selectUserPermissionsByTemplateId(session, template.getId()); - List<PermissionTemplateGroupDto> groupPermissions = selectGroupPermissionsByTemplateId(session, template.getId()); - PermissionTemplateCharacteristicMapper characteristicMapper = session.getMapper(PermissionTemplateCharacteristicMapper.class); - List<PermissionTemplateCharacteristicDto> characteristics = characteristicMapper.selectByTemplateId(template.getId()); - - return new PermissionTemplate(template, userPermissions, groupPermissions, characteristics); - } - public List<PermissionTemplateDto> selectAll(DbSession session, String nameMatch) { String uppercaseNameMatch = toUppercaseSqlQuery(nameMatch); return mapper(session).selectAll(uppercaseNameMatch); @@ -226,23 +209,8 @@ public class PermissionTemplateDao implements Dao { session.commit(); } - /** - * Load permission template and load associated collections of users and groups permissions, and characteristics - */ - public PermissionTemplate selectPermissionTemplateWithPermissions(DbSession session, String templateUuid) { - PermissionTemplateDto template = selectByUuid(session, templateUuid); - if (template == null) { - throw new IllegalArgumentException("Could not retrieve permission template with uuid " + templateUuid); - } - PermissionTemplate templateWithDependencies = selectByUuidWithUserAndGroupPermissions(session, template.getUuid()); - if (templateWithDependencies == null) { - throw new IllegalArgumentException("Could not retrieve permissions for template with uuid " + templateUuid); - } - return templateWithDependencies; - } - - public PermissionTemplateDto selectByName(DbSession dbSession, String name) { - return mapper(dbSession).selectByName(name.toUpperCase(Locale.ENGLISH)); + public PermissionTemplateDto selectByName(DbSession dbSession, String organizationUuid, String name) { + return mapper(dbSession).selectByName(organizationUuid, name.toUpperCase(Locale.ENGLISH)); } public List<String> selectPotentialPermissionsByUserIdAndTemplateId(DbSession dbSession, @Nullable Long currentUserId, long templateId) { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java index 68a3615c8be..7cd947b94f4 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java @@ -58,7 +58,7 @@ public interface PermissionTemplateMapper { void deleteByGroupId(long groupId); - PermissionTemplateDto selectByName(String name); + PermissionTemplateDto selectByName(@Param("organizationUuid") String organizationUuid, @Param("name") String name); List<String> selectUserLoginsByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId, RowBounds rowBounds); diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml index 00050a13f19..13664b4293e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateCharacteristicMapper.xml @@ -10,15 +10,6 @@ ptc.updated_at as updatedAt </sql> - <select id="selectByTemplateId" parameterType="long" resultType="PermissionTemplateCharacteristic"> - select - <include refid="columns" /> - from perm_tpl_characteristics ptc - where - ptc.template_id = #{templateId} - order by id - </select> - <select id="selectByTemplateIds" parameterType="long" resultType="PermissionTemplateCharacteristic"> select <include refid="columns" /> diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml index 13eaa7be8a3..8d4e454219c 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml @@ -175,11 +175,13 @@ ORDER BY UPPER(name), name </select> - <select id="selectByName" parameterType="String" resultType="PermissionTemplate"> - SELECT + <select id="selectByName" parameterType="map" resultType="PermissionTemplate"> + select <include refid="templateColumns"/> - FROM permission_templates - WHERE UPPER(name)=#{templateName} + from permission_templates + where + organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and + upper(name) = #{name,jdbcType=VARCHAR} </select> <sql id="permissionTemplateUserColumns"> diff --git a/sonar-db/src/test/java/org/sonar/db/permission/PermissionRepositoryTest.java b/sonar-db/src/test/java/org/sonar/db/permission/PermissionRepositoryTest.java index ecdf59dd0b9..4b8ea83df2a 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/PermissionRepositoryTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/PermissionRepositoryTest.java @@ -42,12 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; +import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.user.GroupTesting.newGroupDto; public class PermissionRepositoryTest { private static final String DEFAULT_TEMPLATE = "default_20130101_010203"; - private static final ComponentDto PROJECT = new ComponentDto().setId(123L).setUuid("THE_PROJECT_UUID"); + private static final ComponentDto PROJECT = newProjectDto().setId(123L).setUuid("THE_PROJECT_UUID"); private static final long NOW = 123456789L; @Rule @@ -78,7 +79,8 @@ public class PermissionRepositoryTest { assertThat(roleDao.selectGroupPermissions(session, "Anyone", PROJECT.getId())).isEmpty(); assertThat(dbTester.getDbClient().userPermissionDao().selectPermissionsByLogin(session, "marius", PROJECT.uuid())).isEmpty(); - underTest.applyPermissionTemplate(session, "default_20130101_010203", PROJECT); + PermissionTemplateDto template = dbTester.getDbClient().permissionTemplateDao().selectByUuid(session, "default_20130101_010203"); + underTest.apply(session, template, PROJECT, null); assertThat(roleDao.selectGroupPermissions(session, "sonar-administrators", PROJECT.getId())).containsOnly("admin", "issueadmin"); assertThat(roleDao.selectGroupPermissions(session, "sonar-users", PROJECT.getId())).containsOnly("user", "codeviewer"); diff --git a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java index d891bfda4dc..b0d5bba35b3 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java @@ -39,10 +39,8 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.tuple; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.api.security.DefaultGroups.ANYONE; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.CODEVIEWER; import static org.sonar.api.web.UserRole.ISSUE_ADMIN; @@ -50,7 +48,6 @@ import static org.sonar.api.web.UserRole.USER; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto; import static org.sonar.db.user.GroupTesting.newGroupDto; -import static org.sonar.db.user.UserTesting.newUserDto; public class PermissionTemplateDaoTest { @@ -83,30 +80,6 @@ public class PermissionTemplateDaoTest { } @Test - public void should_select_permission_template() { - db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml"); - - PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "my_template_20130102_030405"); - - assertThat(result).isNotNull(); - PermissionTemplateDto template = result.getTemplate(); - assertThat(template.getName()).isEqualTo("my template"); - assertThat(template.getUuid()).isEqualTo("my_template_20130102_030405"); - assertThat(template.getDescription()).isEqualTo("my description"); - List<PermissionTemplateUserDto> usersPermissions = result.getUserPermissions(); - assertThat(usersPermissions).hasSize(3); - assertThat(usersPermissions).extracting("userId").containsOnly(1L, 2L, 1L); - assertThat(usersPermissions).extracting("userLogin").containsOnly("login1", "login2", "login2"); - assertThat(usersPermissions).extracting("userName").containsOnly("user1", "user2", "user2"); - assertThat(usersPermissions).extracting("permission").containsOnly("user_permission1", "user_permission1", "user_permission2"); - List<PermissionTemplateGroupDto> groupsPermissions = result.getGroupPermissions(); - assertThat(groupsPermissions).hasSize(3); - assertThat(groupsPermissions).extracting("groupId").containsOnly(1L, 2L, 0L); - assertThat(groupsPermissions).extracting("groupName").containsOnly("group1", "group2", "Anyone"); - assertThat(groupsPermissions).extracting("permission").containsOnly("group_permission1", "group_permission1", "group_permission2"); - } - - @Test public void should_select_permission_template_by_key() { db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml"); @@ -207,63 +180,6 @@ public class PermissionTemplateDaoTest { } @Test - public void new_permission_template_with_empty_user_group_characteristics() { - PermissionTemplateDto template = underTest.insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID")); - - PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID"); - - assertThat(result.getTemplate()) - .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription) - .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription()); - - assertThat(result.getUserPermissions()).isEmpty(); - assertThat(result.getGroupPermissions()).isEmpty(); - assertThat(result.getCharacteristics()).isEmpty(); - } - - @Test - public void unknown_permission_template() { - PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "UNKNOWN_TEMPLATE_UUID"); - - assertThat(result).isNull(); - } - - @Test - public void permission_template_with_user_group_and_characteristics() { - PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID")); - GroupDto group = db.users().insertGroup(newGroupDto()); - UserDto user = db.users().insertUser(newUserDto()); - templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN); - templateDb.addGroupToTemplate(template.getId(), null, UserRole.USER); - templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.CODEVIEWER); - templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.USER); - - PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID"); - assertThat(result.getTemplate()) - .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription) - .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription()); - assertThat(result.getCharacteristics()).hasSize(1) - .extracting(PermissionTemplateCharacteristicDto::getPermission, PermissionTemplateCharacteristicDto::getWithProjectCreator) - .containsExactly(tuple(UserRole.USER, true)); - assertThat(result.getGroupPermissions()) - .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission) - .containsOnly( - tuple(group.getId(), group.getName(), UserRole.ADMIN), - tuple(0L, ANYONE, UserRole.USER) - ); - assertThat(result.getUserPermissions()).hasSize(1) - .extracting(PermissionTemplateUserDto::getUserId, PermissionTemplateUserDto::getUserLogin, PermissionTemplateUserDto::getPermission) - .containsExactly(tuple(user.getId(), user.getLogin(), UserRole.CODEVIEWER)); - } - - @Test - public void should_fail_on_unmatched_template() { - expectedException.expect(IllegalArgumentException.class); - - underTest.selectPermissionTemplateWithPermissions(db.getSession(), "unmatched"); - } - - @Test public void group_count_by_template_and_permission() { PermissionTemplateDto template1 = templateDb.insertTemplate(); PermissionTemplateDto template2 = templateDb.insertTemplate(); |