@Test public void testValidateContextToEntitiesSelf() { EntityDefinition def = createEntityDef("staff"); Entity staff1 = createEntity("staff", 1); Mockito.when(repo.findAll(Mockito.matches("staff"), Mockito.any(NeutralQuery.class))).thenReturn(Arrays.asList(staff1)); Mockito.when(edOrgHelper.getDirectEdorgs(Mockito.eq(staff1))).thenReturn(new HashSet<String>(Arrays.asList("edOrg1"))); boolean isTransitive = false; Collection<String> ids = new HashSet<String>(Arrays.asList("staff1")); contextValidator.validateContextToEntities(def, ids, isTransitive); Mockito.verify(ownership, Mockito.never()).canAccess(Mockito.any(Entity.class), Mockito.anyBoolean()); }
public void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids==null) ? "null" : ids.toArray().toString() ); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }
ContextValidator implements ApplicationContextAware { public void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids==null) ? "null" : ids.toArray().toString() ); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } } }
ContextValidator implements ApplicationContextAware { public void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids==null) ? "null" : ids.toArray().toString() ); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } else { throw new APIAccessDeniedException("No validator for " + def.getType() + ", transitive=" + isTransitive, def.getType(), ids); } }  }
ContextValidator implements ApplicationContextAware { public void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids==null) ? "null" : ids.toArray().toString() ); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } 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 void validateContextToEntities(EntityDefinition def, Collection<String> ids, boolean isTransitive) throws APIAccessDeniedException { LOG.debug(">>>ContextValidator.validateContextToEntities()"); LOG.debug(" def: " + ToStringBuilder.reflectionToString(def, ToStringStyle.DEFAULT_STYLE)); LOG.debug(" ids: {}", (ids==null) ? "null" : ids.toArray().toString() ); LOG.debug(" isTransitive" + isTransitive); IContextValidator validator = findValidator(def.getType(), isTransitive); LOG.debug(" loaded validator: " + ToStringBuilder.reflectionToString(validator, ToStringStyle.DEFAULT_STYLE)); if (validator != null) { NeutralQuery getIdsQuery = new NeutralQuery(new NeutralCriteria("_id", "in", new ArrayList<String>(ids))); LOG.debug(" getIdsQuery: " + getIdsQuery.toString()); Collection<Entity> entities = (Collection<Entity>) repo.findAll(def.getStoredCollectionName(), getIdsQuery); LOG.debug(" entities: " + ToStringBuilder.reflectionToString(entities, ToStringStyle.DEFAULT_STYLE)); Set<String> idsToValidate = getEntityIdsToValidate(def, entities, isTransitive, ids); LOG.debug(" idsToValidate: " + idsToValidate.toString()); Set<String> validatedIds = getValidatedIds(def, idsToValidate, validator); LOG.debug(" validatedIds: " + validatedIds.toString()); if (!validatedIds.containsAll(idsToValidate)) { LOG.debug(" ...APIAccessDeniedException"); throw new APIAccessDeniedException("Cannot access entities", def.getType(), idsToValidate); } } 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);  }