Search in sources :

Example 21 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class ReadOnlyAttributeUnchangedValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    AttributeContext attributeContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
    Map.Entry<String, List<String>> attribute = attributeContext.getAttribute();
    String key = attribute.getKey();
    Pattern pattern = (Pattern) config.get(CFG_PATTERN);
    if (!pattern.matcher(key).find()) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null) {
        return context;
    }
    UserModel user = attributeContext.getUser();
    List<String> existingAttrValues = user == null ? null : user.getAttribute(key);
    String existingValue = null;
    if (existingAttrValues != null && !existingAttrValues.isEmpty()) {
        existingValue = existingAttrValues.get(0);
    }
    String value = null;
    if (!values.isEmpty()) {
        value = values.get(0);
    }
    if (!isUnchanged(existingValue, value)) {
        logger.warnf("Attempt to edit denied attribute '%s' of user '%s'", pattern, user == null ? "new user" : user.getFirstAttribute(UserModel.USERNAME));
        context.addError(new ValidationError(ID, key, UPDATE_READ_ONLY_ATTRIBUTES_REJECTED_MSG));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) Pattern(java.util.regex.Pattern) AttributeContext(org.keycloak.userprofile.AttributeContext) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError) Map(java.util.Map)

Example 22 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class RegistrationEmailAsUsernameUsernameValueValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    RealmModel realm = context.getSession().getContext().getRealm();
    if (!realm.isRegistrationEmailAsUsername()) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (value != null && Validation.isBlank(value)) {
        context.addError(new ValidationError(ID, inputHint, Messages.MISSING_USERNAME));
    }
    return context;
}
Also used : RealmModel(org.keycloak.models.RealmModel) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 23 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class UsernameHasValueValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    String value = null;
    if (values != null && !values.isEmpty()) {
        value = values.get(0);
    }
    if (Validation.isBlank(value)) {
        context.addError(new ValidationError(ID, inputHint, Messages.MISSING_USERNAME));
    }
    return context;
}
Also used : List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Aggregations

ValidationError (org.keycloak.validate.ValidationError)23 List (java.util.List)14 UserModel (org.keycloak.models.UserModel)8 RealmModel (org.keycloak.models.RealmModel)7 KeycloakSession (org.keycloak.models.KeycloakSession)5 AttributeContext (org.keycloak.userprofile.AttributeContext)4 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ValidationResult (org.keycloak.validate.ValidationResult)2 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1