Search in sources :

Example 1 with AttributeMetadata

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

the class AccountRestService method validationErrorParamsToString.

private String[] validationErrorParamsToString(Object[] messageParameters, Attributes userProfileAttributes) {
    if (messageParameters == null)
        return null;
    String[] ret = new String[messageParameters.length];
    int i = 0;
    for (Object p : messageParameters) {
        if (p != null) {
            // first parameter is user profile attribute name, we have to take Display Name for it
            if (i == 0) {
                AttributeMetadata am = userProfileAttributes.getMetadata(p.toString());
                if (am != null)
                    ret[i++] = am.getAttributeDisplayName();
                else
                    ret[i++] = p.toString();
            } else {
                ret[i++] = p.toString();
            }
        } else {
            i++;
        }
    }
    return ret;
}
Also used : UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) AttributeMetadata(org.keycloak.userprofile.AttributeMetadata)

Example 2 with AttributeMetadata

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

Aggregations

AttributeMetadata (org.keycloak.userprofile.AttributeMetadata)2 List (java.util.List)1 UserProfileAttributeMetadata (org.keycloak.representations.account.UserProfileAttributeMetadata)1 AttributeContext (org.keycloak.userprofile.AttributeContext)1 ValidationError (org.keycloak.validate.ValidationError)1