Search in sources :

Example 1 with UserProfileAttributeValidationContext

use of org.keycloak.userprofile.UserProfileAttributeValidationContext in project keycloak by keycloak.

the class ImmutableAttributeValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    UserProfileAttributeValidationContext ac = (UserProfileAttributeValidationContext) context;
    AttributeContext attributeContext = ac.getAttributeContext();
    if (!isReadOnly(attributeContext)) {
        return context;
    }
    UserModel user = attributeContext.getUser();
    if (user == null) {
        return context;
    }
    List<String> currentValue = user.getAttributeStream(inputHint).collect(Collectors.toList());
    List<String> values = (List<String>) input;
    if (!CollectionUtil.collectionEquals(currentValue, values)) {
        if (currentValue.isEmpty() && !notBlankValidator().validate(values).isValid()) {
            return context;
        }
        context.addError(new ValidationError(ID, inputHint, DEFAULT_ERROR_MESSAGE));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) AttributeContext(org.keycloak.userprofile.AttributeContext) UserProfileAttributeValidationContext(org.keycloak.userprofile.UserProfileAttributeValidationContext) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Aggregations

List (java.util.List)1 UserModel (org.keycloak.models.UserModel)1 AttributeContext (org.keycloak.userprofile.AttributeContext)1 UserProfileAttributeValidationContext (org.keycloak.userprofile.UserProfileAttributeValidationContext)1 ValidationError (org.keycloak.validate.ValidationError)1