use of nl.knaw.huygens.timbuctoo.v5.graphql.security.UserPermissionCheck in project timbuctoo by HuygensING.
the class MutationHelpers method checkAdminPermissions.
public static void checkAdminPermissions(DataFetchingEnvironment env, DataSetMetaData dataSetMetaData) throws RuntimeException {
ContextData contextData = env.getContext();
UserPermissionCheck userPermissionCheck = contextData.getUserPermissionCheck();
if (!userPermissionCheck.getPermissions(dataSetMetaData).contains(Permission.ADMIN)) {
throw new RuntimeException("You are not admin of this dataset");
}
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.security.UserPermissionCheck 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")));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.security.UserPermissionCheck 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()));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.security.UserPermissionCheck 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"))));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.security.UserPermissionCheck in project timbuctoo by HuygensING.
the class UserPermissionCheckTest method getPermissionsReturnsNoPermissionsForEmptyUserIfPublicDataSet.
@Test
public void getPermissionsReturnsNoPermissionsForEmptyUserIfPublicDataSet() {
PermissionFetcher permissionFetcher = mock(PermissionFetcher.class);
Set<Permission> defaultPermissions = Sets.newHashSet(Permission.READ);
UserPermissionCheck userPermissionCheck = new UserPermissionCheck(Optional.empty(), permissionFetcher, defaultPermissions);
DataSetMetaData dataSetMetaData = mock(BasicDataSetMetaData.class);
given(dataSetMetaData.isPublished()).willReturn(true);
Set<Permission> permissions = userPermissionCheck.getPermissions(dataSetMetaData);
assertEquals(Sets.newHashSet(Permission.READ), permissions);
}
Aggregations