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());
}
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() + "'");
}
}
Aggregations