Search in sources :

Example 31 with EntityTypeIdentity

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

the class PermissionRegistryImpl method getPermissions.

@Override
public Multimap<ObjectIdentity, Pair<Permission, Sid>> getPermissions() {
    ImmutableMultimap.Builder<ObjectIdentity, Pair<Permission, Sid>> mapBuilder = new ImmutableMultimap.Builder<>();
    Group allUsersGroup = dataService.query(GROUP, Group.class).eq(NAME, ALL_USER_GROUP).findOne();
    Sid allUsersGroupSid = createSid(allUsersGroup);
    ObjectIdentity pluginIdentity = new PluginIdentity(UserAccountController.ID);
    mapBuilder.putAll(pluginIdentity, new Pair<>(PluginPermission.READ, allUsersGroupSid));
    dataService.findAll(ENTITY_TYPE_META_DATA, Stream.of(ENTITY_TYPE_META_DATA, ATTRIBUTE_META_DATA, PACKAGE, TAG, LANGUAGE, L10N_STRING, FILE_META, DECORATOR_CONFIGURATION), EntityType.class).forEach(entityType -> {
        ObjectIdentity entityTypeIdentity = new EntityTypeIdentity(entityType);
        Permission entityTypePermissions = EntityTypePermissionUtils.getCumulativePermission(EntityTypePermission.READ);
        mapBuilder.putAll(entityTypeIdentity, new Pair<>(entityTypePermissions, allUsersGroupSid));
    });
    return mapBuilder.build();
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Group(org.molgenis.data.security.auth.Group) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) ObjectIdentity(org.springframework.security.acls.model.ObjectIdentity) PluginIdentity(org.molgenis.data.plugin.model.PluginIdentity) Permission(org.springframework.security.acls.model.Permission) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) PluginPermission(org.molgenis.data.plugin.model.PluginPermission) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Pair(org.molgenis.util.Pair) SidUtils.createSid(org.molgenis.security.acl.SidUtils.createSid) Sid(org.springframework.security.acls.model.Sid)

Example 32 with EntityTypeIdentity

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

the class EmxImportServiceIT method populateUserPermissions.

private void populateUserPermissions() {
    Sid sid = new PrincipalSid(SecurityUtils.getCurrentUsername());
    Map<String, EntityTypePermission> entityTypePermissionMap = new HashMap<>();
    entityTypePermissionMap.put("sys_md_Package", READ);
    entityTypePermissionMap.put("sys_md_EntityType", READ);
    entityTypePermissionMap.put("sys_md_Attribute", READ);
    entityTypePermissionMap.put("sys_md_Tag", READ);
    entityTypePermissionMap.put("sys_FileMeta", READ);
    entityTypePermissionMap.put("sys_dec_DecoratorConfiguration", READ);
    runAsSystem(() -> entityTypePermissionMap.forEach((entityTypeId, permission) -> {
        MutableAcl acl = (MutableAcl) mutableAclService.readAclById(new EntityTypeIdentity(entityTypeId));
        acl.insertAce(acl.getEntries().size(), getCumulativePermission(permission), sid, true);
        mutableAclService.updateAcl(acl);
    }));
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) DataProvider(org.testng.annotations.DataProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.testng.annotations.Test) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) User(org.molgenis.data.security.auth.User) Tag(org.molgenis.data.meta.model.Tag) Assert(org.testng.Assert) PACKAGE_SEPARATOR(org.molgenis.data.meta.model.Package.PACKAGE_SEPARATOR) Arrays.asList(java.util.Arrays.asList) PACKAGE_DEFAULT(org.molgenis.data.meta.DefaultPackage.PACKAGE_DEFAULT) MutableAcl(org.springframework.security.acls.model.MutableAcl) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Collectors.toSet(java.util.stream.Collectors.toSet) ImportService(org.molgenis.data.importer.ImportService) Sid(org.springframework.security.acls.model.Sid) Collections.emptyMap(java.util.Collections.emptyMap) ADD(org.molgenis.data.DatabaseAction.ADD) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) ADD_UPDATE_EXISTING(org.molgenis.data.DatabaseAction.ADD_UPDATE_EXISTING) EntityTypePermissionUtils.getCumulativePermission(org.molgenis.data.security.EntityTypePermissionUtils.getCumulativePermission) EntityImportReport(org.molgenis.data.importer.EntityImportReport) READ(org.molgenis.data.security.EntityTypePermission.READ) Instant(java.time.Instant) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) File(java.io.File) FileRepositoryCollection(org.molgenis.data.file.support.FileRepositoryCollection) SecurityUtils(org.molgenis.security.core.utils.SecurityUtils) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LocalDate(java.time.LocalDate) Package(org.molgenis.data.meta.model.Package) MutableAclService(org.springframework.security.acls.model.MutableAclService) Entity(org.molgenis.data.Entity) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) MutableAcl(org.springframework.security.acls.model.MutableAcl) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) Sid(org.springframework.security.acls.model.Sid) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid)

Example 33 with EntityTypeIdentity

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

the class QuestionnaireServiceTest method testGetQuestionnaires.

@Test
public void testGetQuestionnaires() {
    // =========== Setup ===========
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn(QUESTIONNAIRE_ID);
    when(entityType.getLabel()).thenReturn("label");
    when(entityType.getDescription()).thenReturn("description");
    Query<EntityType> typedQuery = mock(Query.class);
    Query<EntityType> query = mock(Query.class);
    when(typedQuery.eq(EntityTypeMetadata.EXTENDS, QUESTIONNAIRE)).thenReturn(query);
    when(dataService.query(ENTITY_TYPE_META_DATA, EntityType.class)).thenReturn(typedQuery);
    when(query.findAll()).thenReturn(Stream.of(entityType));
    when(userPermissionEvaluator.hasPermission(new EntityTypeIdentity(QUESTIONNAIRE_ID), WRITE)).thenReturn(true);
    Entity entity = mock(Entity.class);
    when(dataService.findOne(QUESTIONNAIRE_ID, EQ(OWNER_USERNAME, null))).thenReturn(entity);
    Questionnaire questionnaire = mock(Questionnaire.class);
    when(questionnaire.getStatus()).thenReturn(OPEN);
    when(questionnaireFactory.create(entity)).thenReturn(questionnaire);
    // =========== Test ===========
    List<QuestionnaireResponse> actual = questionnaireService.getQuestionnaires();
    QuestionnaireResponse questionnaireResponse = QuestionnaireResponse.create(QUESTIONNAIRE_ID, "label", "description", OPEN);
    List<QuestionnaireResponse> expected = newArrayList(questionnaireResponse);
    assertEquals(actual, expected);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) Entity(org.molgenis.data.Entity) Questionnaire(org.molgenis.questionnaires.meta.Questionnaire) QuestionnaireResponse(org.molgenis.questionnaires.response.QuestionnaireResponse) Test(org.testng.annotations.Test)

Example 34 with EntityTypeIdentity

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

the class PermissionManagerControllerTest method setUp.

@BeforeMethod
public void setUp() {
    config.resetMocks();
    mockMvc = MockMvcBuilders.standaloneSetup(permissionManagerController).setMessageConverters(gsonHttpMessageConverter).build();
    user1 = when(mock(User.class).getId()).thenReturn("1").getMock();
    when(user1.isSuperuser()).thenReturn(true);
    when(user1.getUsername()).thenReturn("Ipsum");
    userSid = new PrincipalSid("Ipsum");
    user2 = when(mock(User.class).getId()).thenReturn("2").getMock();
    group1 = when(mock(Group.class).getId()).thenReturn("1").getMock();
    groupSid = new GrantedAuthoritySid("ROLE_1");
    group2 = when(mock(Group.class).getId()).thenReturn("2").getMock();
    plugin1 = when(mock(Plugin.class).getId()).thenReturn("1").getMock();
    plugin2 = when(mock(Plugin.class).getId()).thenReturn("2").getMock();
    pluginIdentity1 = new PluginIdentity(plugin1);
    pluginIdentity2 = new PluginIdentity(plugin2);
    entityType1 = when(mock(EntityType.class).getId()).thenReturn("1").getMock();
    entityType2 = when(mock(EntityType.class).getId()).thenReturn("2").getMock();
    entityType3 = when(mock(EntityType.class).getId()).thenReturn("3").getMock();
    when(entityType1.getLabel()).thenReturn("label1");
    when(entityType2.getLabel()).thenReturn("label2");
    when(entityType3.getLabel()).thenReturn("label3");
    entityIdentity1 = new EntityTypeIdentity(entityType1);
    entityIdentity2 = new EntityTypeIdentity(entityType2);
    entityIdentity3 = new EntityTypeIdentity(entityType3);
    package1 = when(mock(Package.class).getId()).thenReturn("1").getMock();
    package2 = when(mock(Package.class).getId()).thenReturn("2").getMock();
    package3 = when(mock(Package.class).getId()).thenReturn("3").getMock();
    packageIdentity1 = new PackageIdentity(package1);
    packageIdentity2 = new PackageIdentity(package2);
    packageIdentity3 = new PackageIdentity(package3);
    when(dataService.findAll(USER, User.class)).thenReturn(Stream.of(user1, user2));
    when(dataService.findAll(GROUP, Group.class)).thenReturn(Stream.of(group1, group2));
    when(dataService.findOneById(GROUP, "1", Group.class)).thenReturn(group1);
    when(dataService.findOneById(USER, "1", User.class)).thenReturn(user1);
    when(dataService.findAll(PLUGIN, Plugin.class)).thenReturn(Stream.of(plugin1, plugin2));
    when(dataService.findAll(ENTITY_TYPE_META_DATA, EntityType.class)).thenReturn(Stream.of(entityType1, entityType2, entityType3));
    when(dataService.findAll(PACKAGE, Package.class)).thenReturn(Stream.of(package1, package2, package3));
    pluginPermissionRead = PluginPermission.READ;
    cumulativeEntityPermissionWritemeta = new CumulativePermission();
    cumulativeEntityPermissionWritemeta.set(EntityTypePermission.WRITEMETA).set(EntityTypePermission.WRITE).set(EntityTypePermission.READ).set(EntityTypePermission.COUNT);
    cumulativeEntityPermissionWrite = new CumulativePermission();
    cumulativeEntityPermissionWrite.set(EntityTypePermission.WRITE).set(EntityTypePermission.READ).set(EntityTypePermission.COUNT);
    cumulativeEntityPermissionRead = new CumulativePermission();
    cumulativeEntityPermissionRead.set(EntityTypePermission.READ).set(EntityTypePermission.COUNT);
    cumulativeEntityPermissionCount = new CumulativePermission();
    cumulativeEntityPermissionCount.set(EntityTypePermission.COUNT);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Group(org.molgenis.data.security.auth.Group) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) User(org.molgenis.data.security.auth.User) GrantedAuthoritySid(org.springframework.security.acls.domain.GrantedAuthoritySid) PluginIdentity(org.molgenis.data.plugin.model.PluginIdentity) CumulativePermission(org.springframework.security.acls.domain.CumulativePermission) PackageIdentity(org.molgenis.data.security.PackageIdentity) Package(org.molgenis.data.meta.model.Package) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) Plugin(org.molgenis.data.plugin.model.Plugin) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 35 with EntityTypeIdentity

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

the class PermissionManagerControllerTest method testUpdateUserEntityClassPermissions.

@Test
public void testUpdateUserEntityClassPermissions() {
    WebRequest webRequest = mock(WebRequest.class);
    when(webRequest.getParameter("radio-1")).thenReturn("write");
    when(webRequest.getParameter("radio-2")).thenReturn("none");
    when(webRequest.getParameter("radio-3")).thenReturn("read");
    ObjectIdentity objectIdentity1 = new EntityTypeIdentity(entityType1);
    ObjectIdentity objectIdentity2 = new EntityTypeIdentity(entityType2);
    ObjectIdentity objectIdentity3 = new EntityTypeIdentity(entityType3);
    MutableAcl acl1 = mock(MutableAcl.class);
    MutableAcl acl2 = mock(MutableAcl.class);
    MutableAcl acl3 = mock(MutableAcl.class);
    AccessControlEntry ace1 = mock(AccessControlEntry.class);
    AccessControlEntry ace2 = mock(AccessControlEntry.class);
    PrincipalSid sid = new PrincipalSid("Ipsum");
    when(ace1.getSid()).thenReturn(sid);
    when(ace2.getSid()).thenReturn(sid);
    when(acl1.getEntries()).thenReturn(Collections.singletonList(ace1));
    when(acl2.getEntries()).thenReturn(Collections.singletonList(ace2));
    when(acl3.getEntries()).thenReturn(Collections.emptyList());
    when(mutableAclService.readAclById(objectIdentity1, singletonList(sid))).thenReturn(acl1);
    when(mutableAclService.readAclById(objectIdentity2, singletonList(sid))).thenReturn(acl2);
    when(mutableAclService.readAclById(objectIdentity3, singletonList(sid))).thenReturn(acl3);
    permissionManagerController.updateUserEntityClassPermissions("1", webRequest);
    verify(acl1).deleteAce(0);
    verify(acl1).insertAce(0, cumulativeEntityPermissionWrite, sid, true);
    verify(acl2).deleteAce(0);
    verify(acl3).insertAce(0, cumulativeEntityPermissionRead, sid, true);
    verify(mutableAclService).updateAcl(acl1);
    verify(mutableAclService).updateAcl(acl2);
    verify(mutableAclService).updateAcl(acl3);
}
Also used : EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) WebRequest(org.springframework.web.context.request.WebRequest) PrincipalSid(org.springframework.security.acls.domain.PrincipalSid) Test(org.testng.annotations.Test)

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