Search in sources :

Example 21 with EntityTypeIdentity

use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.

the class AttributeRepositorySecurityDecoratorTest method countQueryUser.

@WithMockUser(username = USERNAME)
@Test
public void countQueryUser() throws Exception {
    String entityType0Name = "entity0";
    EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
    String entityType1Name = "entity1";
    EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn(entityType1Name).getMock();
    String attr0Name = "entity0attr0";
    Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
    when(attr0.getEntity()).thenReturn(entityType0);
    String attr1Name = "entity1attr0";
    Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn(attr1Name).getMock();
    when(attr1.getEntity()).thenReturn(entityType1);
    Query<Attribute> q = new QueryImpl<>();
    @SuppressWarnings("unchecked") ArgumentCaptor<Query<Attribute>> queryCaptor = forClass(Query.class);
    when(delegateRepository.findAll(queryCaptor.capture())).thenReturn(Stream.of(attr0, attr1));
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType1Name), EntityTypePermission.COUNT)).thenReturn(true);
    assertEquals(repo.count(q), 1L);
    assertEquals(queryCaptor.getValue().getOffset(), 0);
    assertEquals(queryCaptor.getValue().getPageSize(), Integer.MAX_VALUE);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) Attribute(org.molgenis.data.meta.model.Attribute) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 22 with EntityTypeIdentity

use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.

the class AttributeRepositorySecurityDecoratorTest method forEachBatchedUser.

@WithMockUser(username = USERNAME)
@Test
public void forEachBatchedUser() throws Exception {
    List<Attribute> attributes = newArrayList();
    Attribute attribute1 = mock(Attribute.class);
    Attribute attribute2 = mock(Attribute.class);
    Attribute attribute3 = mock(Attribute.class);
    Attribute attribute4 = mock(Attribute.class);
    EntityType entityType1 = mock(EntityType.class);
    EntityType entityType2 = mock(EntityType.class);
    EntityType entityType3 = mock(EntityType.class);
    EntityType entityType4 = mock(EntityType.class);
    when(attribute1.getEntity()).thenReturn(entityType1);
    when(attribute2.getEntity()).thenReturn(entityType2);
    when(attribute3.getEntity()).thenReturn(entityType3);
    when(attribute4.getEntity()).thenReturn(entityType4);
    when(entityType1.getId()).thenReturn("EntityType1");
    when(entityType2.getId()).thenReturn("EntityType2");
    when(entityType3.getId()).thenReturn("EntityType3");
    when(entityType4.getId()).thenReturn("EntityType4");
    repo.forEachBatched(attributes::addAll, 2);
    when(permissionService.hasPermission(new EntityTypeIdentity("EntityType1"), EntityTypePermission.COUNT)).thenReturn(true);
    when(permissionService.hasPermission(new EntityTypeIdentity("EntityType2"), EntityTypePermission.COUNT)).thenReturn(false);
    when(permissionService.hasPermission(new EntityTypeIdentity("EntityType3"), EntityTypePermission.COUNT)).thenReturn(false);
    when(permissionService.hasPermission(new EntityTypeIdentity("EntityType4"), EntityTypePermission.COUNT)).thenReturn(true);
    // Decorated repo returns two batches of two entityTypes
    verify(delegateRepository).forEachBatched(eq(null), consumerCaptor.capture(), eq(2));
    consumerCaptor.getValue().accept(Lists.newArrayList(attribute1, attribute2));
    consumerCaptor.getValue().accept(Lists.newArrayList(attribute3, attribute4));
    assertEquals(attributes, newArrayList(attribute1, attribute4));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Attribute(org.molgenis.data.meta.model.Attribute) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 23 with EntityTypeIdentity

use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.

the class AttributeRepositorySecurityDecoratorTest method countUser.

@WithMockUser(username = USERNAME)
@Test
public void countUser() throws Exception {
    String entityType0Name = "entity0";
    EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
    String entityType1Name = "entity1";
    EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn(entityType1Name).getMock();
    String attr0Name = "entity0attr0";
    Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
    when(attr0.getEntity()).thenReturn(entityType0);
    String attr1Name = "entity1attr0";
    Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn(attr1Name).getMock();
    when(attr1.getEntity()).thenReturn(entityType1);
    when(delegateRepository.spliterator()).thenReturn(asList(attr0, attr1).spliterator());
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
    when(permissionService.hasPermission(new EntityTypeIdentity(entityType1Name), EntityTypePermission.COUNT)).thenReturn(true);
    assertEquals(repo.count(), 1L);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Attribute(org.molgenis.data.meta.model.Attribute) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 24 with EntityTypeIdentity

use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.

the class RestControllerTest method retrieveEntityTypeWritable.

@Test
public void retrieveEntityTypeWritable() throws Exception {
    when(permissionService.hasPermission(new EntityTypeIdentity(ENTITY_NAME), EntityTypePermission.WRITE)).thenReturn(true);
    when(dataService.getCapabilities(ENTITY_NAME)).thenReturn(new HashSet<>(singletonList(RepositoryCapability.WRITABLE)));
    mockMvc.perform(get(HREF_ENTITY_META)).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8)).andExpect(content().json("{\"href\":\"" + HREF_ENTITY_META + "\",\"hrefCollection\":\"/api/v1/Person\",\"name\":\"" + ENTITY_NAME + "\",\"attributes\":{\"name\":{\"href\":\"" + HREF_ENTITY_META + "/name\"},\"id\":{\"href\":\"/api/v1/Person/meta/id\"},\"enum\":{\"href\":\"/api/v1/Person/meta/enum\"},\"int\":{\"href\":\"/api/v1/Person/meta/int\"}},\"idAttribute\":\"id\",\"isAbstract\":false,\"languageCode\":\"en\",\"writable\":true}"));
}
Also used : EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Test(org.testng.annotations.Test)

Example 25 with EntityTypeIdentity

use of org.molgenis.data.security.EntityTypeIdentity in project molgenis by molgenis.

the class RestControllerV2 method copyEntity.

/**
 * Copy an entity.
 *
 * @param entityTypeId name of the entity that will be copied.
 * @param request      CopyEntityRequestV2
 * @param response     HttpServletResponse
 * @return String name of the new entity
 */
@Transactional
@PostMapping(value = "copy/{entityTypeId}", produces = APPLICATION_JSON_VALUE)
public String copyEntity(@PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid CopyEntityRequestV2 request, HttpServletResponse response) throws Exception {
    // No repo
    if (!dataService.hasRepository(entityTypeId))
        throw createUnknownEntityException(entityTypeId);
    Repository<Entity> repositoryToCopyFrom = dataService.getRepository(entityTypeId);
    // Validate the new name
    NameValidator.validateEntityName(request.getNewEntityName());
    // Check if the entity already exists
    String newFullName = EntityTypeUtils.buildFullName(repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
    if (dataService.hasRepository(newFullName))
        throw createDuplicateEntityException(newFullName);
    // Permission
    boolean readPermission = permissionService.hasPermission(new EntityTypeIdentity(repositoryToCopyFrom.getName()), EntityTypePermission.READ);
    if (!readPermission)
        throw createNoReadPermissionOnEntityException(entityTypeId);
    // Capabilities
    boolean writableCapabilities = dataService.getCapabilities(repositoryToCopyFrom.getName()).contains(RepositoryCapability.WRITABLE);
    if (!writableCapabilities)
        throw createNoWriteCapabilitiesOnEntityException(entityTypeId);
    // Copy
    Repository<Entity> repository = this.copyRepositoryRunAsSystem(repositoryToCopyFrom, request.getNewEntityName(), repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName());
    // Retrieve new repo
    permissionSystemService.giveUserWriteMetaPermissions(repository.getEntityType());
    response.addHeader("Location", Href.concatMetaEntityHrefV2(RestControllerV2.BASE_URI, repository.getName()));
    response.setStatus(HttpServletResponse.SC_CREATED);
    return repository.getName();
}
Also used : EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)75 Test (org.testng.annotations.Test)57 EntityType (org.molgenis.data.meta.model.EntityType)40 WithMockUser (org.springframework.security.test.context.support.WithMockUser)39 Attribute (org.molgenis.data.meta.model.Attribute)16 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)8 MutableAcl (org.springframework.security.acls.model.MutableAcl)8 EntityTypePermission (org.molgenis.data.security.EntityTypePermission)6 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 PrincipalSid (org.springframework.security.acls.domain.PrincipalSid)6 Sid (org.springframework.security.acls.model.Sid)6 Entity (org.molgenis.data.Entity)5 Package (org.molgenis.data.meta.model.Package)5 EntityTypePermissionUtils.getCumulativePermission (org.molgenis.data.security.EntityTypePermissionUtils.getCumulativePermission)4 QueryImpl (org.molgenis.data.support.QueryImpl)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 File (java.io.File)3 Map (java.util.Map)3 ADD (org.molgenis.data.DatabaseAction.ADD)3 FileRepositoryCollection (org.molgenis.data.file.support.FileRepositoryCollection)3