// Either credentials are stored in their own class...
if (credentialClass != null) {
Property<?> credentialIdentity = modelProperties.get(PROPERTY_CREDENTIAL_IDENTITY);
Property<?> credentialType = modelProperties.get(PROPERTY_CREDENTIAL_TYPE);
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<?> criteria = builder.createQuery(credentialClass);
Root<?> root = criteria.from(credentialClass);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(builder.equal(root.get(credentialIdentity.getName()),
lookupIdentity(identityObject, em)));
if (credentialType != null) {
if (String.class.equals(credentialType.getJavaClass())) {
predicates.add(builder.equal(root.get(credentialType.getName()),
credential.getType().getName()));
} else {
predicates.add(builder.equal(root.get(credentialType.getName()),
lookupCredentialTypeEntity(credential.getType().getName(), em)));
}
}
criteria.where(predicates.toArray(new Predicate[0]));
List<?> results = em.createQuery(criteria).getResultList();
if (results.isEmpty()) return false;
// TODO this only supports plain text passwords
for (Object result : results) {
Object val = credentialValue.getValue(result);
if (val.equals(credential.getValue())) return true;
}
}
// or they're stored in the identity class
else {
Property<?> identityNameProp = modelProperties.get(PROPERTY_IDENTITY_NAME);
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<?> criteria = builder.createQuery(credentialValue.getDeclaringClass());
Root<?> root = criteria.from(credentialValue.getDeclaringClass());
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(builder.equal(root.get(identityNameProp.getName()),
identityObject.getName()));
criteria.where(predicates.toArray(new Predicate[predicates.size()]));
Object result = em.createQuery(criteria).getSingleResult();