From 0e55e8b6c465cf43959cc02c9b2c0b949829db19 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 13 Oct 2016 07:41:44 +0200 Subject: [PATCH] SONAR-8134 remove RoleDao#countComponentPermissions() replaced by GroupPermissionDao#hasRootComponentPermissions() and UserPermissionDao#hasRootComponentPermissions() --- .../step/ApplyPermissionsStep.java | 19 ++--- .../db/permission/GroupPermissionDao.java | 8 +++ .../db/permission/GroupPermissionMapper.java | 2 + .../db/permission/UserPermissionDao.java | 7 ++ .../db/permission/UserPermissionMapper.java | 2 + .../main/java/org/sonar/db/user/RoleDao.java | 11 --- .../java/org/sonar/db/user/RoleMapper.java | 4 -- .../db/permission/GroupPermissionMapper.xml | 6 ++ .../db/permission/UserPermissionMapper.xml | 6 ++ .../org/sonar/db/user/RoleMapper.xml | 10 --- .../db/permission/GroupPermissionDaoTest.java | 27 +++++++ .../db/permission/UserPermissionDaoTest.java | 71 +++++++++++-------- .../java/org/sonar/db/user/RoleDaoTest.java | 7 -- .../org/sonar/db/user/RoleMapperTest.java | 43 ----------- .../should_count_component_permissions.xml | 26 ------- .../db/user/RoleMapperTest/countRoles.xml | 43 ----------- 16 files changed, 110 insertions(+), 182 deletions(-) delete mode 100644 sonar-db/src/test/java/org/sonar/db/user/RoleMapperTest.java delete mode 100644 sonar-db/src/test/resources/org/sonar/db/user/RoleDaoTest/should_count_component_permissions.xml delete mode 100644 sonar-db/src/test/resources/org/sonar/db/user/RoleMapperTest/countRoles.xml diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java index f038f98488c..f3229145c81 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java @@ -21,7 +21,6 @@ package org.sonar.server.computation.task.projectanalysis.step; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.MyBatis; import org.sonar.db.permission.PermissionRepository; import org.sonar.server.computation.task.projectanalysis.component.Component; import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit; @@ -48,7 +47,7 @@ public class ApplyPermissionsStep implements ComputationStep { private final TreeRootHolder treeRootHolder; public ApplyPermissionsStep(DbClient dbClient, DbIdsRepository dbIdsRepository, IssueAuthorizationIndexer indexer, PermissionRepository permissionRepository, - TreeRootHolder treeRootHolder) { + TreeRootHolder treeRootHolder) { this.dbClient = dbClient; this.dbIdsRepository = dbIdsRepository; this.indexer = indexer; @@ -73,19 +72,21 @@ public class ApplyPermissionsStep implements ComputationStep { } private void execute(Component project) { - DbSession session = dbClient.openSession(false); - try { + try (DbSession dbSession = dbClient.openSession(false)) { long projectId = dbIdsRepository.getComponentId(project); - if (dbClient.roleDao().countComponentPermissions(session, projectId) == 0) { - permissionRepository.applyDefaultPermissionTemplate(session, projectId); - session.commit(); + if (hasNoPermissions(dbSession, projectId)) { + permissionRepository.applyDefaultPermissionTemplate(dbSession, projectId); + dbSession.commit(); indexer.index(); } - } finally { - MyBatis.closeQuietly(session); } } + private boolean hasNoPermissions(DbSession dbSession, long projectId) { + return !dbClient.groupPermissionDao().hasRootComponentPermissions(dbSession, projectId) && + !dbClient.userPermissionDao().hasRootComponentPermissions(dbSession, projectId); + } + @Override public String getDescription() { return "Apply permissions"; diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java index c1685e39776..53e579fa6c1 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java @@ -109,6 +109,14 @@ public class GroupPermissionDao implements Dao { return session.getMapper(GroupPermissionMapper.class).selectAnyonePermissions(projectId); } + /** + * @return {@code true} if the project has at least one permission defined, whatever it is + * on a group or on "anyone", else returns {@code false} + */ + public boolean hasRootComponentPermissions(DbSession dbSession, long rootComponentId) { + return mapper(dbSession).countRowsByRootComponentId(rootComponentId) > 0; + } + public void insert(DbSession dbSession, GroupPermissionDto dto) { mapper(dbSession).insert(dto); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java index 5a8138776c9..6c3978bc10e 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java @@ -64,4 +64,6 @@ public interface GroupPermissionMapper { void delete(@Param("permission") String permission, @Param("organizationUuid") String organizationUuid, @Nullable @Param("groupId") Long groupId, @Nullable @Param("rootComponentId") Long rootComponentId); + + int countRowsByRootComponentId(@Param("rootComponentId") long rootComponentId); } 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 16a21e2e6c0..648ea0afd32 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 @@ -94,6 +94,13 @@ public class UserPermissionDao implements Dao { return executeLargeInputs(projectIds, mapper(dbSession)::countUsersByProjectPermission); } + /** + * @return {@code true} if the project has at least one user permission defined, else returns {@code false} + */ + public boolean hasRootComponentPermissions(DbSession dbSession, long rootComponentId) { + return mapper(dbSession).countRowsByRootComponentId(rootComponentId) > 0; + } + public void insert(DbSession dbSession, UserPermissionDto dto) { mapper(dbSession).insert(dto); } diff --git a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java index 547db6cec9e..5da6ff35ff4 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/UserPermissionMapper.java @@ -56,4 +56,6 @@ public interface UserPermissionMapper { * Delete permissions by user and/or by project. In both cases scope can be restricted to a specified permission */ void delete(@Nullable @Param("login") String login, @Nullable @Param("projectUuid") String projectUuid, @Nullable @Param("permission") String permission); + + int countRowsByRootComponentId(@Param("rootComponentId") long projectId); } 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 2eb62c0efc6..a09bba9ec33 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 @@ -44,22 +44,11 @@ public class RoleDao implements Dao { return session.getMapper(RoleMapper.class).selectGroupPermissions(groupName, resourceId, DefaultGroups.isAnyone(groupName)); } - private static int countResourceGroupRoles(DbSession session, Long resourceId) { - return mapper(session).countResourceGroupRoles(resourceId); - } - - private static int countResourceUserRoles(DbSession session, long resourceId) { - return mapper(session).countResourceUserRoles(resourceId); - } public void deleteGroupRolesByGroupId(DbSession session, long groupId) { mapper(session).deleteGroupRolesByGroupId(groupId); } - public int countComponentPermissions(DbSession session, Long componentId) { - return countResourceGroupRoles(session, componentId) + countResourceUserRoles(session, componentId); - } - public int countUserPermissions(DbSession session, String permission, @Nullable Long allGroupsExceptThisGroupId) { return mapper(session).countUsersWithPermission(permission, allGroupsExceptThisGroupId); } 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 23d154d5cbe..eae65a9bf34 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 @@ -35,10 +35,6 @@ public interface RoleMapper { */ List selectGroupPermissions(@Param("groupName") String groupName, @Nullable @Param("resourceId") Long resourceId, @Param("isAnyOneGroup") Boolean isAnyOneGroup); - int countResourceGroupRoles(Long resourceId); - - int countResourceUserRoles(long resourceId); - 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/permission/GroupPermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml index dff26a62a71..e9c9bbb780e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml @@ -150,6 +150,12 @@ + + insert into group_roles ( organization_uuid, diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml index b9163e08ad0..a134e797cb6 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml @@ -60,6 +60,12 @@ group by ur.resource_id, ur.role + + insert into user_roles ( organization_uuid, 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 0ac0d435512..766ae4a6bf7 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 @@ -49,16 +49,6 @@ - - - -