This is an automated email from the ASF dual-hosted git repository.
madhan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git
The following commit(s) were added to refs/heads/master by this push:
new b22d25890 RANGER-5212: roles lookup API updated to apply role name
filter for role-membership as well
b22d25890 is described below
commit b22d25890818aa75ddc049e433fc217296beef37
Author: Rakesh Gupta <[email protected]>
AuthorDate: Wed Dec 31 00:53:46 2025 +0530
RANGER-5212: roles lookup API updated to apply role name filter for
role-membership as well
---
.../java/org/apache/ranger/biz/RoleDBStore.java | 69 +++++++++++-----------
.../org/apache/ranger/biz/TestRoleDBStore.java | 21 ++++---
2 files changed, 45 insertions(+), 45 deletions(-)
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 d314f046b..c89609eb9 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<RangerRole> roles = new ArrayList<>();
- List<XXRole> xxRoles = roleService.searchResources(filter,
roleService.searchFields, roleService.sortFields, rangerRoleList);
+ List<RangerRole> 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<RangerRole> roles = new ArrayList<>();
+ public RangerRoleList getRolesForUser(SearchFilter filter, RangerRoleList
rangerRoleList) throws Exception {
+ if (filter == null) {
+ filter = new SearchFilter();
+ }
+
+ List<RangerRole> 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<XXRole> 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<RangerRole> 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<XXRole> 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<RangerRole> 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 5ff94d7cf..e6a0cdc31 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<XXRole> xxRoles = Collections.singletonList(xxRole);
- SearchFilter searchFilter = new SearchFilter();
+ XXRoleDao xxRoleDao = Mockito.mock(XXRoleDao.class);
+ XXRole xxRole = getTestRole();
+ List<XXRole> 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<XXRole> 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);