Search in sources :

Example 1 with AttributeContext

use of org.keycloak.userprofile.AttributeContext 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)

Example 2 with AttributeContext

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

the class UsernameMutationValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (Validation.isBlank(value)) {
        return context;
    }
    AttributeContext attributeContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
    UserModel user = attributeContext.getUser();
    RealmModel realm = context.getSession().getContext().getRealm();
    if (!realm.isEditUsernameAllowed() && user != null && !value.equals(user.getFirstAttribute(UserModel.USERNAME))) {
        if (realm.isRegistrationEmailAsUsername() && UserProfileContext.UPDATE_PROFILE.equals(attributeContext.getContext())) {
            // it is expected that username changes when attributes are normalized by the provider
            return context;
        }
        context.addError(new ValidationError(ID, inputHint, Messages.READ_ONLY_USERNAME));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) RealmModel(org.keycloak.models.RealmModel) AttributeContext(org.keycloak.userprofile.AttributeContext) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 3 with AttributeContext

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

the class AttributeRequiredByMetadataValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    AttributeContext attContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
    AttributeMetadata metadata = attContext.getMetadata();
    if (!metadata.isRequired(attContext)) {
        return context;
    }
    if (metadata.isReadOnly(attContext)) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
    } else {
        for (String value : values) {
            if (Validation.isBlank(value)) {
                context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
                return context;
            }
        }
    }
    return context;
}
Also used : AttributeContext(org.keycloak.userprofile.AttributeContext) AttributeMetadata(org.keycloak.userprofile.AttributeMetadata) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 4 with AttributeContext

use of org.keycloak.userprofile.AttributeContext 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)

Aggregations

List (java.util.List)4 AttributeContext (org.keycloak.userprofile.AttributeContext)4 ValidationError (org.keycloak.validate.ValidationError)4 UserModel (org.keycloak.models.UserModel)3 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 RealmModel (org.keycloak.models.RealmModel)1 AttributeMetadata (org.keycloak.userprofile.AttributeMetadata)1 UserProfileAttributeValidationContext (org.keycloak.userprofile.UserProfileAttributeValidationContext)1