Search in sources :

Example 71 with PrincipalKey

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

the class SecurityServiceImplTest method testCreateIdProvider.

@Test
public void testCreateIdProvider() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey userKey = PrincipalKey.ofUser(SYSTEM, "User1");
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "group-a");
        final PrincipalKey groupKey2 = PrincipalKey.ofGroup(SYSTEM, "group-b");
        final IdProviderAccessControlList permissions = IdProviderAccessControlList.of(IdProviderAccessControlEntry.create().principal(userKey).access(CREATE_USERS).build(), IdProviderAccessControlEntry.create().principal(groupKey1).access(ADMINISTRATOR).build(), IdProviderAccessControlEntry.create().principal(groupKey2).access(WRITE_USERS).build());
        final CreateIdProviderParams createIdProvider = CreateIdProviderParams.create().key(IdProviderKey.from("enonic")).displayName("Enonic Id Provider").permissions(permissions).description("id provider description").build();
        final IdProvider idProviderCreated = securityService.createIdProvider(createIdProvider);
        assertNotNull(idProviderCreated);
        assertEquals("enonic", idProviderCreated.getKey().toString());
        assertEquals("Enonic Id Provider", idProviderCreated.getDisplayName());
        assertEquals("id provider description", idProviderCreated.getDescription());
        final IdProviderAccessControlList createdPermissions = securityService.getIdProviderPermissions(IdProviderKey.from("enonic"));
        assertNotNull(idProviderCreated);
        assertEquals(CREATE_USERS, createdPermissions.getEntry(userKey).getAccess());
        assertEquals(ADMINISTRATOR, createdPermissions.getEntry(groupKey1).getAccess());
        assertEquals(WRITE_USERS, createdPermissions.getEntry(groupKey2).getAccess());
    });
}
Also used : IdProviderAccessControlList(com.enonic.xp.security.acl.IdProviderAccessControlList) IdProvider(com.enonic.xp.security.IdProvider) PrincipalKey(com.enonic.xp.security.PrincipalKey) CreateIdProviderParams(com.enonic.xp.security.CreateIdProviderParams) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 72 with PrincipalKey

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

the class SecurityServiceImplTest method testCreateGroup.

@Test
public void testCreateGroup() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
        final CreateGroupParams createGroup = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").description("Group A Description").build();
        final PrincipalKey groupKey2 = PrincipalKey.ofGroup(SYSTEM, "group-b");
        final CreateGroupParams createGroup2 = CreateGroupParams.create().groupKey(groupKey2).displayName("Group B").build();
        final Group group1 = securityService.createGroup(createGroup);
        final Group group2 = securityService.createGroup(createGroup2);
        refresh();
        final Group createdGroup1 = securityService.getGroup(groupKey1).get();
        final Group createdGroup2 = securityService.getGroup(groupKey2).get();
        assertEquals("Group A", group1.getDisplayName());
        assertEquals("Group A", createdGroup1.getDisplayName());
        assertEquals("Group A Description", group1.getDescription());
        assertEquals("Group A Description", createdGroup1.getDescription());
        assertEquals("Group B", group2.getDisplayName());
        assertEquals("Group B", createdGroup2.getDisplayName());
        assertNull(group2.getDescription());
        assertNull(createdGroup2.getDescription());
    });
}
Also used : Group(com.enonic.xp.security.Group) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 73 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey 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 74 with PrincipalKey

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

the class SecurityServiceImplTest method testGetAllMemberships.

@Test
public void testGetAllMemberships() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey userKey = PrincipalKey.ofUser(SYSTEM, "user1");
        final CreateUserParams createUser = CreateUserParams.create().userKey(userKey).displayName("User 1").email("user1@enonic.com").login("user1").password("123456").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "group-a");
        final CreateGroupParams createGroup1 = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        final PrincipalKey groupKey2 = PrincipalKey.ofGroup(SYSTEM, "group-b");
        final CreateGroupParams createGroup2 = CreateGroupParams.create().groupKey(groupKey2).displayName("Group B").build();
        final PrincipalKey roleKey1 = PrincipalKey.ofRole("role-a");
        final CreateRoleParams createRole = CreateRoleParams.create().roleKey(roleKey1).displayName("Role A").description("Group A Description").build();
        securityService.createUser(createUser);
        securityService.createGroup(createGroup1);
        securityService.createGroup(createGroup2);
        securityService.createRole(createRole);
        securityService.addRelationship(PrincipalRelationship.from(groupKey1).to(userKey));
        securityService.addRelationship(PrincipalRelationship.from(groupKey2).to(groupKey1));
        securityService.addRelationship(PrincipalRelationship.from(roleKey1).to(groupKey2));
        refresh();
        final PrincipalKeys memberships = securityService.getAllMemberships(userKey);
        assertTrue(memberships.contains(groupKey1));
        assertTrue(memberships.contains(groupKey2));
        assertTrue(memberships.contains(roleKey1));
        assertEquals(3, memberships.getSize());
    });
}
Also used : CreateRoleParams(com.enonic.xp.security.CreateRoleParams) CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 75 with PrincipalKey

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

the class PrincipalKeyNodeTranslatorTest method userKeyFromNode.

@Test
public void userKeyFromNode() throws Exception {
    PropertyTree rootDataSet = new PropertyTree();
    rootDataSet.setString(PrincipalPropertyNames.PRINCIPAL_TYPE_KEY, PrincipalType.USER.toString());
    rootDataSet.setString(PrincipalPropertyNames.ID_PROVIDER_KEY, IdProviderKey.system().toString());
    Node userNode = Node.create().data(rootDataSet).name(NodeName.from("rmy")).build();
    PrincipalKey principalKey = PrincipalKeyNodeTranslator.toKey(userNode);
    assertTrue(principalKey.isUser());
    assertEquals(PrincipalType.USER, principalKey.getType());
    assertEquals(IdProviderKey.system(), principalKey.getIdProviderKey());
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Aggregations

PrincipalKey (com.enonic.xp.security.PrincipalKey)84 Test (org.junit.jupiter.api.Test)47 PropertyTree (com.enonic.xp.data.PropertyTree)22 User (com.enonic.xp.security.User)18 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)15 CreateUserParams (com.enonic.xp.security.CreateUserParams)14 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)14 Instant (java.time.Instant)12 Node (com.enonic.xp.node.Node)10 DescriptorKey (com.enonic.xp.page.DescriptorKey)10 Context (com.enonic.xp.context.Context)9 IdProviderKey (com.enonic.xp.security.IdProviderKey)9 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)9 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)8 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)8 NodeId (com.enonic.xp.node.NodeId)7 NodeQuery (com.enonic.xp.node.NodeQuery)7 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)7 Group (com.enonic.xp.security.Group)6 AccessControlList (com.enonic.xp.security.acl.AccessControlList)6