From: Simon Brandhof Date: Mon, 17 Oct 2016 13:11:35 +0000 (+0200) Subject: SONAR-8134 remove unused production code from RoleDao X-Git-Tag: 6.2-RC1~338 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=612f98e594d71e291690d5250c8f36be4c739df9;p=sonarqube.git SONAR-8134 remove unused production code from RoleDao --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStepTest.java index fd83a6f5682..218446d299d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStepTest.java @@ -19,6 +19,9 @@ */ package org.sonar.server.computation.task.projectanalysis.step; +import java.util.List; +import java.util.Map; +import org.elasticsearch.search.SearchHit; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -107,7 +110,8 @@ public class ApplyPermissionsStepTest extends BaseStepTest { dbSession.commit(); assertThat(dbClient.componentDao().selectOrFailByKey(dbSession, ROOT_KEY).getAuthorizationUpdatedAt()).isNotNull(); - assertThat(dbClient.roleDao().selectGroupPermissions(dbSession, DefaultGroups.ANYONE, projectDto.getId())).containsOnly(UserRole.USER); + assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, + dbTester.getDefaultOrganization().getUuid(), null, projectDto.getId())).containsOnly(UserRole.USER); authorizationIndexerTester.verifyProjectExistsWithPermission(ROOT_UUID, singletonList(DefaultGroups.ANYONE), emptyList()); } @@ -148,7 +152,8 @@ public class ApplyPermissionsStepTest extends BaseStepTest { dbSession.commit(); assertThat(dbClient.componentDao().selectOrFailByKey(dbSession, ROOT_KEY).getAuthorizationUpdatedAt()).isNotNull(); - assertThat(dbClient.roleDao().selectGroupPermissions(dbSession, DefaultGroups.ANYONE, viewDto.getId())).containsOnly(permission); + assertThat(dbClient.groupPermissionDao().selectProjectPermissionsOfGroup(dbSession, + dbTester.getDefaultOrganization().getUuid(), null, viewDto.getId())).containsOnly(permission); } @Test @@ -170,9 +175,12 @@ public class ApplyPermissionsStepTest extends BaseStepTest { } private void createDefaultPermissionTemplate(String permission) { - PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setName("Default")); - settings.setProperty("sonar.permission.template.default", permissionTemplateDto.getKee()); - dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplateDto.getId(), null, permission); + PermissionTemplateDto defaultTemplate = newPermissionTemplateDto() + .setOrganizationUuid(dbTester.getDefaultOrganization().getUuid()) + .setName("Default"); + dbClient.permissionTemplateDao().insert(dbSession, defaultTemplate); + settings.setProperty("sonar.permission.template.default", defaultTemplate.getKee()); + dbClient.permissionTemplateDao().insertGroupPermission(dbSession, defaultTemplate.getId(), null, permission); dbSession.commit(); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java index bb170254026..d7512cb7c11 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionDao.java @@ -21,7 +21,6 @@ package org.sonar.db.permission; import java.util.Collection; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; @@ -30,7 +29,6 @@ import org.sonar.db.DatabaseUtils; import org.sonar.db.DbSession; import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -61,22 +59,6 @@ public class UserPermissionDao implements Dao { .collect(Collectors.toList()); } - /** - * Shortcut over {@link #select(DbSession, PermissionQuery, Collection)} - * @param userLogin the non-null user login - * @param projectUuid if null, then return global permissions, else return permissions of user on this project - */ - public Set selectPermissionsByLogin(DbSession dbSession, String userLogin, @Nullable String projectUuid) { - PermissionQuery query = PermissionQuery.builder() - .withAtLeastOnePermission() - .setComponentUuid(projectUuid) - .build(); - return select(dbSession, query, asList(userLogin)).stream() - .map(ExtendedUserPermissionDto::getPermission) - .collect(Collectors.toSet()); - } - - /** * @see UserPermissionMapper#countUsersByQuery(PermissionQuery, Collection) */ diff --git a/sonar-db/src/main/java/org/sonar/db/user/RoleDao.java b/sonar-db/src/main/java/org/sonar/db/user/RoleDao.java index cfd6a2ea69c..766322ecd5c 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/RoleDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/RoleDao.java @@ -20,8 +20,6 @@ package org.sonar.db.user; import java.util.List; -import javax.annotation.Nullable; -import org.sonar.api.security.DefaultGroups; import org.sonar.db.Dao; import org.sonar.db.DbSession; @@ -35,28 +33,10 @@ public class RoleDao implements Dao { return mapper(dbSession).selectComponentIdsByPermissionAndUserId(permission, userId); } - /** - * @deprecated replaced by {@link org.sonar.db.permission.GroupPermissionDao#selectGroupPermissions(DbSession, long, Long)} - * and {@link org.sonar.db.permission.GroupPermissionDao#selectAnyonePermissions(DbSession, Long)} - */ - @Deprecated - public List selectGroupPermissions(DbSession session, String groupName, @Nullable Long resourceId) { - return session.getMapper(RoleMapper.class).selectGroupPermissions(groupName, resourceId, DefaultGroups.isAnyone(groupName)); - } - - public void deleteGroupRolesByGroupId(DbSession session, long groupId) { mapper(session).deleteGroupRolesByGroupId(groupId); } - /** - * @deprecated does not support organizations - */ - @Deprecated - public int countUserPermissions(DbSession session, String permission, @Nullable Long allGroupsExceptThisGroupId) { - return mapper(session).countUsersWithPermission(permission, allGroupsExceptThisGroupId); - } - private static RoleMapper mapper(DbSession session) { return session.getMapper(RoleMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/RoleMapper.java b/sonar-db/src/main/java/org/sonar/db/user/RoleMapper.java index eae65a9bf34..963aa4466e7 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/RoleMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/RoleMapper.java @@ -20,22 +20,12 @@ package org.sonar.db.user; import java.util.List; -import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; -/** - * @since 3.2 - */ public interface RoleMapper { List selectComponentIdsByPermissionAndUserId(@Param("permission") String permission, @Param("userId") long userId); - /** - * @return permissions from to a group - */ - List selectGroupPermissions(@Param("groupName") String groupName, @Nullable @Param("resourceId") Long resourceId, @Param("isAnyOneGroup") Boolean isAnyOneGroup); - void deleteGroupRolesByGroupId(long groupId); - int countUsersWithPermission(@Param("permission") String permission, @Nullable @Param("groupId") Long groupId); } diff --git a/sonar-db/src/main/resources/org/sonar/db/user/RoleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/RoleMapper.xml index 766ae4a6bf7..98d5c9cc520 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/RoleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/RoleMapper.xml @@ -23,56 +23,6 @@ ORDER by resource_id - - - - delete from group_roles where group_id=#{id} 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 5d7247fd90a..ab0436797c6 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 @@ -19,6 +19,7 @@ */ package org.sonar.db.permission; +import java.util.List; import javax.annotation.Nullable; import org.junit.Before; import org.junit.Rule; @@ -35,7 +36,6 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.template.PermissionTemplateDbTester; import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.db.user.GroupDto; -import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserDto; import static org.assertj.core.api.Assertions.assertThat; @@ -73,24 +73,32 @@ public class PermissionRepositoryTest { public void apply_permission_template() { dbTester.prepareDbUnit(getClass(), "should_apply_permission_template.xml"); - UserDto marius = dbTester.users().selectUserByLogin("marius").get(); - RoleDao roleDao = dbTester.getDbClient().roleDao(); - assertThat(roleDao.selectGroupPermissions(session, "sonar-administrators", PROJECT.getId())).isEmpty(); - assertThat(roleDao.selectGroupPermissions(session, "sonar-users", PROJECT.getId())).isEmpty(); - assertThat(roleDao.selectGroupPermissions(session, "Anyone", PROJECT.getId())).isEmpty(); - assertThat(dbTester.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(session, marius.getId(), PROJECT.getId())).isEmpty(); + assertThat(selectProjectPermissionsOfGroup("org1", 100L, PROJECT)).isEmpty(); + assertThat(selectProjectPermissionsOfGroup("org1", 101L, PROJECT)).isEmpty(); + assertThat(selectProjectPermissionsOfGroup("org1", null, PROJECT)).isEmpty(); + assertThat(selectProjectPermissionsOfUser(200L, PROJECT)).isEmpty(); 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"); - assertThat(roleDao.selectGroupPermissions(session, "Anyone", PROJECT.getId())).containsOnly("user", "codeviewer"); - assertThat(dbTester.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(session, marius.getId(), PROJECT.getId())).containsOnly("admin"); + assertThat(selectProjectPermissionsOfGroup("org1", 100L, PROJECT)).containsOnly("admin", "issueadmin"); + assertThat(selectProjectPermissionsOfGroup("org1", 101L, PROJECT)).containsOnly("user", "codeviewer"); + assertThat(selectProjectPermissionsOfGroup("org1", null, PROJECT)).containsOnly("user", "codeviewer"); + assertThat(selectProjectPermissionsOfUser(200L, PROJECT)).containsOnly("admin"); checkAuthorizationUpdatedAtIsUpdated(); } + private List selectProjectPermissionsOfGroup(String organizationUuid, @Nullable Long groupId, ComponentDto project) { + return dbTester.getDbClient().groupPermissionDao().selectProjectPermissionsOfGroup(session, + organizationUuid, groupId != null ? groupId : null, project.getId()); + } + + private List selectProjectPermissionsOfUser(long userId, ComponentDto project) { + return dbTester.getDbClient().userPermissionDao().selectProjectPermissionsOfUser(session, + userId, project.getId()); + } + @Test public void apply_default_permission_template_from_component_id() { dbTester.prepareDbUnit(getClass(), "apply_default_permission_template_by_component_id.xml"); diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index 8cfe4d3c5bc..4210dd4b5a0 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -226,29 +226,6 @@ public class UserPermissionDaoTest { assertThat(underTest.selectLogins(dbSession, query)).isEmpty(); } - @Test - public void selectPermissionsByLogin() { - addGlobalPermissionOnDefaultOrganization(SYSTEM_ADMIN, user1); - addProjectPermissionOnDefaultOrganization(USER, user1, project1); - addProjectPermissionOnDefaultOrganization(USER, user2, project1); - addProjectPermissionOnDefaultOrganization(ISSUE_ADMIN, user2, project1); - addProjectPermissionOnDefaultOrganization(ISSUE_ADMIN, user2, project2); - - // user1 has one global permission and user2 has no global permissions - assertThat(underTest.selectPermissionsByLogin(dbSession, user1.getLogin(), null)).hasSize(1); - assertThat(underTest.selectPermissionsByLogin(dbSession, user2.getLogin(), null)).hasSize(0); - - // user1 has one permission on project1, user2 has 2 - assertThat(underTest.selectPermissionsByLogin(dbSession, user1.getLogin(), project1.uuid())).hasSize(1); - assertThat(underTest.selectPermissionsByLogin(dbSession, user2.getLogin(), project1.uuid())).hasSize(2); - - // nobody has permissions on a project that does not exist! - assertThat(underTest.selectPermissionsByLogin(dbSession, user1.getLogin(), "missing")).hasSize(0); - - // users who do not exist don't have permissions! - assertThat(underTest.selectPermissionsByLogin(dbSession, "missing", null)).hasSize(0); - } - @Test public void deleteGlobalPermission() { addGlobalPermissionOnDefaultOrganization("perm1", user1); diff --git a/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java index 08a880b03c6..52cfd71a593 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/RoleDaoTest.java @@ -23,7 +23,6 @@ import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.security.DefaultGroups; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; @@ -94,28 +93,6 @@ public class RoleDaoTest { assertThat(result).containsExactly(project1.getId(), project2.getId()); } - @Test - public void retrieve_global_group_permissions() { - db.prepareDbUnit(getClass(), "globalGroupPermissions.xml"); - - assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-administrators", null)).containsOnly(GlobalPermissions.SYSTEM_ADMIN, - GlobalPermissions.QUALITY_PROFILE_ADMIN, - GlobalPermissions.QUALITY_GATE_ADMIN); - assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-users", null)).containsOnly(GlobalPermissions.QUALITY_GATE_ADMIN); - assertThat(underTest.selectGroupPermissions(db.getSession(), DefaultGroups.ANYONE, null)).containsOnly(GlobalPermissions.PROVISIONING, - GlobalPermissions.SCAN_EXECUTION); - assertThat(underTest.selectGroupPermissions(db.getSession(), "anyone", null)).containsOnly(GlobalPermissions.PROVISIONING, GlobalPermissions.SCAN_EXECUTION); - assertThat(underTest.selectGroupPermissions(db.getSession(), "AnYoNe", null)).containsOnly(GlobalPermissions.PROVISIONING, GlobalPermissions.SCAN_EXECUTION); - } - - @Test - public void retrieve_resource_group_permissions() { - db.prepareDbUnit(getClass(), "resourceGroupPermissions.xml"); - - assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-administrators", 1L)).containsOnly(UserRole.ADMIN, UserRole.CODEVIEWER); - assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-users", 1L)).containsOnly(UserRole.CODEVIEWER); - } - @Test public void delete_all_group_permissions_by_group_id() { db.prepareDbUnit(getClass(), "deleteGroupPermissionsByGroupId.xml"); @@ -125,40 +102,4 @@ public class RoleDaoTest { db.assertDbUnit(getClass(), "deleteGroupPermissionsByGroupId-result.xml", "group_roles"); } - - @Test - public void countUserPermissions() { - db.users().insertProjectPermissionOnUser(user1, GlobalPermissions.SYSTEM_ADMIN, project1); - db.users().insertPermissionOnUser(user1, GlobalPermissions.SYSTEM_ADMIN); - db.users().insertPermissionOnUser(user1, GlobalPermissions.SCAN_EXECUTION); - - int result = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); - - assertThat(result).isEqualTo(1); - } - - @Test - public void countUserPermissions_counts_users_with_one_permission_when_the_last_one_is_in_a_group() { - GroupDto group1 = db.users().insertGroup(newGroupDto()); - db.users().insertMember(group1, user1); - db.users().insertPermissionOnGroup(group1, GlobalPermissions.SYSTEM_ADMIN); - - int resultWithoutExcludingGroup = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); - assertThat(resultWithoutExcludingGroup).isEqualTo(1); - - int resultWithGroupExclusion = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, group1.getId()); - assertThat(resultWithGroupExclusion).isEqualTo(0); - } - - @Test - public void countUserPermissions_counts_user_twice_when_both_user_and_group_permission() { - GroupDto group1 = db.users().insertGroup(newGroupDto()); - db.users().insertMember(group1, user1); - db.users().insertPermissionOnGroup(group1, GlobalPermissions.SYSTEM_ADMIN); - db.users().insertPermissionOnUser(user1, GlobalPermissions.SYSTEM_ADMIN); - - int result = underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null); - - assertThat(result).isEqualTo(2); - } } diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java index 35eca3f53c1..dc1114eef2f 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -407,7 +407,7 @@ public class UserDaoTest { MeasureFilterDto measureFilter = insertMeasureFilter(user, false); MeasureFilterFavouriteDto measureFilterFavourite = insertMeasureFilterFavourite(measureFilter, user); PropertyDto property = insertProperty(user); - insertUserPermission(user); + db.users().insertPermissionOnUser(user, "perm"); insertUserGroup(user); UserDto otherUser = newActiveUser(); @@ -437,7 +437,7 @@ public class UserDaoTest { assertThat(dbClient.measureFilterDao().selectById(session, measureFilter.getId())).isNull(); assertThat(dbClient.measureFilterFavouriteDao().selectById(session, measureFilterFavourite.getId())).isNull(); assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setKey(property.getKey()).build(), session)).isEmpty(); - assertThat(dbClient.userPermissionDao().selectPermissionsByLogin(session, user.getLogin(), null)).isEmpty(); + assertThat(dbClient.userPermissionDao().selectGlobalPermissionsOfUser(session, user.getId(), db.getDefaultOrganization().getUuid())).isEmpty(); assertThat(dbClient.groupMembershipDao().countGroups(session, builder().login(user.getLogin()).membership(IN).build(), user.getId())).isZero(); } @@ -836,13 +836,6 @@ public class UserDaoTest { return dto; } - private org.sonar.db.permission.UserPermissionDto insertUserPermission(UserDto user) { - String permission = randomAlphanumeric(64); - UserPermissionDto dto = new UserPermissionDto(db.getDefaultOrganization().getUuid(), permission, user.getId(), null); - dbClient.userPermissionDao().insert(session, dto); - return dto; - } - private UserGroupDto insertUserGroup(UserDto user) { GroupDto group = newGroupDto().setName(randomAlphanumeric(30)); dbClient.groupDao().insert(session, group); diff --git a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions-result.xml b/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions-result.xml deleted file mode 100644 index cdf185a15cd..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions-result.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions.xml b/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions.xml deleted file mode 100644 index 570c81e0ea5..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/globalGroupPermissions.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions-result.xml b/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions-result.xml deleted file mode 100644 index 3c41ea646ab..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions-result.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - - - - diff --git a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions.xml b/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions.xml deleted file mode 100644 index 569141d25df..00000000000 --- a/sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/resourceGroupPermissions.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - -