@SuppressWarnings("unchecked") @Test public void testListBasedOnContextualRoles() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { securityContextInjector.setEducatorContext(); SecurityUtil.setUserContext(SecurityUtil.UserContext.TEACHER_CONTEXT); RightAccessValidator mockAccessValidator = Mockito.mock(RightAccessValidator.class); Field rightAccessValidator = BasicService.class.getDeclaredField("rightAccessValidator"); rightAccessValidator.setAccessible(true); rightAccessValidator.set(service, mockAccessValidator); Collection<GrantedAuthority> teacherContextRights = new HashSet<GrantedAuthority>(Arrays.asList(Right.TEACHER_CONTEXT, Right.READ_PUBLIC, Right.WRITE_PUBLIC)); EntityBody entityBody1 = new EntityBody(); entityBody1.put("studentUniqueStateId", "student1"); EntityBody entityBody2 = new EntityBody(); entityBody2.put("studentUniqueStateId", "student2"); Entity entity1 = new MongoEntity("student", "student1", entityBody1, new HashMap<String,Object>()); Entity entity2 = new MongoEntity("student", "student2", entityBody2, new HashMap<String,Object>()); Iterable<Entity> entities = Arrays.asList(entity1, entity2); Mockito.when(mockRepo.findAll(Mockito.eq("student"), Mockito.any(NeutralQuery.class))).thenReturn(entities); Mockito.when(mockRepo.count(Mockito.eq("student"), Mockito.any(NeutralQuery.class))).thenReturn(2L); Mockito.when(mockRightsFilter.makeEntityBody(Mockito.eq(entity1), Mockito.any(List.class), Mockito.any(EntityDefinition.class), Mockito.anyBoolean(), Mockito.any(Collection.class), Mockito.any(SecurityUtil.UserContext.class))).thenReturn(entityBody1); Mockito.when(mockRightsFilter.makeEntityBody(Mockito.eq(entity2), Mockito.any(List.class), Mockito.any(EntityDefinition.class), Mockito.anyBoolean(), Mockito.any(Collection.class), Mockito.any(SecurityUtil.UserContext.class))).thenReturn(entityBody2); Mockito.when(mockAccessValidator.getContextualAuthorities(Matchers.eq(false), Matchers.eq(entity1), Matchers.eq(SecurityUtil.UserContext.TEACHER_CONTEXT), Matchers.eq(true))).thenReturn(teacherContextRights); ContextValidator mockContextValidator = Mockito.mock(ContextValidator.class); Field contextValidator = BasicService.class.getDeclaredField("contextValidator"); contextValidator.setAccessible(true); contextValidator.set(service, mockContextValidator); Map<String, SecurityUtil.UserContext> studentContext = new HashMap<String, SecurityUtil.UserContext>(); studentContext.put(entity1.getEntityId(), SecurityUtil.UserContext.TEACHER_CONTEXT); studentContext.put(entity2.getEntityId(), SecurityUtil.UserContext.TEACHER_CONTEXT); Mockito.when(mockContextValidator.getValidatedEntityContexts(Matchers.any(EntityDefinition.class), Matchers.any(Collection.class), Matchers.anyBoolean(), Matchers.anyBoolean())).thenReturn(studentContext); NeutralQuery query = new NeutralQuery(); query.setLimit(ApiQuery.API_QUERY_DEFAULT_LIMIT); RequestUtil.setCurrentRequestId(); Iterable<EntityBody> listResult = service.listBasedOnContextualRoles(query); List<EntityBody> bodies= new ArrayList<EntityBody>(); for (EntityBody body : listResult) { bodies.add(body); } Assert.assertEquals("EntityBody mismatch", entityBody1, bodies.toArray()[0]); Assert.assertEquals("EntityBody mismatch", entityBody2, bodies.toArray()[1]); long count = service.countBasedOnContextualRoles(new NeutralQuery()); Assert.assertEquals(2, count); }
@Override public Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery) { boolean isSelf = isSelf(neutralQuery); boolean noDataInDB = true; Map<String, UserContext> entityContexts = null; injectSecurity(neutralQuery); boolean findSpecial = userHasMultipleContextsOrDifferingRights() && (!EntityNames.isPublic(defn.getType())); Collection<Entity> entities = new HashSet<Entity>(); if (findSpecial) { entities = getResponseEntities(neutralQuery, isSelf); entityContexts = getEntityContexts(); } else { entities = (Collection<Entity>) repo.findAll(collectionName, neutralQuery); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { entityContexts = getEntityContextMap(entities, true); } } Collection<Entity> bodylessEntities = new HashSet<Entity>(); for(Entity ent : entities) { if(ent.getBody() == null || ent.getBody().size() == 0) { bodylessEntities.add(ent); } } entities.removeAll(bodylessEntities); noDataInDB = entities.isEmpty(); List<EntityBody> results = new ArrayList<EntityBody>(); for (Entity entity : entities) { UserContext context = getEntityContext(entity.getEntityId(), entityContexts); try { Collection<GrantedAuthority> auths = null; if (!findSpecial) { auths = rightAccessValidator.getContextualAuthorities(isSelf, entity, context, true); rightAccessValidator.checkAccess(true, isSelf, entity, defn.getType(), auths); rightAccessValidator.checkFieldAccess(neutralQuery, entity, defn.getType(), auths); } else { auths = getEntityAuthorities(entity.getEntityId()); } results.add(entityRightsFilter.makeEntityBody(entity, treatments, defn, isSelf, auths, context)); } catch (AccessDeniedException aex) { if (entities.size() == 1) { throw aex; } else { LOG.error(aex.getMessage()); } } } if (results.isEmpty()) { validateQuery(neutralQuery, isSelf); return noEntitiesFound(noDataInDB); } return results; }
BasicService implements EntityService, AccessibilityCheck { @Override public Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery) { boolean isSelf = isSelf(neutralQuery); boolean noDataInDB = true; Map<String, UserContext> entityContexts = null; injectSecurity(neutralQuery); boolean findSpecial = userHasMultipleContextsOrDifferingRights() && (!EntityNames.isPublic(defn.getType())); Collection<Entity> entities = new HashSet<Entity>(); if (findSpecial) { entities = getResponseEntities(neutralQuery, isSelf); entityContexts = getEntityContexts(); } else { entities = (Collection<Entity>) repo.findAll(collectionName, neutralQuery); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { entityContexts = getEntityContextMap(entities, true); } } Collection<Entity> bodylessEntities = new HashSet<Entity>(); for(Entity ent : entities) { if(ent.getBody() == null || ent.getBody().size() == 0) { bodylessEntities.add(ent); } } entities.removeAll(bodylessEntities); noDataInDB = entities.isEmpty(); List<EntityBody> results = new ArrayList<EntityBody>(); for (Entity entity : entities) { UserContext context = getEntityContext(entity.getEntityId(), entityContexts); try { Collection<GrantedAuthority> auths = null; if (!findSpecial) { auths = rightAccessValidator.getContextualAuthorities(isSelf, entity, context, true); rightAccessValidator.checkAccess(true, isSelf, entity, defn.getType(), auths); rightAccessValidator.checkFieldAccess(neutralQuery, entity, defn.getType(), auths); } else { auths = getEntityAuthorities(entity.getEntityId()); } results.add(entityRightsFilter.makeEntityBody(entity, treatments, defn, isSelf, auths, context)); } catch (AccessDeniedException aex) { if (entities.size() == 1) { throw aex; } else { LOG.error(aex.getMessage()); } } } if (results.isEmpty()) { validateQuery(neutralQuery, isSelf); return noEntitiesFound(noDataInDB); } return results; } }
BasicService implements EntityService, AccessibilityCheck { @Override public Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery) { boolean isSelf = isSelf(neutralQuery); boolean noDataInDB = true; Map<String, UserContext> entityContexts = null; injectSecurity(neutralQuery); boolean findSpecial = userHasMultipleContextsOrDifferingRights() && (!EntityNames.isPublic(defn.getType())); Collection<Entity> entities = new HashSet<Entity>(); if (findSpecial) { entities = getResponseEntities(neutralQuery, isSelf); entityContexts = getEntityContexts(); } else { entities = (Collection<Entity>) repo.findAll(collectionName, neutralQuery); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { entityContexts = getEntityContextMap(entities, true); } } Collection<Entity> bodylessEntities = new HashSet<Entity>(); for(Entity ent : entities) { if(ent.getBody() == null || ent.getBody().size() == 0) { bodylessEntities.add(ent); } } entities.removeAll(bodylessEntities); noDataInDB = entities.isEmpty(); List<EntityBody> results = new ArrayList<EntityBody>(); for (Entity entity : entities) { UserContext context = getEntityContext(entity.getEntityId(), entityContexts); try { Collection<GrantedAuthority> auths = null; if (!findSpecial) { auths = rightAccessValidator.getContextualAuthorities(isSelf, entity, context, true); rightAccessValidator.checkAccess(true, isSelf, entity, defn.getType(), auths); rightAccessValidator.checkFieldAccess(neutralQuery, entity, defn.getType(), auths); } else { auths = getEntityAuthorities(entity.getEntityId()); } results.add(entityRightsFilter.makeEntityBody(entity, treatments, defn, isSelf, auths, context)); } catch (AccessDeniedException aex) { if (entities.size() == 1) { throw aex; } else { LOG.error(aex.getMessage()); } } } if (results.isEmpty()) { validateQuery(neutralQuery, isSelf); return noEntitiesFound(noDataInDB); } return results; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); }
BasicService implements EntityService, AccessibilityCheck { @Override public Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery) { boolean isSelf = isSelf(neutralQuery); boolean noDataInDB = true; Map<String, UserContext> entityContexts = null; injectSecurity(neutralQuery); boolean findSpecial = userHasMultipleContextsOrDifferingRights() && (!EntityNames.isPublic(defn.getType())); Collection<Entity> entities = new HashSet<Entity>(); if (findSpecial) { entities = getResponseEntities(neutralQuery, isSelf); entityContexts = getEntityContexts(); } else { entities = (Collection<Entity>) repo.findAll(collectionName, neutralQuery); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { entityContexts = getEntityContextMap(entities, true); } } Collection<Entity> bodylessEntities = new HashSet<Entity>(); for(Entity ent : entities) { if(ent.getBody() == null || ent.getBody().size() == 0) { bodylessEntities.add(ent); } } entities.removeAll(bodylessEntities); noDataInDB = entities.isEmpty(); List<EntityBody> results = new ArrayList<EntityBody>(); for (Entity entity : entities) { UserContext context = getEntityContext(entity.getEntityId(), entityContexts); try { Collection<GrantedAuthority> auths = null; if (!findSpecial) { auths = rightAccessValidator.getContextualAuthorities(isSelf, entity, context, true); rightAccessValidator.checkAccess(true, isSelf, entity, defn.getType(), auths); rightAccessValidator.checkFieldAccess(neutralQuery, entity, defn.getType(), auths); } else { auths = getEntityAuthorities(entity.getEntityId()); } results.add(entityRightsFilter.makeEntityBody(entity, treatments, defn, isSelf, auths, context)); } catch (AccessDeniedException aex) { if (entities.size() == 1) { throw aex; } else { LOG.error(aex.getMessage()); } } } if (results.isEmpty()) { validateQuery(neutralQuery, isSelf); return noEntitiesFound(noDataInDB); } return results; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); @Override long count(NeutralQuery neutralQuery); @Override long countBasedOnContextualRoles(NeutralQuery neutralQuery); @Override List<String> create(List<EntityBody> content); @Override List<String> createBasedOnContextualRoles(List<EntityBody> content); @Override Iterable<String> listIds(final NeutralQuery neutralQuery); @Override String create(EntityBody content); @Override String createBasedOnContextualRoles(EntityBody content); @Override boolean accessibilityCheck(String id); @Override void delete(String id); @Override void deleteBasedOnContextualRoles(String id); @Override boolean update(String id, EntityBody content, boolean applySecurityContext); @Override boolean patch(String id, EntityBody content, boolean applySecurityContext); @Override EntityBody get(String id); @Override EntityBody get(String id, NeutralQuery neutralQuery); @Override Iterable<EntityBody> get(Iterable<String> ids); @Override Iterable<EntityBody> get(Iterable<String> ids, final NeutralQuery neutralQuery); @Override Iterable<EntityBody> list(NeutralQuery neutralQuery); @Override Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery); @Override boolean exists(String id); @Override EntityBody getCustom(String id); @Override void deleteCustom(String id); @Override void createOrUpdateCustom(String id, EntityBody customEntity); void setDefn(EntityDefinition defn); @Override EntityDefinition getEntityDefinition(); long getCountLimit(); int getBatchSize(); @Override CalculatedData<String> getCalculatedValues(String id); @Override CalculatedData<Map<String, Integer>> getAggregates(String id); @Override boolean collectionExists(String collection); }
BasicService implements EntityService, AccessibilityCheck { @Override public Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery) { boolean isSelf = isSelf(neutralQuery); boolean noDataInDB = true; Map<String, UserContext> entityContexts = null; injectSecurity(neutralQuery); boolean findSpecial = userHasMultipleContextsOrDifferingRights() && (!EntityNames.isPublic(defn.getType())); Collection<Entity> entities = new HashSet<Entity>(); if (findSpecial) { entities = getResponseEntities(neutralQuery, isSelf); entityContexts = getEntityContexts(); } else { entities = (Collection<Entity>) repo.findAll(collectionName, neutralQuery); if (SecurityUtil.getUserContext() == UserContext.DUAL_CONTEXT) { entityContexts = getEntityContextMap(entities, true); } } Collection<Entity> bodylessEntities = new HashSet<Entity>(); for(Entity ent : entities) { if(ent.getBody() == null || ent.getBody().size() == 0) { bodylessEntities.add(ent); } } entities.removeAll(bodylessEntities); noDataInDB = entities.isEmpty(); List<EntityBody> results = new ArrayList<EntityBody>(); for (Entity entity : entities) { UserContext context = getEntityContext(entity.getEntityId(), entityContexts); try { Collection<GrantedAuthority> auths = null; if (!findSpecial) { auths = rightAccessValidator.getContextualAuthorities(isSelf, entity, context, true); rightAccessValidator.checkAccess(true, isSelf, entity, defn.getType(), auths); rightAccessValidator.checkFieldAccess(neutralQuery, entity, defn.getType(), auths); } else { auths = getEntityAuthorities(entity.getEntityId()); } results.add(entityRightsFilter.makeEntityBody(entity, treatments, defn, isSelf, auths, context)); } catch (AccessDeniedException aex) { if (entities.size() == 1) { throw aex; } else { LOG.error(aex.getMessage()); } } } if (results.isEmpty()) { validateQuery(neutralQuery, isSelf); return noEntitiesFound(noDataInDB); } return results; } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); @Override long count(NeutralQuery neutralQuery); @Override long countBasedOnContextualRoles(NeutralQuery neutralQuery); @Override List<String> create(List<EntityBody> content); @Override List<String> createBasedOnContextualRoles(List<EntityBody> content); @Override Iterable<String> listIds(final NeutralQuery neutralQuery); @Override String create(EntityBody content); @Override String createBasedOnContextualRoles(EntityBody content); @Override boolean accessibilityCheck(String id); @Override void delete(String id); @Override void deleteBasedOnContextualRoles(String id); @Override boolean update(String id, EntityBody content, boolean applySecurityContext); @Override boolean patch(String id, EntityBody content, boolean applySecurityContext); @Override EntityBody get(String id); @Override EntityBody get(String id, NeutralQuery neutralQuery); @Override Iterable<EntityBody> get(Iterable<String> ids); @Override Iterable<EntityBody> get(Iterable<String> ids, final NeutralQuery neutralQuery); @Override Iterable<EntityBody> list(NeutralQuery neutralQuery); @Override Iterable<EntityBody> listBasedOnContextualRoles(NeutralQuery neutralQuery); @Override boolean exists(String id); @Override EntityBody getCustom(String id); @Override void deleteCustom(String id); @Override void createOrUpdateCustom(String id, EntityBody customEntity); void setDefn(EntityDefinition defn); @Override EntityDefinition getEntityDefinition(); long getCountLimit(); int getBatchSize(); @Override CalculatedData<String> getCalculatedValues(String id); @Override CalculatedData<Map<String, Integer>> getAggregates(String id); @Override boolean collectionExists(String collection);  }