Search in sources :

Example 46 with User

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

the class SecurityServiceImplTest method testCreateUser.

@Test
public void testCreateUser() 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();
        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 User user1 = securityService.createUser(createUser1);
        final User user2 = securityService.createUser(createUser2);
        refresh();
        final User createdUser1 = securityService.getUser(userKey1).get();
        final User createdUser2 = securityService.getUser(userKey2).get();
        assertEquals("User 1", user1.getDisplayName());
        assertEquals("user1@enonic.com", user1.getEmail());
        assertEquals("User1", user1.getLogin());
        assertEquals("User 1", createdUser1.getDisplayName());
        assertEquals("user1@enonic.com", createdUser1.getEmail());
        assertEquals("User1", createdUser1.getLogin());
        assertEquals("User 2", user2.getDisplayName());
        assertEquals("user2@enonic.com", user2.getEmail());
        assertEquals("user2", user2.getLogin());
        assertEquals("User 2", createdUser2.getDisplayName());
        assertEquals("user2@enonic.com", createdUser2.getEmail());
        assertEquals("user2", createdUser2.getLogin());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) 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 47 with User

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

the class SecurityInitializer method createAdminContext.

private Context createAdminContext() {
    final User admin = User.create().key(SUPER_USER).login(SUPER_USER.getId()).build();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().principals(RoleKeys.ADMIN).user(admin).build();
    return ContextBuilder.create().branch(SecurityConstants.BRANCH_SECURITY).repositoryId(SystemConstants.SYSTEM_REPO_ID).authInfo(authInfo).build();
}
Also used : User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 48 with User

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

the class SecurityServiceImpl method doUpdateUser.

private User doUpdateUser(final UpdateUserParams updateUserParams) {
    return callWithContext(() -> {
        final Node node = this.nodeService.getByPath(updateUserParams.getKey().toPath());
        if (node == null) {
            throw new PrincipalNotFoundException(updateUserParams.getKey());
        }
        final User existingUser = PrincipalNodeTranslator.userFromNode(node);
        final User userToUpdate = updateUserParams.update(existingUser);
        duplicateEmailValidation(userToUpdate.getKey(), userToUpdate.getEmail());
        final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.toUpdateNodeParams(userToUpdate);
        final Node updatedNode = nodeService.update(updateNodeParams);
        this.nodeService.refresh(RefreshMode.SEARCH);
        return PrincipalNodeTranslator.userFromNode(updatedNode);
    });
}
Also used : User(com.enonic.xp.security.User) Node(com.enonic.xp.node.Node) PrincipalNotFoundException(com.enonic.xp.security.PrincipalNotFoundException) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams)

Example 49 with User

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

the class SecurityServiceImpl method duplicateEmailValidation.

private void duplicateEmailValidation(final PrincipalKey key, final String email) {
    if (email == null) {
        return;
    }
    final CompareExpr idProviderExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.ID_PROVIDER_KEY), CompareExpr.Operator.EQ, ValueExpr.string(key.getIdProviderKey().toString()));
    final CompareExpr emailExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.EMAIL_KEY), CompareExpr.Operator.EQ, ValueExpr.string(email));
    final QueryExpr query = QueryExpr.from(LogicalExpr.and(idProviderExpr, emailExpr));
    final Nodes nodes = callWithContext(() -> {
        final FindNodesByQueryResult result = nodeService.findByQuery(NodeQuery.create().query(query).build());
        return this.nodeService.getByIds(result.getNodeIds());
    });
    final User user = nodes.isEmpty() ? null : PrincipalNodeTranslator.userFromNode(nodes.first());
    if (nodes.getSize() > 1 || (user != null && !user.getKey().equals(key))) {
        throw new IllegalArgumentException("A user with email '" + email + "' already exists in id provider '" + key.getIdProviderKey() + "'");
    }
}
Also used : User(com.enonic.xp.security.User) CompareExpr(com.enonic.xp.query.expr.CompareExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Nodes(com.enonic.xp.node.Nodes)

Example 50 with User

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

the class SecurityServiceImpl method setPassword.

@Override
public User setPassword(final PrincipalKey key, final String password) {
    Preconditions.checkArgument(key.isUser(), "Expected principal key of type User");
    Preconditions.checkArgument(password != null && password.length() > 0, "Password cannot be empty");
    return callWithContext(() -> {
        final Node node = callWithContext(() -> this.nodeService.getByPath(key.toPath()));
        if (node == null) {
            throw new NodeNotFoundException("setPassword failed, user with key " + key + " not found");
        }
        final User user = PrincipalNodeTranslator.userFromNode(node);
        if (user == null) {
            throw new NodeNotFoundException("setPassword failed, user with key " + key + " not found");
        }
        final String authenticationHash = this.passwordEncoder.encodePassword(password);
        final User userToUpdate = User.create(user).authenticationHash(authenticationHash).build();
        final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.toUpdateNodeParams(userToUpdate);
        final Node updatedNode = nodeService.update(updateNodeParams);
        return PrincipalNodeTranslator.userFromNode(updatedNode);
    });
}
Also used : NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) User(com.enonic.xp.security.User) Node(com.enonic.xp.node.Node) UpdateNodeParams(com.enonic.xp.node.UpdateNodeParams)

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