diff --git a/security-admin/src/main/java/org/apache/ranger/biz/RoleDBStore.java b/security-admin/src/main/java/org/apache/ranger/biz/RoleDBStore.java index f9296f699e..efc83118db 100644 --- a/security-admin/src/main/java/org/apache/ranger/biz/RoleDBStore.java +++ b/security-admin/src/main/java/org/apache/ranger/biz/RoleDBStore.java @@ -299,68 +299,45 @@ public void initStore() { } public RangerRoleList getRoles(SearchFilter filter, RangerRoleList rangerRoleList) throws Exception { - List roles = new ArrayList<>(); - List xxRoles = roleService.searchResources(filter, roleService.searchFields, roleService.sortFields, rangerRoleList); + List roles = getRoles(filter); - if (CollectionUtils.isNotEmpty(xxRoles)) { - for (XXRole xxRole : xxRoles) { - roles.add(roleService.read(xxRole.getId())); - } - } - - rangerRoleList.setRoleList(roles); + setPaginatedResult(roles, filter, rangerRoleList); return rangerRoleList; } - public RangerRoleList getRolesForUser(SearchFilter filter, RangerRoleList rangerRoleList) { - List roles = new ArrayList<>(); + public RangerRoleList getRolesForUser(SearchFilter filter, RangerRoleList rangerRoleList) throws Exception { + if (filter == null) { + filter = new SearchFilter(); + } + + List roles; UserSessionBase userSession = ContextUtil.getCurrentUserSession(); if (userSession != null && userSession.getUserRoleList().size() == 1 && userSession.getUserRoleList().contains(RangerConstants.ROLE_USER) && userSession.getLoginId() != null) { VXUser loggedInVXUser = xUserService.getXUserByUserName(userSession.getLoginId()); List xxRoles = daoMgr.getXXRole().findByUserId(loggedInVXUser.getId()); + roles = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(xxRoles)) { for (XXRole xxRole : xxRoles) { roles.add(roleService.read(xxRole.getId())); } } - if (predicateUtil != null && filter != null && !filter.isEmpty()) { + if (predicateUtil != null && !filter.isEmpty()) { List copy = new ArrayList<>(roles); predicateUtil.applyFilter(copy, filter); roles = copy; } - - int totalCount = roles.size(); - int startIndex = filter.getStartIndex(); - int pageSize = filter.getMaxRows(); - int toIndex = Math.min(startIndex + pageSize, totalCount); - - if (CollectionUtils.isNotEmpty(roles)) { - roles = roles.subList(startIndex, toIndex); - - rangerRoleList.setResultSize(roles.size()); - rangerRoleList.setPageSize(filter.getMaxRows()); - rangerRoleList.setSortBy(filter.getSortBy()); - rangerRoleList.setSortType(filter.getSortType()); - rangerRoleList.setStartIndex(filter.getStartIndex()); - rangerRoleList.setTotalCount(totalCount); - } } else { - List xxRoles = roleService.searchResources(filter, roleService.searchFields, roleService.sortFields, rangerRoleList); - - if (CollectionUtils.isNotEmpty(xxRoles)) { - for (XXRole xxRole : xxRoles) { - roles.add(roleService.read(xxRole.getId())); - } - } + roles = getRoles(filter); } - rangerRoleList.setRoleList(roles); + setPaginatedResult(roles, filter, rangerRoleList); return rangerRoleList; } @@ -500,6 +477,26 @@ private boolean ensureRoleNotInZone(String roleName) { return roleRefZoneCount < 1; } + private void setPaginatedResult(List roles, SearchFilter filter, RangerRoleList result) { + int totalCount = roles.size(); + int startIndex = filter.getStartIndex(); + int pageSize = filter.getMaxRows(); + int toIndex = Math.min(startIndex + pageSize, totalCount); + + if (CollectionUtils.isNotEmpty(roles)) { + roles = roles.subList(startIndex, toIndex); + + result.setResultSize(roles.size()); + result.setPageSize(filter.getMaxRows()); + result.setSortBy(filter.getSortBy()); + result.setSortType(filter.getSortType()); + result.setStartIndex(filter.getStartIndex()); + result.setTotalCount(totalCount); + } + + result.setRoleList(roles); + } + public static class RoleVersionUpdater implements Runnable { final RangerDaoManager daoManager; diff --git a/security-admin/src/test/java/org/apache/ranger/biz/TestRoleDBStore.java b/security-admin/src/test/java/org/apache/ranger/biz/TestRoleDBStore.java index 5ff94d7cf8..e6a0cdc318 100644 --- a/security-admin/src/test/java/org/apache/ranger/biz/TestRoleDBStore.java +++ b/security-admin/src/test/java/org/apache/ranger/biz/TestRoleDBStore.java @@ -138,16 +138,17 @@ public void testGetRoleByRoleId() throws Exception { @Test public void testGetRolesBySearchFilter() throws Exception { - RangerRole rangerRole = getRangerRole(); - RangerRoleList rangerRoleList = new RangerRoleList(Collections.singletonList(rangerRole)); - XXRole xxRole = getTestRole(); - List xxRoles = Collections.singletonList(xxRole); - SearchFilter searchFilter = new SearchFilter(); + XXRoleDao xxRoleDao = Mockito.mock(XXRoleDao.class); + XXRole xxRole = getTestRole(); + List xxRoles = Collections.singletonList(xxRole); + SearchFilter searchFilter = new SearchFilter(); + RangerRole rangerRole = getRangerRole(); - Mockito.when(roleService.searchResources(searchFilter, roleService.searchFields, roleService.sortFields, rangerRoleList)).thenReturn(xxRoles); + Mockito.when(daoMgr.getXXRole()).thenReturn(xxRoleDao); + Mockito.when(xxRoleDao.getAll()).thenReturn(xxRoles); Mockito.when(roleService.read(xxRole.getId())).thenReturn(rangerRole); - RangerRoleList rangerRoleListInDB = roleDBStore.getRoles(searchFilter, rangerRoleList); + RangerRoleList rangerRoleListInDB = roleDBStore.getRoles(searchFilter, new RangerRoleList()); Assertions.assertNotNull(rangerRoleListInDB); Assertions.assertEquals(1, rangerRoleListInDB.getList().size()); @@ -156,12 +157,14 @@ public void testGetRolesBySearchFilter() throws Exception { @Test public void testGetRolesForUser_WithoutUserSession() throws Exception { RangerRole rangerRole = getRangerRole(); - RangerRoleList rangerRoleList = new RangerRoleList(Collections.singletonList(rangerRole)); + RangerRoleList rangerRoleList = new RangerRoleList(); XXRole xxRole = getTestRole(); List xxRoles = Collections.singletonList(xxRole); SearchFilter searchFilter = new SearchFilter(); + XXRoleDao xxRoleDao = Mockito.mock(XXRoleDao.class); - Mockito.when(roleService.searchResources(searchFilter, roleService.searchFields, roleService.sortFields, rangerRoleList)).thenReturn(xxRoles); + Mockito.when(daoMgr.getXXRole()).thenReturn(xxRoleDao); + Mockito.when(xxRoleDao.getAll()).thenReturn(xxRoles); Mockito.when(roleService.read(xxRole.getId())).thenReturn(rangerRole); RangerContextHolder.setSecurityContext(null);