@Test public void getDoNotIncludeOwningOrphans() { EntityDefinition def = createEntityDef(EntityNames.STUDENT); Entity orphanNotByUser = createEntity(EntityNames.STUDENT, 14); Entity accessStudent1 = createEntity(EntityNames.STUDENT, 1); Entity accessStudent2 = createEntity(EntityNames.STUDENT, 2); Map<String, Object> metaData = new HashMap<String, Object>(); metaData.put("isOrphaned", "true"); metaData.put("createdBy", "staff2"); Mockito.when(orphanNotByUser.getMetaData()).thenReturn(metaData); List<Entity> students = Arrays.asList(orphanNotByUser, accessStudent1, accessStudent2); Set<String> studentIds = new HashSet(Arrays.asList("student14", "student1", "student2")); Mockito.when(repo.findAll(Mockito.eq(EntityNames.STUDENT), Mockito.any(NeutralQuery.class))).thenReturn(students); Set<String> results = contextValidator.getValidIdsIncludeOrphans(def, studentIds, true); Assert.assertEquals(2, results.size()); }
public Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive) throws APIAccessDeniedException { IContextValidator validator = findValidator(def.getType(), isTransitive); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); Set<String> orphans = new HashSet<String>(); for (Entity entity: entities) { if (isOrphanCreatedByUser(entity)) { orphans.add(entity.getEntityId()); } } Set<String> nonOrphanIds = new HashSet<String>(); nonOrphanIds.addAll(ids); nonOrphanIds.removeAll(orphans); Set<String> validatedIds = validator.getValid(def.getType(), nonOrphanIds); validatedIds.addAll(orphans); return validatedIds; } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }
ContextValidator implements ApplicationContextAware { public Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive) throws APIAccessDeniedException { IContextValidator validator = findValidator(def.getType(), isTransitive); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); Set<String> orphans = new HashSet<String>(); for (Entity entity: entities) { if (isOrphanCreatedByUser(entity)) { orphans.add(entity.getEntityId()); } } Set<String> nonOrphanIds = new HashSet<String>(); nonOrphanIds.addAll(ids); nonOrphanIds.removeAll(orphans); Set<String> validatedIds = validator.getValid(def.getType(), nonOrphanIds); validatedIds.addAll(orphans); return validatedIds; } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } } }
ContextValidator implements ApplicationContextAware { public Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive) throws APIAccessDeniedException { IContextValidator validator = findValidator(def.getType(), isTransitive); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); Set<String> orphans = new HashSet<String>(); for (Entity entity: entities) { if (isOrphanCreatedByUser(entity)) { orphans.add(entity.getEntityId()); } } Set<String> nonOrphanIds = new HashSet<String>(); nonOrphanIds.addAll(ids); nonOrphanIds.removeAll(orphans); Set<String> validatedIds = validator.getValid(def.getType(), nonOrphanIds); validatedIds.addAll(orphans); return validatedIds; } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }  }
ContextValidator implements ApplicationContextAware { public Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive) throws APIAccessDeniedException { IContextValidator validator = findValidator(def.getType(), isTransitive); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); Set<String> orphans = new HashSet<String>(); for (Entity entity: entities) { if (isOrphanCreatedByUser(entity)) { orphans.add(entity.getEntityId()); } } Set<String> nonOrphanIds = new HashSet<String>(); nonOrphanIds.addAll(ids); nonOrphanIds.removeAll(orphans); Set<String> validatedIds = validator.getValid(def.getType(), nonOrphanIds); validatedIds.addAll(orphans); return validatedIds; } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }  @Override void setApplicationContext(ApplicationContext applicationContext); void validateContextToUri(ContainerRequest request, SLIPrincipal principal); boolean isUrlBlocked(ContainerRequest request); List<PathSegment> cleanEmptySegments(List<PathSegment> pathSegments); void validateContextToCallUri(List<PathSegment> segments); boolean checkAccessOfStudentsThroughDisciplineIncidents(List<PathSegment> segments); void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive); Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive); Map<String, SecurityUtil.UserContext> getValidatedEntityContexts(EntityDefinition def, Collection<Entity> entities, boolean isTransitive, boolean isRead); IContextValidator findValidator(String toType, boolean isTransitive); static boolean isTransitive(List<PathSegment> segs); List<IContextValidator> findContextualValidators(String toType, boolean isTransitive); boolean isGlobalEntity(String type); }
ContextValidator implements ApplicationContextAware { public Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive) throws APIAccessDeniedException { IContextValidator validator = findValidator(def.getType(), isTransitive); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); Set<String> orphans = new HashSet<String>(); for (Entity entity: entities) { if (isOrphanCreatedByUser(entity)) { orphans.add(entity.getEntityId()); } } Set<String> nonOrphanIds = new HashSet<String>(); nonOrphanIds.addAll(ids); nonOrphanIds.removeAll(orphans); Set<String> validatedIds = validator.getValid(def.getType(), nonOrphanIds); validatedIds.addAll(orphans); return validatedIds; } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }  @Override void setApplicationContext(ApplicationContext applicationContext); void validateContextToUri(ContainerRequest request, SLIPrincipal principal); boolean isUrlBlocked(ContainerRequest request); List<PathSegment> cleanEmptySegments(List<PathSegment> pathSegments); void validateContextToCallUri(List<PathSegment> segments); boolean checkAccessOfStudentsThroughDisciplineIncidents(List<PathSegment> segments); void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive); Set<String> getValidIdsIncludeOrphans(EntityDefinition def, Set<String> ids, boolean isTransitive); Map<String, SecurityUtil.UserContext> getValidatedEntityContexts(EntityDefinition def, Collection<Entity> entities, boolean isTransitive, boolean isRead); IContextValidator findValidator(String toType, boolean isTransitive); static boolean isTransitive(List<PathSegment> segs); List<IContextValidator> findContextualValidators(String toType, boolean isTransitive); boolean isGlobalEntity(String type);  }