Search in sources :

Example 41 with User

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

the class SchedulableTaskImplTest method taskCalledWithSetUser.

@Test
public void taskCalledWithSetUser() {
    mockNode();
    final PropertyTree taskData = new PropertyTree();
    taskData.addString("string", "value");
    final User customUser = User.create().displayName("Custom").key(PrincipalKey.from("user:system:user")).login("custom").build();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(customUser).principals(RoleKeys.AUTHENTICATED, RoleKeys.ADMIN).build();
    when(securityService.authenticate(isA(VerifiedUsernameAuthToken.class))).thenReturn(authenticationInfo);
    final SchedulableTaskImpl task = createAndRunTask(ScheduledJobName.from("task"), DescriptorKey.from("app:key"), taskData, customUser.getKey());
    assertEquals("task", task.getName());
    verify(taskService, times(1)).submitTask(taskCaptor.capture());
    assertEquals(DescriptorKey.from("app:key"), taskCaptor.getValue().getDescriptorKey());
    assertEquals(taskData, taskCaptor.getValue().getData());
}
Also used : User(com.enonic.xp.security.User) PropertyTree(com.enonic.xp.data.PropertyTree) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Example 42 with User

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

the class PrincipalNodeTranslator method createUserFromNode.

private static User createUserFromNode(final Node node) {
    Preconditions.checkNotNull(node);
    final PropertyTree nodeAsTree = node.data();
    final User.Builder user = User.create().email(nodeAsTree.getString(PrincipalPropertyNames.EMAIL_KEY)).login(nodeAsTree.getString(PrincipalPropertyNames.LOGIN_KEY)).key(PrincipalKeyNodeTranslator.toKey(node)).displayName(nodeAsTree.getString(PrincipalPropertyNames.DISPLAY_NAME_KEY)).authenticationHash(nodeAsTree.getString(PrincipalPropertyNames.AUTHENTICATION_HASH_KEY));
    createUserProfileFromNode(nodeAsTree, user);
    return user.build();
}
Also used : User(com.enonic.xp.security.User) PropertyTree(com.enonic.xp.data.PropertyTree)

Example 43 with User

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

the class SecurityServiceImplTest method testQuery.

@Test
public void testQuery() 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("123456").build();
        refresh();
        final PrincipalQuery query = PrincipalQuery.create().idProvider(IdProviderKey.system()).build();
        PrincipalQueryResult queryResult = securityService.query(query);
        queryResult = securityService.query(query);
        assertEquals(2, queryResult.getTotalSize());
        final User user1 = securityService.createUser(createUser1);
        refresh();
        queryResult = securityService.query(query);
        assertEquals(3, queryResult.getTotalSize());
        assertEquals(user1, queryResult.getPrincipals().getPrincipal(userKey1));
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) PrincipalQuery(com.enonic.xp.security.PrincipalQuery) PrincipalQueryResult(com.enonic.xp.security.PrincipalQueryResult) User(com.enonic.xp.security.User) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 44 with User

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

the class SecurityServiceImplTest method testUpdateUser.

@Test
public void testUpdateUser() throws Exception {
    runAsAdmin(() -> {
        final CreateUserParams createUser = CreateUserParams.create().userKey(PrincipalKey.ofUser(SYSTEM, "User1")).displayName("User 1").email("user1@enonic.com").login("User1").build();
        final User user = securityService.createUser(createUser);
        refresh();
        final UpdateUserParams updateUserParams = UpdateUserParams.create(user).email("u2@enonic.net").build();
        final User updateUserResult = securityService.updateUser(updateUserParams);
        refresh();
        final User updatedUser = securityService.getUser(user.getKey()).get();
        assertEquals("u2@enonic.net", updateUserResult.getEmail());
        assertEquals("u2@enonic.net", updatedUser.getEmail());
        assertEquals("User1", updatedUser.getLogin());
        assertEquals("User 1", updatedUser.getDisplayName());
        assertEquals(PrincipalKey.ofUser(SYSTEM, "User1"), updatedUser.getKey());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) UpdateUserParams(com.enonic.xp.security.UpdateUserParams) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 45 with User

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

the class SecurityServiceImplTest method testAuthenticateByUsername.

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

Aggregations

User (com.enonic.xp.security.User)63 Test (org.junit.jupiter.api.Test)40 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)22 PropertyTree (com.enonic.xp.data.PropertyTree)17 PrincipalKey (com.enonic.xp.security.PrincipalKey)17 Context (com.enonic.xp.context.Context)14 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)11 CreateUserParams (com.enonic.xp.security.CreateUserParams)10 IdProviderKey (com.enonic.xp.security.IdProviderKey)10 Node (com.enonic.xp.node.Node)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)6 UpdateUserParams (com.enonic.xp.security.UpdateUserParams)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Mockito (org.mockito.Mockito)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)4 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)4 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)4 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)4 TaskId (com.enonic.xp.task.TaskId)4