UserAndGroupsTasksPreprocessor extends UserTasksPreprocessor { @SuppressWarnings("rawtypes") @Override public void preprocess(DataSetLookup lookup) { if (identityProvider == null || userGroupCallback == null) { return; } ColumnFilter columnFilter; List<Comparable> orgEntities = new ArrayList<>(); if (lookup.getFirstFilterOp() != null) { List<String> potOwners = new ArrayList<String>(); List<CoreFunctionFilter> columnFilters = extractCoreFunctionFilter(lookup.getFirstFilterOp().getColumnFilterList()); Iterator<CoreFunctionFilter> it = columnFilters.iterator(); while (orgEntities.isEmpty() && it.hasNext()) { CoreFunctionFilter column = it.next(); if (column.getColumnId().toUpperCase().equals(columnId)) { potOwners.addAll(column.getParameters()); for (String potOwner : potOwners) { addUserAndGroupsFromIdentityProvider(orgEntities, potOwner); } removeCoreFunctionFilter(lookup.getFirstFilterOp().getColumnFilterList(), column); } } if (orgEntities.isEmpty()) { addUserAndGroupsFromIdentityProvider(orgEntities, identityProvider.getName()); } columnFilter = AND(in(COLUMN_POTOWNER, orgEntities)); lookup.getFirstFilterOp().addFilterColumn(columnFilter); } else { DataSetFilter filter = new DataSetFilter(); addUserAndGroupsFromIdentityProvider(orgEntities, identityProvider.getName()); columnFilter = AND(in(COLUMN_POTOWNER, orgEntities)); filter.addFilterColumn(columnFilter); lookup.addOperation(filter); } LOGGER.debug("Adding column filter: {}", columnFilter); super.preprocess(lookup); } UserAndGroupsTasksPreprocessor(IdentityProvider identityProvider, UserGroupCallback userGroupCallback, 
                                          String columnId, DataSetMetadata metadata); @SuppressWarnings("rawtypes") @Override void preprocess(DataSetLookup lookup);  }
@Test public void testSetUser() { String role1 = "role1"; String role2 = "role2"; String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Arrays.asList(role1, role2)); when(identityProvider.getName()).thenReturn(userId); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in " + role1 + ", " + role2 + ", " + userId + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testSetUserWithoutRoles() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(Collections.emptyList()); when(identityProvider.getName()).thenReturn(userId); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in " + userId + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testNullGroups() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in " + userId + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testPotOwnerFilterNoIdentityProvider() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); String potOwner = "potOwner"; when(userGroupCallback.getGroupsForUser(potOwner)).thenReturn(Arrays.asList("role1", "role2")); List<String> potOwners = new ArrayList<String>(); potOwners.add(potOwner); QueryParam queryParam = new QueryParam(COL_ID, "IN", potOwners); List<QueryParam> queryParams = new ArrayList<QueryParam>(); queryParams.add(queryParam); CoreFunctionQueryParamBuilder coreFunctionQueryParamBuilder = new CoreFunctionQueryParamBuilder(queryParam); CoreFunctionFilter columnFilter = (CoreFunctionFilter) coreFunctionQueryParamBuilder.build(); DataSetFilter filter = new DataSetFilter(); filter.addFilterColumn(columnFilter); dataSetLookup.addOperation(filter); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in role1, role2, " + potOwner + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testPotOwnerFilterComplexExpression() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); String potOwner = "potOwner"; when(userGroupCallback.getGroupsForUser(potOwner)).thenReturn(Arrays.asList("role1", "role2")); List<String> potOwners = new ArrayList<String>(); potOwners.add(potOwner); QueryParam queryParam = new QueryParam(COL_ID, "IN", potOwners); QueryParam queryParamTerm1 = new QueryParam("MY_COLUMN", "EQUALS_TO", Arrays.asList("1234")); QueryParam queryParamTerm2 = new QueryParam("MY_COLUMN", "NOT_IN", Arrays.asList(1, 2, 3, 4)); QueryParam queryParamOR = new QueryParam(null, "OR", Arrays.asList(queryParamTerm1, queryParamTerm2, queryParam)); CoreFunctionQueryParamBuilder coreFunctionQueryParamBuilder = new CoreFunctionQueryParamBuilder(queryParamOR); ColumnFilter columnFilter = (ColumnFilter) coreFunctionQueryParamBuilder.build(); DataSetFilter filter = new DataSetFilter(); filter.addFilterColumn(columnFilter); dataSetLookup.addOperation(filter); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); List<ColumnFilter> filters = dataSetLookup.getFirstFilterOp().getColumnFilterList(); assertEquals(filters.size(), 2); assertEquals("(MY_COLUMN = 1234 OR MY_COLUMN not_in 1, 2, 3, 4)", filters.get(0).toString()); assertEquals("(POTOWNER in role1, role2, " + potOwner + ")", filters.get(1).toString()); }
@Test public void testPotOwnerFilterWithEmptyPotOwnerList() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); List<String> potOwners = new ArrayList<String>(); QueryParam queryParam = new QueryParam(COL_ID, "IN", potOwners); List<QueryParam> queryParams = new ArrayList<QueryParam>(); queryParams.add(queryParam); CoreFunctionQueryParamBuilder coreFunctionQueryParamBuilder = new CoreFunctionQueryParamBuilder(queryParam); CoreFunctionFilter columnFilter = (CoreFunctionFilter) coreFunctionQueryParamBuilder.build(); DataSetFilter filter = new DataSetFilter(); filter.addFilterColumn(columnFilter); dataSetLookup.addOperation(filter); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in " + userId + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }
@Test public void testPotOwnerFilterNoIdentityProviderAndNullGroups() { String userId = "userId"; when(userGroupCallback.getGroupsForUser(userId)).thenReturn(null); when(identityProvider.getName()).thenReturn(userId); String potOwner = "potOwner"; when(userGroupCallback.getGroupsForUser(potOwner)).thenReturn(null); List<String> potOwners = new ArrayList<String>(); potOwners.add(potOwner); QueryParam queryParam = new QueryParam(COL_ID, "IN", potOwners); List<QueryParam> queryParams = new ArrayList<QueryParam>(); queryParams.add(queryParam); CoreFunctionQueryParamBuilder coreFunctionQueryParamBuilder = new CoreFunctionQueryParamBuilder(queryParam); CoreFunctionFilter columnFilter = (CoreFunctionFilter) coreFunctionQueryParamBuilder.build(); DataSetFilter filter = new DataSetFilter(); filter.addFilterColumn(columnFilter); dataSetLookup.addOperation(filter); userAndGroupsTasksPreprocessor.preprocess(dataSetLookup); assertEquals("(POTOWNER in " + potOwner + ")", dataSetLookup.getFirstFilterOp().getColumnFilterList().get(0).toString()); }