Hello guys, ive just recently updated keycloak from version 9 to version 14, previusly i had a provider that implemented UserProfileProvider in which i’ve done some logic for adding a BE validation,
like this:
public UserProfileValidationResult validate(UserProfileContext updateContext, UserProfile updatedProfile) {
LOGGER.info("UserProfileValidationResult - " + updateContext.getUpdateEvent());
RealmModel realm = this.session.getContext().getRealm();
ValidationChainBuilder builder = ValidationChainBuilder.builder();
switch (updateContext.getUpdateEvent()) {
case UserResource:
this.addReadOnlyAttributeValidators(builder, this.adminReadOnlyAttributes, updateContext, updatedProfile);
break;
case IdpReview:
this.addBasicValidators(builder, !realm.isRegistrationEmailAsUsername());
this.addReadOnlyAttributeValidators(builder, this.readOnlyAttributes, updateContext, updatedProfile);
break;
case Account:
case RegistrationProfile:
case UpdateProfile:
this.addBasicValidators(builder, !realm.isRegistrationEmailAsUsername() && realm.isEditUsernameAllowed());
this.addReadOnlyAttributeValidators(builder, this.readOnlyAttributes, updateContext, updatedProfile);
this.addSessionValidators(builder);
this.addCustomValidators(builder, updatedProfile); //funzione per validare gli attributi custom
break;
case RegistrationUserCreation:
this.addUserCreationValidators(builder);
this.addReadOnlyAttributeValidators(builder, this.readOnlyAttributes, updateContext, updatedProfile);
}
return new UserProfileValidationResult(builder.build().validate(updateContext, updatedProfile), updatedProfile);
}
private void addCustomValidators(ValidationChainBuilder builder, UserProfile updatedProfile) {
final int IS_ENDUSER = 1;
final int IS_PROFESSIONAL = 2;
int userType = IS_ENDUSER;
try {
userType = Integer.parseInt(updatedProfile.getAttributes().getFirstAttribute("user_type"));
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
if (userType == IS_PROFESSIONAL) {
builder.addAttributeValidator().forAttribute("company")
.addSingleAttributeValueValidationFunction("missingCompany", StaticValidators.isBlank())
.build()
.addAttributeValidator().forAttribute("job_title")
.addSingleAttributeValueValidationFunction("missingJobTitle", StaticValidators.isBlank())
.build();
}
builder.addAttributeValidator().forAttribute("country")
.addSingleAttributeValueValidationFunction("missingCountry", StaticValidators.isBlank())
.build();
}
In the new 14.0 version the UserProfileProvider is completely different, is there any way to implement backend attributes validation without enabling the thecnical preview feature of User Profile SPI? (modificato)