@Test public void testgetEntityContextAuthorities() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException { securityContextInjector.setDualContext(); RightAccessValidator mockAccessValidator = Mockito.mock(RightAccessValidator.class); Field rightAccessValidator = BasicService.class.getDeclaredField("rightAccessValidator"); rightAccessValidator.setAccessible(true); rightAccessValidator.set(service, mockAccessValidator); boolean isSelf = true; boolean isRead = true; EntityBody entityBody1 = new EntityBody(); entityBody1.put("studentUniqueStateId", "student1"); Entity student = securityRepo.create(EntityNames.STUDENT, entityBody1); Collection<GrantedAuthority> staffContextRights = SecurityUtil.getSLIPrincipal().getEdOrgContextRights().get(SecurityContextInjector.ED_ORG_ID).get(SecurityUtil.UserContext.STAFF_CONTEXT); Collection<GrantedAuthority> teacherContextRights = SecurityUtil.getSLIPrincipal().getEdOrgContextRights().get(SecurityContextInjector.ED_ORG_ID).get(SecurityUtil.UserContext.TEACHER_CONTEXT); Mockito.when(mockAccessValidator.getContextualAuthorities(Matchers.eq(isSelf), Matchers.eq(student), Matchers.eq(SecurityUtil.UserContext.STAFF_CONTEXT), Matchers.eq(isRead))).thenReturn(staffContextRights); Mockito.when(mockAccessValidator.getContextualAuthorities(Matchers.eq(isSelf), Matchers.eq(student), Matchers.eq(SecurityUtil.UserContext.TEACHER_CONTEXT), Matchers.eq(isRead))).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(student.getEntityId(), SecurityUtil.UserContext.STAFF_CONTEXT); Mockito.when(mockContextValidator.getValidatedEntityContexts(Matchers.any(EntityDefinition.class), Matchers.any(Collection.class), Matchers.anyBoolean(), Matchers.anyBoolean())).thenReturn(studentContext); Collection<GrantedAuthority> rights = service.getEntityContextAuthorities(student, isSelf, isRead); Assert.assertEquals(staffContextRights, rights); securityContextInjector.setEducatorContext(); rights = service.getEntityContextAuthorities(student, isSelf, isRead); Assert.assertEquals(teacherContextRights, rights); }
protected Collection<GrantedAuthority> getEntityContextAuthorities(Entity entity, boolean isSelf, boolean isRead) { UserContext context = SecurityUtil.getUserContext(); if (context == UserContext.DUAL_CONTEXT) { Map<String, UserContext> entityContext = getEntityContextMap(Arrays.asList(entity), isRead); if (entityContext != null) { context = entityContext.get(entity.getEntityId()); } } return rightAccessValidator.getContextualAuthorities(isSelf, entity, context, isRead); }
BasicService implements EntityService, AccessibilityCheck { protected Collection<GrantedAuthority> getEntityContextAuthorities(Entity entity, boolean isSelf, boolean isRead) { UserContext context = SecurityUtil.getUserContext(); if (context == UserContext.DUAL_CONTEXT) { Map<String, UserContext> entityContext = getEntityContextMap(Arrays.asList(entity), isRead); if (entityContext != null) { context = entityContext.get(entity.getEntityId()); } } return rightAccessValidator.getContextualAuthorities(isSelf, entity, context, isRead); } }
BasicService implements EntityService, AccessibilityCheck { protected Collection<GrantedAuthority> getEntityContextAuthorities(Entity entity, boolean isSelf, boolean isRead) { UserContext context = SecurityUtil.getUserContext(); if (context == UserContext.DUAL_CONTEXT) { Map<String, UserContext> entityContext = getEntityContextMap(Arrays.asList(entity), isRead); if (entityContext != null) { context = entityContext.get(entity.getEntityId()); } } return rightAccessValidator.getContextualAuthorities(isSelf, entity, context, isRead); } BasicService(String collectionName, List<Treatment> treatments, Repository<Entity> repo); }
BasicService implements EntityService, AccessibilityCheck { protected Collection<GrantedAuthority> getEntityContextAuthorities(Entity entity, boolean isSelf, boolean isRead) { UserContext context = SecurityUtil.getUserContext(); if (context == UserContext.DUAL_CONTEXT) { Map<String, UserContext> entityContext = getEntityContextMap(Arrays.asList(entity), isRead); if (entityContext != null) { context = entityContext.get(entity.getEntityId()); } } return rightAccessValidator.getContextualAuthorities(isSelf, entity, context, isRead); } 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 { protected Collection<GrantedAuthority> getEntityContextAuthorities(Entity entity, boolean isSelf, boolean isRead) { UserContext context = SecurityUtil.getUserContext(); if (context == UserContext.DUAL_CONTEXT) { Map<String, UserContext> entityContext = getEntityContextMap(Arrays.asList(entity), isRead); if (entityContext != null) { context = entityContext.get(entity.getEntityId()); } } return rightAccessValidator.getContextualAuthorities(isSelf, entity, context, isRead); } 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);  }