RoleBasedUserSearchUtil extends BaseLoader { public void validateUserAndFetchUserList() { userLoginId = userLoginId.toLowerCase(); XXPortalUser xxPortalUser = daoMgr.getXXPortalUser().findByLoginId(userLoginId); Boolean isUserAuthorized = false; if (xxPortalUser != null) { String dbPassword = xxPortalUser.getPassword(); String currentEncryptedPassword = null; try { currentEncryptedPassword = userMgr.encrypt(userLoginId,currentPassword); if (currentEncryptedPassword != null && currentEncryptedPassword.equals(dbPassword)) { VXUser vxUser = xUserService.getXUserByUserName(xxPortalUser.getLoginId()); if (vxUser != null) { List<String> existingRole = (List<String>) vxUser.getUserRoleList(); List<String> permissionList = daoMgr.getXXModuleDef().findAccessibleModulesByUserId(xxPortalUser.getId(), vxUser.getId()); if (permissionList != null && permissionList.contains(RangerConstants.MODULE_USER_GROUPS) && !CollectionUtils.isEmpty(existingRole) && !StringUtils.isBlank(existingRole.get(0))) { List<String> userRoleList = new ArrayList<String>(); if (existingRole.get(0).equalsIgnoreCase(RangerConstants.ROLE_USER)) { userRoleList.add(RangerConstants.ROLE_USER); if (checkRole) { getUsersBasedOnRole(userRoleList); } else if (existingRole.get(0).equalsIgnoreCase(userRole) || userRole.equalsIgnoreCase(RangerConstants.ROLE_USER)) { getUsersBasedOnRole(userRoleList); } else { isUserAuthorized = true; } } else if (existingRole.get(0).equalsIgnoreCase(RangerConstants.ROLE_SYS_ADMIN) || existingRole.get(0).equalsIgnoreCase(RangerConstants.ROLE_ADMIN_AUDITOR)) { if (checkRole) { userRoleList.add(RangerConstants.ROLE_SYS_ADMIN); userRoleList.add(RangerConstants.ROLE_ADMIN_AUDITOR); userRoleList.add(RangerConstants.ROLE_USER); getUsersBasedOnRole(userRoleList); } else if (existingRole.get(0).equalsIgnoreCase(userRole) || userRole.equalsIgnoreCase(RangerConstants.ROLE_USER) || userRole.equalsIgnoreCase(RangerConstants.ROLE_ADMIN_AUDITOR) || userRole.equalsIgnoreCase(RangerConstants.ROLE_SYS_ADMIN)) { userRoleList.add(userRole); getUsersBasedOnRole(userRoleList); }else { isUserAuthorized = true; } } else if (existingRole.get(0).equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN) || existingRole.get(0).equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN_AUDITOR) || userRole.equalsIgnoreCase(RangerConstants.ROLE_USER)) { if (checkRole) { userRoleList.add(RangerConstants.ROLE_KEY_ADMIN); userRoleList.add(RangerConstants.ROLE_KEY_ADMIN_AUDITOR); userRoleList.add(RangerConstants.ROLE_USER); getUsersBasedOnRole(userRoleList); } else if (existingRole.get(0).equalsIgnoreCase(userRole) || userRole.equalsIgnoreCase(RangerConstants.ROLE_USER) || userRole.equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN) || userRole.equalsIgnoreCase(RangerConstants.ROLE_KEY_ADMIN_AUDITOR)) { userRoleList.add(userRole); getUsersBasedOnRole(userRoleList); } else { isUserAuthorized = true; } } if (isUserAuthorized == true) { System.out.println("user is not authorized to fetch this list"); logger.error("user is not authorized to fetch this list"); System.exit(1); } } else { System.out.println("user permission denied"); logger.error("user permission denied"); System.exit(1); } } } else { System.out.println("Invalid user password"); logger.error("Invalid user password"); System.exit(1); } } catch (Exception e) { logger.error("Getting User's List with the mentioned role failure. Detail: \n",e); System.exit(1); } } else { System.out.println("User does not exist in DB!!"); logger.error("User does not exist in DB"); System.exit(1); } }  static void main(String[] args); @Override void init(); @Override void printStats(); @Override void execLoad(); void getUsersBasedOnRole(List<String> userRoleList); void validateUserAndFetchUserList(); static Boolean checkRole; static String userLoginId; static String currentPassword; static String userRole; }
@Test public void TestValidateUserAndFetchUserList() { List<String> permissionList = new ArrayList<String>(); permissionList.add(RangerConstants.MODULE_USER_GROUPS); String currentEncryptedPassword = "testpassword"; XXPortalUser xxPortalUser = new XXPortalUser(); xxPortalUser.setId(1L); xxPortalUser.setLoginId("testUser"); xxPortalUser.setPassword("testpassword"); xxPortalUser.setFirstName("testUser"); VXUser vxUser = new VXUser(); vxUser.setId(1L); VXUserList vXUserList = new VXUserList(); List<VXUser> vXUsers = new ArrayList<VXUser>(); vXUsers.add(vxUser); vXUserList.setVXUsers(vXUsers ); List<String> userRoleList = new ArrayList<String>(); userRoleList.add("ROLE_SYS_ADMIN"); List<XXPortalUser> listXXPortalUser = new ArrayList<XXPortalUser>(); listXXPortalUser.add(xxPortalUser); vxUser.setUserRoleList(userRoleList); XXModuleDefDao xXModuleDefDao = Mockito.mock(XXModuleDefDao.class); Mockito.when(daoMgr.getXXPortalUser()).thenReturn(xXPortalUserDao); Mockito.when(xXPortalUserDao.findByLoginId(Mockito.anyString())).thenReturn(xxPortalUser); Mockito.when(xUserService.getXUserByUserName(xxPortalUser.getLoginId())).thenReturn(vxUser); Mockito.when(daoMgr.getXXModuleDef()).thenReturn(xXModuleDefDao); Mockito.when(xXModuleDefDao.findAccessibleModulesByUserId(Mockito.anyLong(), Mockito.anyLong())).thenReturn(permissionList); Mockito.when(userMgr.encrypt(Mockito.anyString(),Mockito.anyString())).thenReturn(currentEncryptedPassword); Mockito.when(xXPortalUserDao.findByRole(Mockito.anyString())).thenReturn(listXXPortalUser); roleBasedUserSearchUtil.validateUserAndFetchUserList(); Mockito.verify(xXPortalUserDao).findByLoginId(Mockito.anyString()); Mockito.verify(xUserService).getXUserByUserName(xxPortalUser.getLoginId()); Mockito.verify(xXModuleDefDao).findAccessibleModulesByUserId(Mockito.anyLong(), Mockito.anyLong()); Mockito.verify(userMgr).encrypt(Mockito.anyString(),Mockito.anyString()); Mockito.verify(xXPortalUserDao, Mockito.atLeast(2)).findByRole(Mockito.anyString()); }