Search in sources :

Example 11 with PrincipalKey

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

the class SecurityServiceImplTest method setPassword.

@Test
public void setPassword() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("fisk").build();
        final User user = securityService.createUser(createUser1);
        refresh();
        final UsernamePasswordAuthToken authToken = new UsernamePasswordAuthToken();
        authToken.setUsername("user1");
        authToken.setPassword("runar");
        authToken.setIdProvider(SYSTEM);
        AuthenticationInfo authInfo = securityService.authenticate(authToken);
        assertFalse(authInfo.isAuthenticated());
        securityService.setPassword(user.getKey(), "runar");
        AuthenticationInfo authInfo2 = securityService.authenticate(authToken);
        assertTrue(authInfo2.isAuthenticated());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) UsernamePasswordAuthToken(com.enonic.xp.security.auth.UsernamePasswordAuthToken) PrincipalKey(com.enonic.xp.security.PrincipalKey) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 12 with PrincipalKey

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

the class SecurityServiceImplTest method testGetUserMemberships.

@Test
public void testGetUserMemberships() 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();
        securityService.createUser(createUser);
        securityService.createGroup(createGroup1);
        securityService.createGroup(createGroup2);
        securityService.addRelationship(PrincipalRelationship.from(groupKey1).to(userKey));
        securityService.addRelationship(PrincipalRelationship.from(groupKey2).to(userKey));
        refresh();
        final PrincipalKeys memberships = securityService.getMemberships(userKey);
        assertTrue(memberships.contains(groupKey1));
        assertTrue(memberships.contains(groupKey2));
        assertEquals(2, memberships.getSize());
    });
}
Also used : 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 13 with PrincipalKey

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

the class GroupNodeTranslatorTest method toGroup.

@Test
public void toGroup() throws Exception {
    final PrincipalKey groupKey = PrincipalKey.ofGroup(IdProviderKey.system(), "group-a");
    final PropertyTree rootDataSet = new PropertyTree();
    rootDataSet.setString(PrincipalPropertyNames.DISPLAY_NAME_KEY, "Group A");
    rootDataSet.setString(PrincipalPropertyNames.PRINCIPAL_TYPE_KEY, groupKey.getType().toString());
    rootDataSet.setString(PrincipalPropertyNames.ID_PROVIDER_KEY, groupKey.getIdProviderKey().toString());
    final Node node = Node.create().id(NodeId.from("id")).name(PrincipalKeyNodeTranslator.toNodeName(groupKey)).data(rootDataSet).build();
    final Group group = PrincipalNodeTranslator.groupFromNode(node);
    assertEquals(groupKey, group.getKey());
}
Also used : Group(com.enonic.xp.security.Group) PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 14 with PrincipalKey

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

the class PrincipalKeyNodeTranslatorTest method toNodeName.

@Test
public void toNodeName() throws Exception {
    PrincipalKey principalKey = PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "rmy");
    User user = User.create().key(principalKey).email("rmy@enonic.com").login("rmy").displayName("Runar Myklebust").modifiedTime(Instant.now(clock)).build();
    String nodeName = PrincipalKeyNodeTranslator.toNodeName(user.getKey()).toString();
    assertEquals("rmy", nodeName);
}
Also used : User(com.enonic.xp.security.User) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 15 with PrincipalKey

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

the class ChangePasswordHandler method changePassword.

public void changePassword() {
    final PrincipalKey principalKey = PrincipalKey.from(userKey);
    this.securityService.get().setPassword(principalKey, normalize(password));
}
Also used : PrincipalKey(com.enonic.xp.security.PrincipalKey)

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