BusinessAdminTasksPreprocessor extends UserTasksPreprocessor { @SuppressWarnings("rawtypes") @Override public void preprocess(DataSetLookup lookup) { if (identityProvider == null || userGroupCallback == null) { return; } if (ADMIN_USER.equals(identityProvider.getName())) { return; } final List<String> groups = Optional.ofNullable(userGroupCallback.getGroupsForUser(identityProvider.getName())).orElse(new ArrayList<>()); if (groups.stream().filter(s -> s.equals(ADMIN_GROUP)).findFirst().isPresent()) { return; } final List<Comparable> orgEntities = new ArrayList<>(groups); orgEntities.add(identityProvider.getName()); final ColumnFilter columnFilter = equalsTo(COLUMN_ORGANIZATIONAL_ENTITY, orgEntities); LOGGER.debug("Adding column filter: {}", columnFilter); if (lookup.getFirstFilterOp() != null) { lookup.getFirstFilterOp().addFilterColumn(columnFilter); } else { DataSetFilter filter = new DataSetFilter(); filter.addFilterColumn(columnFilter); lookup.addOperation(filter); } super.preprocess(lookup); } BusinessAdminTasksPreprocessor(IdentityProvider identityProvider,
                                          UserGroupCallback userGroupCallback,
                                          DataSetMetadata metadata); @SuppressWarnings("rawtypes") @Override void preprocess(DataSetLookup lookup); static final String ADMIN_USER; static final String ADMIN_GROUP; }
@Test public void testQueryByGroupBusinessAdmin() { dataSetLookup = spy(new DataSetLookup()); String role1 = "admins"; String role2 = "role2"; String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Arrays.asList(role1, role2)); when(identityProvider.getName()).thenReturn(userId); preprocessor.preprocess(dataSetLookup); verifyNoMoreInteractions(dataSetLookup); assertNull(dataSetLookup.getFirstFilterOp()); }
@Test public void testQueryByUserBusinessAdmin() { dataSetLookup = spy(new DataSetLookup()); String role1 = "role1"; String role2 = "role2"; String userId = "admin"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Arrays.asList(role1, role2)); when(identityProvider.getName()).thenReturn(userId); preprocessor.preprocess(dataSetLookup); verifyNoMoreInteractions(dataSetLookup); assertNull(dataSetLookup.getFirstFilterOp()); }
@Test public void testQueryByNoBusinessAdmin() { dataSetLookup = new DataSetLookup(); String role1 = "role1"; String role2 = "role2"; String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Arrays.asList(role1, role2)); when(identityProvider.getName()).thenReturn(userId); preprocessor.preprocess(dataSetLookup); assertEquals("ID = role1, role2, userId", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testSetUserWithoutRoles() { dataSetLookup = new DataSetLookup(); String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Collections.emptyList()); when(identityProvider.getName()).thenReturn(userId); preprocessor.preprocess(dataSetLookup); assertEquals("ID = userId", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testNullGroups() { dataSetLookup = new DataSetLookup(); String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); preprocessor.preprocess(dataSetLookup); assertEquals("ID = userId", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }