Search in sources :

Example 1 with PrincipalRelationship

use of com.enonic.xp.security.PrincipalRelationship in project xp by enonic.

the class PrincipalNodeTranslator method relationshipsFromNode.

static PrincipalRelationships relationshipsFromNode(final Node node) {
    final PropertyTree rootDataSet = node.data();
    final List<Property> members = rootDataSet.getProperties(PrincipalPropertyNames.MEMBER_KEY);
    if (members == null || members.isEmpty()) {
        return PrincipalRelationships.empty();
    }
    final ImmutableList.Builder<PrincipalRelationship> relationships = ImmutableList.builder();
    final PrincipalKey relationshipFrom = PrincipalKeyNodeTranslator.toKey(node);
    for (Property member : members) {
        final String memberKey = member.getValue().asString();
        final PrincipalKey relationshipTo = PrincipalKey.from(memberKey);
        final PrincipalRelationship relationship = PrincipalRelationship.from(relationshipFrom).to(relationshipTo);
        relationships.add(relationship);
    }
    return PrincipalRelationships.from(relationships.build());
}
Also used : PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) ImmutableList(com.google.common.collect.ImmutableList) PropertyTree(com.enonic.xp.data.PropertyTree) Property(com.enonic.xp.data.Property) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 2 with PrincipalRelationship

use of com.enonic.xp.security.PrincipalRelationship in project xp by enonic.

the class SecurityServiceImpl method doRemoveMemberships.

private void doRemoveMemberships(final PrincipalKey member) {
    final PrincipalKeys memberships = queryDirectMemberships(member);
    if (memberships.isEmpty()) {
        return;
    }
    for (PrincipalKey from : memberships) {
        final PrincipalRelationship relationship = PrincipalRelationship.from(from).to(member);
        final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.removeRelationshipToUpdateNodeParams(relationship);
        nodeService.update(updateNodeParams);
    }
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 3 with PrincipalRelationship

use of com.enonic.xp.security.PrincipalRelationship in project xp by enonic.

the class SecurityServiceImplTest method testRemoveRelationship.

@Test
public void testRemoveRelationship() throws Exception {
    runAsAdmin(() -> {
        // set up
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("123456").build();
        final PrincipalKey userKey2 = PrincipalKey.ofUser(SYSTEM, "user2");
        final CreateUserParams createUser2 = CreateUserParams.create().userKey(userKey2).displayName("User 2").email("user2@enonic.com").login("user2").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
        final CreateGroupParams createGroup = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        securityService.createUser(createUser1);
        securityService.createUser(createUser2);
        securityService.createGroup(createGroup);
        refresh();
        PrincipalRelationship membership = PrincipalRelationship.from(groupKey1).to(userKey1);
        PrincipalRelationship membership2 = PrincipalRelationship.from(groupKey1).to(userKey2);
        securityService.addRelationship(membership);
        securityService.addRelationship(membership2);
        refresh();
        // exercise
        securityService.removeRelationship(membership);
        refresh();
        // verify
        final PrincipalRelationships relationships = securityService.getRelationships(groupKey1);
        assertEquals(1, relationships.getSize());
        assertEquals(membership2, relationships.get(0));
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with PrincipalRelationship

use of com.enonic.xp.security.PrincipalRelationship in project xp by enonic.

the class SecurityServiceImplTest method testRemoveAllRelationships.

@Test
public void testRemoveAllRelationships() throws Exception {
    runAsAdmin(() -> {
        // set up
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("123456").build();
        final PrincipalKey userKey2 = PrincipalKey.ofUser(SYSTEM, "user2");
        final CreateUserParams createUser2 = CreateUserParams.create().userKey(userKey2).displayName("User 2").email("user2@enonic.com").login("user2").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
        final CreateGroupParams createGroup = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        securityService.createUser(createUser1);
        securityService.createUser(createUser2);
        securityService.createGroup(createGroup);
        refresh();
        PrincipalRelationship membership = PrincipalRelationship.from(groupKey1).to(userKey1);
        PrincipalRelationship membership2 = PrincipalRelationship.from(groupKey1).to(userKey2);
        securityService.addRelationship(membership);
        securityService.addRelationship(membership2);
        refresh();
        // exercise
        securityService.removeRelationships(groupKey1);
        refresh();
        // verify
        final PrincipalRelationships relationships = securityService.getRelationships(groupKey1);
        assertEquals(0, relationships.getSize());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with PrincipalRelationship

use of com.enonic.xp.security.PrincipalRelationship in project xp by enonic.

the class SecurityServiceImplTest method testAddRelationship.

@Test
public void testAddRelationship() throws Exception {
    runAsAdmin(() -> {
        // set up
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("123456").build();
        final PrincipalKey userKey2 = PrincipalKey.ofUser(SYSTEM, "user2");
        final CreateUserParams createUser2 = CreateUserParams.create().userKey(userKey2).displayName("User 2").email("user2@enonic.com").login("user2").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
        final CreateGroupParams createGroup = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        securityService.createUser(createUser1);
        securityService.createUser(createUser2);
        securityService.createGroup(createGroup);
        PrincipalRelationship membership = PrincipalRelationship.from(groupKey1).to(userKey1);
        PrincipalRelationship membership2 = PrincipalRelationship.from(groupKey1).to(userKey2);
        // exercise
        securityService.addRelationship(membership);
        securityService.addRelationship(membership2);
        securityService.addRelationship(membership);
        refresh();
        // verify
        final PrincipalRelationships relationships = securityService.getRelationships(groupKey1);
        assertEquals(2, relationships.getSize());
        assertEquals(membership, relationships.get(0));
        assertEquals(membership2, relationships.get(1));
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

PrincipalKey (com.enonic.xp.security.PrincipalKey)6 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)6 PrincipalRelationships (com.enonic.xp.security.PrincipalRelationships)4 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)3 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)3 CreateUserParams (com.enonic.xp.security.CreateUserParams)3 Test (org.junit.jupiter.api.Test)3 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)2 Property (com.enonic.xp.data.Property)1 PropertyTree (com.enonic.xp.data.PropertyTree)1 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)1 Principals (com.enonic.xp.security.Principals)1 ImmutableList (com.google.common.collect.ImmutableList)1