Search in sources :

Example 6 with PrincipalRelationships

use of com.enonic.xp.security.PrincipalRelationships 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)

Example 7 with PrincipalRelationships

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

the class GetMembersHandler method getMembers.

public List<PrincipalMapper> getMembers() {
    final PrincipalRelationships relationships = this.securityService.get().getRelationships(this.principalKey);
    final PrincipalKeys principalKeys = PrincipalKeys.from(relationships.stream().map(PrincipalRelationship::getTo).toArray(PrincipalKey[]::new));
    final Principals principals = this.securityService.get().getPrincipals(principalKeys);
    return principals.stream().map(PrincipalMapper::new).collect(Collectors.toList());
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) Principals(com.enonic.xp.security.Principals) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships)

Example 8 with PrincipalRelationships

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

the class GetMembersHandlerTest method testGetMembers.

@Test
public void testGetMembers() {
    final Group group = TestDataFixtures.getTestGroup();
    final User user1 = TestDataFixtures.getTestUser();
    final User user2 = TestDataFixtures.getTestUser2();
    final PrincipalKeys principalKeys = PrincipalKeys.from(user1.getKey(), user2.getKey());
    final PrincipalRelationships relationships = PrincipalRelationships.from(PrincipalRelationship.from(group.getKey()).to(user1.getKey()), PrincipalRelationship.from(group.getKey()).to(user2.getKey()));
    Mockito.when(securityService.getRelationships(group.getKey())).thenReturn(relationships);
    Mockito.when(securityService.getPrincipals(principalKeys)).thenReturn(Principals.from(user1, user2));
    runFunction("/test/getMembers-test.js", "getMembers");
}
Also used : Group(com.enonic.xp.security.Group) User(com.enonic.xp.security.User) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) Test(org.junit.jupiter.api.Test)

Example 9 with PrincipalRelationships

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

the class GetMembersHandlerTest method testExamples.

@Test
public void testExamples() {
    final Group group = TestDataFixtures.getTestGroup();
    final User user1 = TestDataFixtures.getTestUser();
    final User user2 = TestDataFixtures.getTestUser2();
    final PrincipalKeys principalKeys = PrincipalKeys.from(user1.getKey(), user2.getKey());
    final PrincipalRelationships relationships = PrincipalRelationships.from(PrincipalRelationship.from(group.getKey()).to(user1.getKey()), PrincipalRelationship.from(group.getKey()).to(user2.getKey()));
    Mockito.when(securityService.getRelationships(group.getKey())).thenReturn(relationships);
    Mockito.when(securityService.getPrincipals(principalKeys)).thenReturn(Principals.from(user1, user2));
    runScript("/lib/xp/examples/auth/getMembers.js");
}
Also used : Group(com.enonic.xp.security.Group) User(com.enonic.xp.security.User) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) Test(org.junit.jupiter.api.Test)

Aggregations

PrincipalRelationships (com.enonic.xp.security.PrincipalRelationships)9 Test (org.junit.jupiter.api.Test)7 PrincipalKey (com.enonic.xp.security.PrincipalKey)5 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)4 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)4 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)3 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)3 CreateUserParams (com.enonic.xp.security.CreateUserParams)3 Group (com.enonic.xp.security.Group)3 User (com.enonic.xp.security.User)3 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)1 RepositoryId (com.enonic.xp.repository.RepositoryId)1 Principals (com.enonic.xp.security.Principals)1