public static List<SecurityContext.ProjectEntry> buildProjectEntries( Collection<ProjectState> projects, Collection<String> userGroupLinks) { return projects.stream() .map((project) -> { ProjectEntry projectEntry = new SecurityContext.ProjectEntry(); projectEntry.documentSelfLink = project.documentSelfLink; projectEntry.name = project.name; projectEntry.roles = new HashSet<>(); projectEntry.customProperties = project.customProperties; if (CollectionUtils.containsAny( project.administratorsUserGroupLinks, userGroupLinks)) { projectEntry.roles.add(AuthRole.PROJECT_ADMIN); } if (CollectionUtils.containsAny( project.membersUserGroupLinks, userGroupLinks)) { projectEntry.roles.add(AuthRole.PROJECT_MEMBER); projectEntry.roles.add(AuthRole.PROJECT_MEMBER_EXTENDED); } if (CollectionUtils.containsAny( project.viewersUserGroupLinks, userGroupLinks)) { projectEntry.roles.add(AuthRole.PROJECT_VIEWER); } return projectEntry; }).collect(Collectors.toList()); }
List<ProjectEntry> entries = new ArrayList<>(); for (ProjectState state : projectStates) { ProjectEntry entry = new ProjectEntry(); entry.documentSelfLink = state.documentSelfLink; entry.name = state.name;
@Test public void testIsUserAuthorized() throws Throwable { RequestBrokerService r = new RequestBrokerService(); Method m = r.getClass().getDeclaredMethod("isUserAuthorized", SecurityContext.ProjectEntry.class, SecurityContext.class); m.setAccessible(true); SecurityContext context = new SecurityContext(); context.projects = new ArrayList<>(); SecurityContext.ProjectEntry project = new SecurityContext.ProjectEntry(); project.documentSelfLink = "link"; project.roles = new HashSet<>(); project.roles.add(AuthRole.PROJECT_ADMIN); DeferredResult<Void> deferred = (DeferredResult<Void>) m.invoke(r, project, context); assertNotNull(deferred); assertTrue(deferred.toCompletionStage().toCompletableFuture().isCompletedExceptionally()); context.projects.add(project); deferred = (DeferredResult<Void>) m.invoke(r, project, context); assertNotNull(deferred); assertTrue(deferred.toCompletionStage().toCompletableFuture().isDone()); assertFalse(deferred.toCompletionStage().toCompletableFuture().isCancelled()); assertFalse(deferred.toCompletionStage().toCompletableFuture().isCompletedExceptionally()); }
private static DeferredResult<ProjectEntry> extractProjectEntryFromRoleState(ServiceHost host, RoleState roleState) { ProjectEntry entry = new ProjectEntry(); String roleStateId = Service.getId(roleState.documentSelfLink); String[] roleStateIdData = extractDataFromRoleStateId(roleStateId); if (roleStateIdData.length != 3) { return DeferredResult.failed(new RuntimeException("Cannot extract project entry from " + "role state with invalid id: " + roleState.documentSelfLink)); } String projectId = roleStateIdData[0]; AuthRole projectRole = AuthRole.fromSuffix(roleStateIdData[2]); entry.roles = Collections.singleton(projectRole); return getProjectState(host, projectId) .thenApply(projectState -> { entry.documentSelfLink = projectState.documentSelfLink; entry.name = projectState.name; entry.customProperties = projectState.customProperties; return entry; }); }