Search in sources :

Example 6 with CompareExpr

use of com.enonic.xp.query.expr.CompareExpr in project xp by enonic.

the class SecurityServiceImpl method findByEmail.

private User findByEmail(final IdProviderKey idProvider, final String email) {
    final CompareExpr idProviderExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.ID_PROVIDER_KEY), CompareExpr.Operator.EQ, ValueExpr.string(idProvider.toString()));
    final CompareExpr userNameExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.EMAIL_KEY), CompareExpr.Operator.EQ, ValueExpr.string(email));
    final QueryExpr query = QueryExpr.from(LogicalExpr.and(idProviderExpr, userNameExpr));
    final Nodes nodes = callWithContext(() -> {
        final FindNodesByQueryResult result = nodeService.findByQuery(NodeQuery.create().query(query).build());
        return this.nodeService.getByIds(result.getNodeIds());
    });
    if (nodes.getSize() > 1) {
        throw new IllegalArgumentException("Expected at most 1 user with email " + email + " in id provider " + idProvider);
    }
    return nodes.isEmpty() ? null : PrincipalNodeTranslator.userFromNode(nodes.first());
}
Also used : 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 7 with CompareExpr

use of com.enonic.xp.query.expr.CompareExpr 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)

Aggregations

CompareExpr (com.enonic.xp.query.expr.CompareExpr)7 QueryExpr (com.enonic.xp.query.expr.QueryExpr)4 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)3 Nodes (com.enonic.xp.node.Nodes)3 Context (com.enonic.xp.context.Context)1 ConstraintExpr (com.enonic.xp.query.expr.ConstraintExpr)1 FieldOrderExpr (com.enonic.xp.query.expr.FieldOrderExpr)1 LogicalExpr (com.enonic.xp.query.expr.LogicalExpr)1 SearchQueryFieldNameResolver (com.enonic.xp.repo.impl.elasticsearch.query.translator.resolver.SearchQueryFieldNameResolver)1 User (com.enonic.xp.security.User)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 Test (org.junit.jupiter.api.Test)1