Search in sources :

Example 6 with GraphQLFieldsContainer

use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.

the class PermissionBasedFieldVisibility method getFieldDefinitions.

@Override
public List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
    List<GraphQLFieldDefinition> graphQlFieldDefinitions = new ArrayList<>();
    Collection<DataSet> dataSets = dataSetRepository.getDataSets();
    Set<String> dataSetNamesWithOutReadPermission = dataSets.stream().filter(dataSet -> !userPermissionCheck.getPermissions(dataSet.getMetadata()).contains(Permission.READ)).map(dataSet -> dataSet.getMetadata().getCombinedId()).collect(Collectors.toSet());
    Iterator<GraphQLFieldDefinition> graphQlFieldDefinitionIterator = fieldsContainer.getFieldDefinitions().iterator();
    while (graphQlFieldDefinitionIterator.hasNext()) {
        GraphQLFieldDefinition graphQlFieldDefinition = graphQlFieldDefinitionIterator.next();
        if (!dataSetNamesWithOutReadPermission.contains(graphQlFieldDefinition.getName())) {
            graphQlFieldDefinitions.add(graphQlFieldDefinition);
        }
    }
    return graphQlFieldDefinitions;
}
Also used : List(java.util.List) Iterator(java.util.Iterator) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) Collection(java.util.Collection) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Set(java.util.Set) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) Collectors(java.util.stream.Collectors) Permission(nl.knaw.huygens.timbuctoo.v5.security.dto.Permission) ArrayList(java.util.ArrayList) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition)

Example 7 with GraphQLFieldsContainer

use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.

the class PermissionBasedFieldVisibilityTest method getFieldDefinitionReturnsFieldDefinitionIfUserHasPermission.

@Test
public void getFieldDefinitionReturnsFieldDefinitionIfUserHasPermission() throws Exception {
    final DataSetRepository dataSetRepository = mock(DataSetRepository.class);
    DataSet dataSet = createDataSetWithUserPermissions("user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ));
    DataSet dataSet2 = createDataSetWithUserPermissions("user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet());
    Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2);
    given(dataSetRepository.getDataSets()).willReturn(dataSetCollection);
    final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository);
    final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer("user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo");
    GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility.getFieldDefinition(graphQlFieldsContainer, "user__dataSetUserHasAccessTo");
    assertThat(retrievedGraphQlFieldDefinition, hasProperty("name", is("user__dataSetUserHasAccessTo")));
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Test(org.junit.Test)

Example 8 with GraphQLFieldsContainer

use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.

the class PermissionBasedFieldVisibilityTest method createGraphQlFieldsContainer.

private GraphQLFieldsContainer createGraphQlFieldsContainer(String... fieldNames) {
    List<GraphQLFieldDefinition> graphQlFieldDefinitions = new ArrayList<>();
    final GraphQLFieldsContainer graphQlFieldsContainer = mock(GraphQLFieldsContainer.class);
    for (String fieldName : fieldNames) {
        GraphQLFieldDefinition graphQlFieldDefinition = createGraphQlFieldDefinition(fieldName);
        graphQlFieldDefinitions.add(graphQlFieldDefinition);
        given(graphQlFieldsContainer.getFieldDefinition(fieldName)).willReturn(graphQlFieldDefinition);
    }
    given(graphQlFieldsContainer.getFieldDefinitions()).willReturn(graphQlFieldDefinitions);
    return graphQlFieldsContainer;
}
Also used : ArrayList(java.util.ArrayList) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer)

Example 9 with GraphQLFieldsContainer

use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.

the class PermissionBasedFieldVisibilityTest method getFieldDefinitionReturnsNullIfUserHasNoPermission.

@Test
public void getFieldDefinitionReturnsNullIfUserHasNoPermission() throws Exception {
    final DataSetRepository dataSetRepository = mock(DataSetRepository.class);
    DataSet dataSet = createDataSetWithUserPermissions("user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ));
    DataSet dataSet2 = createDataSetWithUserPermissions("user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet());
    Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2);
    given(dataSetRepository.getDataSets()).willReturn(dataSetCollection);
    final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository);
    final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer("user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo");
    GraphQLFieldDefinition retrievedGraphQlFieldDefinition = permissionBasedFieldVisibility.getFieldDefinition(graphQlFieldsContainer, "user__dataSetUserDoesNotHasAccessTo");
    assertThat(retrievedGraphQlFieldDefinition, is(nullValue()));
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Test(org.junit.Test)

Example 10 with GraphQLFieldsContainer

use of graphql.schema.GraphQLFieldsContainer in project timbuctoo by HuygensING.

the class PermissionBasedFieldVisibilityTest method getFieldDefinitionsShowsOnlyDataSetsThatUserHasAccessTo.

@Test
public void getFieldDefinitionsShowsOnlyDataSetsThatUserHasAccessTo() throws Exception {
    final DataSetRepository dataSetRepository = mock(DataSetRepository.class);
    DataSet dataSet = createDataSetWithUserPermissions("user__dataSetUserHasAccessTo", Sets.newHashSet(Permission.READ));
    DataSet dataSet2 = createDataSetWithUserPermissions("user__dataSetUserDoesNotHasAccessTo", Sets.newHashSet());
    Collection<DataSet> dataSetCollection = Sets.newHashSet(dataSet, dataSet2);
    given(dataSetRepository.getDataSets()).willReturn(dataSetCollection);
    final PermissionBasedFieldVisibility permissionBasedFieldVisibility = new PermissionBasedFieldVisibility(userPermissionCheck, dataSetRepository);
    final GraphQLFieldsContainer graphQlFieldsContainer = createGraphQlFieldsContainer("user__dataSetUserHasAccessTo", "user__dataSetUserDoesNotHasAccessTo");
    List<GraphQLFieldDefinition> retrievedGraphQlFieldDefinitions = permissionBasedFieldVisibility.getFieldDefinitions(graphQlFieldsContainer);
    assertThat(retrievedGraphQlFieldDefinitions, contains(hasProperty("name", is("user__dataSetUserHasAccessTo"))));
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) DataSetRepository(nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Test(org.junit.Test)

Aggregations

GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)10 GraphQLFieldsContainer (graphql.schema.GraphQLFieldsContainer)10 DataSetRepository (nl.knaw.huygens.timbuctoo.v5.dataset.DataSetRepository)7 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Permission (nl.knaw.huygens.timbuctoo.v5.security.dto.Permission)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)1