UserAccessExpressionEvaluator implements ExpressionEvaluator { @Override public boolean hasAccessToProject(Authentication authentication, User user, String projectId) { if(projectDoesNotExistOrHasLobIndependentStatus(projectId)) { return true; } if(userService.userIsManager(user)) { return true; } var project = projectService.getProjectById(projectId); if(!userAccessService.userHasActiveAccessInterval(user)) { return applicationService.userHasAppliedForProject(user, project) || bookmarkService.userHasBookmark(user, project); } var projectLob = project.getLob(); var userLob = userService.getUserData(user).getLob(); var projectLobNullOrEqual = projectLob == null || projectLob.equalsIgnoreCase(userLob); if(projectLobNullOrEqual) { return true; } return applicationService.userHasAppliedForProject(user, project) || bookmarkService.userHasBookmark(user, project); } @Autowired  UserAccessExpressionEvaluator(UserService userService,
                                         UserAccessService userAccessService,
                                         ProjectService projectService,
                                         ApplicationService applicationService,
                                         BookmarkService bookmarkService,
                                         ProjectBoardConfigurationProperties properties); @Override boolean hasAccessToProjects(Authentication authentication, User user); @Override boolean hasAccessToProject(Authentication authentication, User user, String projectId); @Override boolean hasPermissionToApplyToProject(Authentication authentication, User user, String projectId); @Override boolean hasPermissionToAccessUser(Authentication authentication, User user, String userId); @Override boolean hasElevatedAccessToUser(Authentication authentication, User user, String userId);  }
@Test public void hasAccessToProjectReturnsTrueWhenUserHasDifferentLobAndAppliedForProjectAndHasAccess() { var userLob = "LOB Test"; var projectLob = "LOB Prod"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS.toUpperCase()); given(projectMock.getLob()).willReturn(projectLob); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(userLob); given(applicationServiceMock.userHasAppliedForProject(userMock, projectMock)).willReturn(true); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenUserHasNotAppliedForProjectAndHasNoAccess() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS.toUpperCase()); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isFalse(); }
@Test public void hasAccessToProjectReturnsTrueWhenProjectDoesNotExist() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(false); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenUserHasBookmarkedProject() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(bookmarkServiceMock.userHasBookmark(userMock, projectMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenStatusIsLobIndependent() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_INDEPENDENT_STATUS.toUpperCase()); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenStatusIsLobDependentAndUserHasSameLobAndHasAccess() { var lob = "LOB Prod"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS.toUpperCase()); given(projectMock.getLob()).willReturn(lob); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(lob); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenStatusIsLobDependentAndUserIsManager() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS.toUpperCase()); given(userServiceMock.userIsManager(userMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsTrueWhenStatusIsLobDependentAndLobIsNullAndUserHasAccess() { given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS.toUpperCase()); given(projectMock.getLob()).willReturn(null); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn("Anything"); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isTrue(); }
@Test public void hasAccessToProjectReturnsFalseWhenStatusIsLobDependentAndUserHasDifferentLobAndHasAccess() { var userLob = "LOB Test"; var projectLob = "LOB Prod"; given(projectServiceMock.projectExists(PROJECT_ID)).willReturn(true); given(projectServiceMock.getProjectById(PROJECT_ID)).willReturn(projectMock); given(projectMock.getStatus()).willReturn(LOB_DEPENDENT_STATUS); given(projectMock.getLob()).willReturn(projectLob); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getLob()).willReturn(userLob); given(userAccessServiceMock.userHasActiveAccessInterval(userMock)).willReturn(true); boolean actualHasAccess = evaluator.hasAccessToProject(authenticationMock, userMock, PROJECT_ID); assertThat(actualHasAccess).isFalse(); }