XUserMgr extends XUserMgrBase { public VXUser updateXUser(VXUser vXUser) { if (vXUser == null || vXUser.getName() == null || "null".equalsIgnoreCase(vXUser.getName()) || vXUser.getName().trim().isEmpty()) { throw restErrorUtil.createRESTException("Please provide a valid " + "username.", MessageEnums.INVALID_INPUT_DATA); } checkAccess(vXUser.getName()); xaBizUtil.blockAuditorRoleUser(); VXPortalUser oldUserProfile = userMgr.getUserProfileByLoginId(vXUser .getName()); if (oldUserProfile == null) { throw restErrorUtil.createRESTException( "user " + vXUser.getName() + " does not exist.", MessageEnums.INVALID_INPUT_DATA); } VXPortalUser vXPortalUser = new VXPortalUser(); if (oldUserProfile != null && oldUserProfile.getId() != null) { vXPortalUser.setId(oldUserProfile.getId()); } vXPortalUser.setFirstName(vXUser.getFirstName()); if("null".equalsIgnoreCase(vXPortalUser.getFirstName())){ vXPortalUser.setFirstName(""); } vXPortalUser.setLastName(vXUser.getLastName()); if("null".equalsIgnoreCase(vXPortalUser.getLastName())){ vXPortalUser.setLastName(""); } vXPortalUser.setEmailAddress(vXUser.getEmailAddress()); vXPortalUser.setLoginId(vXUser.getName()); vXPortalUser.setStatus(vXUser.getStatus()); vXPortalUser.setUserRoleList(vXUser.getUserRoleList()); if (vXPortalUser.getFirstName() != null && vXPortalUser.getLastName() != null && !vXPortalUser.getFirstName().trim().isEmpty() && !vXPortalUser.getLastName().trim().isEmpty()) { vXPortalUser.setPublicScreenName(vXPortalUser.getFirstName() + " " + vXPortalUser.getLastName()); } else { vXPortalUser.setPublicScreenName(vXUser.getName()); } vXPortalUser.setUserSource(oldUserProfile.getUserSource()); String hiddenPasswordString = PropertiesUtil.getProperty("ranger.password.hidden", "*****"); String password = vXUser.getPassword(); if (oldUserProfile != null && password != null && password.equals(hiddenPasswordString)) { vXPortalUser.setPassword(oldUserProfile.getPassword()); } else if(oldUserProfile != null && oldUserProfile.getUserSource() == RangerCommonEnums.USER_EXTERNAL && password != null){ vXPortalUser.setPassword(oldUserProfile.getPassword()); logger.debug("User is trrying to change external user password which we are not allowing it to change"); } else if(password != null){ validatePassword(vXUser); vXPortalUser.setPassword(password); } Collection<Long> groupIdList = vXUser.getGroupIdList(); XXPortalUser xXPortalUser = new XXPortalUser(); xXPortalUser = userMgr.updateUserWithPass(vXPortalUser); Collection<String> roleListUpdatedProfile =new ArrayList<String>(); if (oldUserProfile != null && oldUserProfile.getId() != null) { if(vXUser!=null && vXUser.getUserRoleList()!=null){ Collection<String> roleListOldProfile = oldUserProfile.getUserRoleList(); Collection<String> roleListNewProfile = vXUser.getUserRoleList(); if(roleListNewProfile!=null && roleListOldProfile!=null){ for (String role : roleListNewProfile) { if(role!=null && !roleListOldProfile.contains(role)){ roleListUpdatedProfile.add(role); } } } } } if(roleListUpdatedProfile!=null && roleListUpdatedProfile.size()>0){ vXPortalUser.setUserRoleList(roleListUpdatedProfile); List<XXUserPermission> xuserPermissionList = daoManager .getXXUserPermission() .findByUserPermissionId(vXPortalUser.getId()); if (xuserPermissionList!=null && xuserPermissionList.size()>0){ for (XXUserPermission xXUserPermission : xuserPermissionList) { if (xXUserPermission != null) { try { xUserPermissionService.deleteResource(xXUserPermission.getId()); } catch (Exception e) { logger.error(e.getMessage()); } } } } assignPermissionToUser(vXPortalUser,true); } Collection<String> roleList = new ArrayList<String>(); if (xXPortalUser != null) { roleList = userMgr.getRolesForUser(xXPortalUser); } if (roleList == null || roleList.size() == 0) { roleList = new ArrayList<String>(); roleList.add(RangerConstants.ROLE_USER); } vXUser = xUserService.updateResource(vXUser); vXUser.setUserRoleList(roleList); if (oldUserProfile != null) { if (oldUserProfile.getUserSource() == RangerCommonEnums.USER_APP) { vXUser.setPassword(password); } else if (oldUserProfile.getUserSource() == RangerCommonEnums.USER_EXTERNAL) { vXUser.setPassword(oldUserProfile.getPassword()); } } List<XXTrxLog> trxLogList = xUserService.getTransactionLog(vXUser, oldUserProfile, "update"); vXUser.setPassword(hiddenPasswordString); Long userId = vXUser.getId(); List<Long> groupUsersToRemove = new ArrayList<Long>(); trxLogList.addAll(createOrDelGrpUserWithUpdatedGrpId(vXUser, groupIdList,userId, groupUsersToRemove)); xaBizUtil.createTrxLog(trxLogList); return vXUser; }  VXUser getXUserByUserName(String userName); VXGroup getGroupByGroupName(String groupName); VXUser createXUser(VXUser vXUser); void assignPermissionToUser(VXPortalUser vXPortalUser, boolean isCreate); void createOrUpdateUserPermisson(VXPortalUser portalUser, Long moduleId, boolean isCreate); HashMap<String, Long> getAllModuleNameAndIdMap(); VXUser updateXUser(VXUser vXUser); VXUserGroupInfo createXUserGroupFromMap(VXUserGroupInfo vXUserGroupInfo); @Transactional(readOnly = false, propagation = Propagation.REQUIRED) VXGroupUserInfo createXGroupUserFromMap(
			VXGroupUserInfo vXGroupUserInfo); VXGroupUserInfo getXGroupUserFromMap(
			String groupName); VXUser createXUserWithOutLogin(VXUser vXUser); VXUser createExternalUser(String userName); VXGroup createXGroup(VXGroup vXGroup); VXGroup createXGroupWithoutLogin(VXGroup vXGroup); VXGroupUser createXGroupUser(VXGroupUser vXGroupUser); VXUser getXUser(Long id); VXGroupUser getXGroupUser(Long id); VXGroup getXGroup(Long id); void deleteXGroupAndXUser(String groupName, String userName); VXGroupList getXUserGroups(Long xUserId); Set<String> getGroupsForUser(String userName); VXUserList getXGroupUsers(SearchCriteria searchCriteria); @Override VXGroup updateXGroup(VXGroup vXGroup); VXGroupUser updateXGroupUser(VXGroupUser vXGroupUser); void deleteXGroupUser(Long id, boolean force); VXGroupGroup createXGroupGroup(VXGroupGroup vXGroupGroup); VXGroupGroup updateXGroupGroup(VXGroupGroup vXGroupGroup); void deleteXGroupGroup(Long id, boolean force); void deleteXPermMap(Long id, boolean force); VXLong getXPermMapSearchCount(SearchCriteria searchCriteria); void deleteXAuditMap(Long id, boolean force); VXLong getXAuditMapSearchCount(SearchCriteria searchCriteria); void modifyUserVisibility(HashMap<Long, Integer> visibilityMap); void modifyGroupsVisibility(HashMap<Long, Integer> groupVisibilityMap); VXModuleDef createXModuleDefPermission(VXModuleDef vXModuleDef); VXModuleDef getXModuleDefPermission(Long id); VXModuleDef updateXModuleDefPermission(VXModuleDef vXModuleDef); void deleteXModuleDefPermission(Long id, boolean force); VXUserPermission createXUserPermission(VXUserPermission vXUserPermission); VXUserPermission getXUserPermission(Long id); VXUserPermission updateXUserPermission(VXUserPermission vXUserPermission); void deleteXUserPermission(Long id, boolean force); VXGroupPermission createXGroupPermission(VXGroupPermission vXGroupPermission); VXGroupPermission getXGroupPermission(Long id); VXGroupPermission updateXGroupPermission(VXGroupPermission vXGroupPermission); void deleteXGroupPermission(Long id, boolean force); void modifyUserActiveStatus(HashMap<Long, Integer> statusMap); void checkAdminAccess(); void checkAccess(String loginID); VXPermMapList searchXPermMaps(SearchCriteria searchCriteria); VXAuditMapList searchXAuditMaps(SearchCriteria searchCriteria); void checkAccessRoles(List<String> stringRolesList); VXStringList setUserRolesByExternalID(Long userId, List<VXString> vStringRolesList); VXStringList setUserRolesByName(String userName, List<VXString> vStringRolesList); List<String> updateUserRoleAssignments(VXUsersGroupRoleAssignments ugRoleAssignments); VXStringList getUserRolesByExternalID(Long userId); VXStringList getUserRolesByName(String userName); void updateUserRolesPermissions(VXPortalUser oldUserProfile,List<String> roleListNewProfile); VXStringList getStringListFromUserRoleList(
			List<XXPortalUserRole> listXXPortalUserRole); boolean hasAccess(String loginID); VXUser getMaskedVXUser(VXUser vXUser); VXGroup getMaskedVXGroup(VXGroup vXGroup); @Override VXUserList searchXUsers(SearchCriteria searchCriteria); @Override VXGroupList searchXGroups(SearchCriteria searchCriteria); VXGroupList lookupXGroups(SearchCriteria searchCriteria); Collection<String> getMaskedCollection(Collection<String> listunMasked); boolean hasAccessToModule(String moduleName); void deleteXGroup(Long id, boolean force); synchronized void deleteXUser(Long id, boolean force); void restrictSelfAccountDeletion(String loginID); @Transactional(readOnly = false, propagation = Propagation.REQUIRED) VXUser createServiceConfigUser(String userName); void denySelfRoleChange(String userName); @Transactional(readOnly = false, propagation = Propagation.REQUIRED) synchronized VXUgsyncAuditInfo postUserGroupAuditInfo(
			VXUgsyncAuditInfo vxUgsyncAuditInfo); Long getUserStoreVersion(); Set<UserInfo> getUsers(); Set<GroupInfo> getGroups(); Map<String, Set<String>> getUserGroups(); RangerUserStore getRangerUserStore(Long lastKnownUserStoreVersion);  }
@Test public void test05UpdateXUser() { setup(); XXUserPermissionDao xUserPermissionDao = Mockito.mock(XXUserPermissionDao.class); Collection<String> existingRoleList = new ArrayList<String>(); existingRoleList.add(RangerConstants.ROLE_USER); Collection<String> reqRoleList = new ArrayList<String>(); reqRoleList.add(RangerConstants.ROLE_SYS_ADMIN); Collection<Long> groupIdList = new ArrayList<Long>(); groupIdList.add(userId); VXUser vxUser = vxUser(); vxUser.setUserRoleList(reqRoleList); vxUser.setUserSource(RangerCommonEnums.USER_EXTERNAL); vxUser.setGroupIdList(groupIdList); vxUser.setFirstName("user12"); vxUser.setLastName("test12"); Mockito.when(xUserService.updateResource(vxUser)).thenReturn(vxUser); XXModuleDefDao xModuleDefDao = Mockito.mock(XXModuleDefDao.class); VXPortalUser vXPortalUser = userProfile(); vXPortalUser.setUserRoleList(existingRoleList); Mockito.when(userMgr.getUserProfileByLoginId(vxUser.getName())).thenReturn(vXPortalUser); Mockito.when(daoManager.getXXUserPermission()).thenReturn(xUserPermissionDao); List<XXUserPermission> xUserPermissionsList = new ArrayList<XXUserPermission>(); XXUserPermission xUserPermissionObj = new XXUserPermission(); xUserPermissionObj.setAddedByUserId(userId); xUserPermissionObj.setCreateTime(new Date()); xUserPermissionObj.setId(userId); xUserPermissionObj.setIsAllowed(1); xUserPermissionObj.setModuleId(1L); xUserPermissionObj.setUpdatedByUserId(userId); xUserPermissionObj.setUpdateTime(new Date()); xUserPermissionObj.setUserId(userId); xUserPermissionsList.add(xUserPermissionObj); List<XXModuleDef> xXModuleDefs = xxModuleDefs(); Mockito.when(xUserPermissionDao.findByUserPermissionId(vXPortalUser.getId())).thenReturn(xUserPermissionsList); Mockito.when(daoManager.getXXModuleDef()).thenReturn(xModuleDefDao); Mockito.when(xModuleDefDao.getAll()).thenReturn(xXModuleDefs); XXUser xXUser = xxUser(vxUser); XXUserDao xxUserDao = Mockito.mock(XXUserDao.class); Mockito.when(daoManager.getXXUser()).thenReturn(xxUserDao); Mockito.when(xxUserDao.findByPortalUserId(vXPortalUser.getId())).thenReturn(xXUser); VXGroupUserList vxGroupUserList = vxGroupUserList(); Mockito.when(xGroupUserService.searchXGroupUsers((SearchCriteria) Mockito.any())).thenReturn(vxGroupUserList); UserSessionBase userSession = Mockito.mock(UserSessionBase.class); Set<UserSessionBase> userSessions = new HashSet<UserSessionBase>(); userSessions.add(userSession); VXUserPermission vXUserPermission = vxUserPermission(); Mockito.when(xUserPermissionService.createResource((VXUserPermission) Mockito.any())).thenReturn(vXUserPermission); Mockito.when(sessionMgr.getActiveUserSessionsForPortalUserId(userId)).thenReturn(userSessions); VXUser dbvxUser = xUserMgr.updateXUser(vxUser); Assert.assertNotNull(dbvxUser); Assert.assertEquals(dbvxUser.getId(), vxUser.getId()); Assert.assertEquals(dbvxUser.getDescription(), vxUser.getDescription()); Assert.assertEquals(dbvxUser.getName(), vxUser.getName()); Mockito.verify(xUserService).updateResource(vxUser); }
@Test public void test72UpdateXUser() { destroySession(); setup(); Collection<String> existingRoleList = new ArrayList<String>(); existingRoleList.add(RangerConstants.ROLE_USER); Collection<String> reqRoleList = new ArrayList<String>(); reqRoleList.add(RangerConstants.ROLE_SYS_ADMIN); Collection<Long> groupIdList = new ArrayList<Long>(); groupIdList.add(userId); VXUser vxUser = vxUser(); vxUser.setUserRoleList(reqRoleList); vxUser.setUserSource(RangerCommonEnums.USER_EXTERNAL); vxUser.setGroupIdList(groupIdList); vxUser.setFirstName("null"); vxUser.setLastName("null"); vxUser.setPassword("*****"); Mockito.when(xUserService.updateResource(vxUser)).thenReturn(vxUser); VXPortalUser oldUserProfile = userProfile(); oldUserProfile.setUserSource(RangerCommonEnums.USER_APP); oldUserProfile.setPassword(vxUser.getPassword()); VXPortalUser vXPortalUser = userProfile(); vXPortalUser.setUserRoleList(existingRoleList); Mockito.when(userMgr.getUserProfileByLoginId(vxUser.getName())).thenReturn(oldUserProfile); XXPortalUser xXPortalUser = xxPortalUser(vXPortalUser); Mockito.when(userMgr.updateUserWithPass((VXPortalUser) Mockito.any())).thenReturn(xXPortalUser); List<XXUserPermission> xUserPermissionsList = new ArrayList<XXUserPermission>(); XXUserPermission xUserPermissionObj = new XXUserPermission(); xUserPermissionObj.setAddedByUserId(userId); xUserPermissionObj.setCreateTime(new Date()); xUserPermissionObj.setId(userId); xUserPermissionObj.setIsAllowed(1); xUserPermissionObj.setModuleId(1L); xUserPermissionObj.setUpdatedByUserId(userId); xUserPermissionObj.setUpdateTime(new Date()); xUserPermissionObj.setUserId(userId); xUserPermissionsList.add(xUserPermissionObj); VXGroupUserList vxGroupUserList = vxGroupUserList(); Mockito.when(xGroupUserService.searchXGroupUsers((SearchCriteria) Mockito.any())).thenReturn(vxGroupUserList); UserSessionBase userSession = Mockito.mock(UserSessionBase.class); Set<UserSessionBase> userSessions = new HashSet<UserSessionBase>(); userSessions.add(userSession); VXUser dbvxUser = xUserMgr.updateXUser(vxUser); Assert.assertNotNull(dbvxUser); Assert.assertEquals(dbvxUser.getId(), vxUser.getId()); Assert.assertEquals(dbvxUser.getDescription(), vxUser.getDescription()); Assert.assertEquals(dbvxUser.getName(), vxUser.getName()); Mockito.verify(xUserService).updateResource(vxUser); Mockito.when(restErrorUtil.createRESTException("Please provide a valid username.",MessageEnums.INVALID_INPUT_DATA)).thenThrow(new WebApplicationException()); thrown.expect(WebApplicationException.class); vxUser=xUserMgr.updateXUser(null); Assert.assertNull(vxUser); }