diff --git a/services/src/main/java/org/keycloak/authentication/authenticators/broker/IdpReviewProfileAuthenticator.java b/services/src/main/java/org/keycloak/authentication/authenticators/broker/IdpReviewProfileAuthenticator.java index 6effc5f8e1..38cee59b40 100755 --- a/services/src/main/java/org/keycloak/authentication/authenticators/broker/IdpReviewProfileAuthenticator.java +++ b/services/src/main/java/org/keycloak/authentication/authenticators/broker/IdpReviewProfileAuthenticator.java @@ -39,9 +39,8 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfileProvider; import org.keycloak.userprofile.profile.DefaultUserProfileContext; import org.keycloak.userprofile.profile.representations.AttributeUserProfile; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; @@ -124,7 +123,7 @@ public class IdpReviewProfileAuthenticator extends AbstractIdpAuthenticator { return; } - UserProfileUpdateHelper.update(UserUpdateEvent.IdpReview, context.getSession(), new UserModelDelegate(null) { + UserUpdateHelper.updateIdpReview(context.getRealm(), new UserModelDelegate(null) { @Override public Map> getAttributes() { return userCtx.getAttributes(); diff --git a/services/src/main/java/org/keycloak/authentication/forms/RegistrationProfile.java b/services/src/main/java/org/keycloak/authentication/forms/RegistrationProfile.java index 90a3be9ced..cab2cf97de 100755 --- a/services/src/main/java/org/keycloak/authentication/forms/RegistrationProfile.java +++ b/services/src/main/java/org/keycloak/authentication/forms/RegistrationProfile.java @@ -38,11 +38,9 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfile; import org.keycloak.userprofile.UserProfileProvider; import org.keycloak.userprofile.profile.representations.AttributeUserProfile; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.DefaultUserProfileContext; -import org.keycloak.userprofile.profile.representations.UserModelUserProfile; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import javax.ws.rs.core.MultivaluedMap; import java.util.List; @@ -97,7 +95,7 @@ public class RegistrationProfile implements FormAction, FormActionFactory { public void success(FormContext context) { UserModel user = context.getUser(); AttributeUserProfile updatedProfile = AttributeFormDataProcessor.toUserProfile(context.getHttpRequest().getDecodedFormParameters()); - UserProfileUpdateHelper.update(UserUpdateEvent.RegistrationProfile, context.getSession(), user, updatedProfile, false); + UserUpdateHelper.updateRegistrationProfile(context.getRealm(), user, updatedProfile); } @Override diff --git a/services/src/main/java/org/keycloak/authentication/forms/RegistrationUserCreation.java b/services/src/main/java/org/keycloak/authentication/forms/RegistrationUserCreation.java index b974444f02..10e7277f65 100755 --- a/services/src/main/java/org/keycloak/authentication/forms/RegistrationUserCreation.java +++ b/services/src/main/java/org/keycloak/authentication/forms/RegistrationUserCreation.java @@ -41,11 +41,9 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfile; import org.keycloak.userprofile.UserProfileProvider; import org.keycloak.userprofile.profile.representations.AttributeUserProfile; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.DefaultUserProfileContext; -import org.keycloak.userprofile.profile.representations.UserModelUserProfile; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import javax.ws.rs.core.MultivaluedMap; import java.util.List; @@ -127,7 +125,7 @@ public class RegistrationUserCreation implements FormAction, FormActionFactory { UserModel user = context.getSession().users().addUser(context.getRealm(), username); user.setEnabled(true); - UserProfileUpdateHelper.update(UserUpdateEvent.RegistrationUserCreation, context.getSession(), user, updatedProfile, false); + UserUpdateHelper.updateRegistrationUserCreation(context.getRealm(), user, updatedProfile); context.getAuthenticationSession().setClientNote(OIDCLoginProtocol.LOGIN_HINT_PARAM, username); diff --git a/services/src/main/java/org/keycloak/authentication/requiredactions/UpdateProfile.java b/services/src/main/java/org/keycloak/authentication/requiredactions/UpdateProfile.java index a6d3e59d0d..895a7f2659 100644 --- a/services/src/main/java/org/keycloak/authentication/requiredactions/UpdateProfile.java +++ b/services/src/main/java/org/keycloak/authentication/requiredactions/UpdateProfile.java @@ -36,11 +36,9 @@ import org.keycloak.services.validation.Validation; import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfileProvider; import org.keycloak.userprofile.profile.representations.AttributeUserProfile; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.DefaultUserProfileContext; -import org.keycloak.userprofile.profile.representations.UserModelUserProfile; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; @@ -92,7 +90,7 @@ public class UpdateProfile implements RequiredActionProvider, RequiredActionFact return; } - UserProfileUpdateHelper.update(UserUpdateEvent.UpdateProfile, context.getSession(), user, updatedProfile); + UserUpdateHelper.updateUserProfile(context.getRealm(), user, updatedProfile); if (result.hasAttributeChanged(UserModel.EMAIL)) { user.setEmailVerified(false); event.clone().event(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, oldEmail).detail(Details.UPDATED_EMAIL, newEmail).success(); diff --git a/services/src/main/java/org/keycloak/services/resources/account/AccountFormService.java b/services/src/main/java/org/keycloak/services/resources/account/AccountFormService.java index 4cb4569450..91295fd93c 100755 --- a/services/src/main/java/org/keycloak/services/resources/account/AccountFormService.java +++ b/services/src/main/java/org/keycloak/services/resources/account/AccountFormService.java @@ -76,11 +76,9 @@ import org.keycloak.storage.ReadOnlyException; import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfileProvider; import org.keycloak.userprofile.profile.representations.AttributeUserProfile; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.DefaultUserProfileContext; -import org.keycloak.userprofile.profile.representations.UserModelUserProfile; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import org.keycloak.util.JsonSerialization; import org.keycloak.utils.CredentialHelper; @@ -387,7 +385,7 @@ public class AccountFormService extends AbstractSecuredLocalService { } try { - UserProfileUpdateHelper.update(UserUpdateEvent.Account, session, user, updatedProfile); + UserUpdateHelper.updateAccount(realm, user, updatedProfile); } catch (ReadOnlyException e) { setReferrerOnPage(); return account.setError(Response.Status.BAD_REQUEST, Messages.READ_ONLY_USER).setProfileFormData(formData).createResponse(AccountPages.ACCOUNT); diff --git a/services/src/main/java/org/keycloak/services/resources/account/AccountRestService.java b/services/src/main/java/org/keycloak/services/resources/account/AccountRestService.java index 29e1109b44..ff98e4339c 100755 --- a/services/src/main/java/org/keycloak/services/resources/account/AccountRestService.java +++ b/services/src/main/java/org/keycloak/services/resources/account/AccountRestService.java @@ -50,11 +50,10 @@ import org.keycloak.theme.Theme; import org.keycloak.userprofile.LegacyUserProfileProviderFactory; import org.keycloak.userprofile.UserProfile; import org.keycloak.userprofile.UserProfileProvider; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.representations.AccountUserRepresentationUserProfile; import org.keycloak.userprofile.profile.DefaultUserProfileContext; import org.keycloak.userprofile.validation.UserProfileValidationResult; -import org.keycloak.userprofile.validation.UserUpdateEvent; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -187,7 +186,7 @@ public class AccountRestService { return ErrorResponse.exists(Messages.EMAIL_EXISTS); try { - UserProfileUpdateHelper.update(UserUpdateEvent.Account, session, user, updatedUser); + UserUpdateHelper.updateAccount(realm, user, updatedUser); event.success(); return Cors.add(request, Response.noContent()).auth().allowedOrigins(auth.getToken()).build(); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java index 864103ee65..9cdd49ecc4 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UserResource.java @@ -71,9 +71,8 @@ import org.keycloak.services.resources.account.AccountFormService; import org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator; import org.keycloak.services.validation.Validation; import org.keycloak.storage.ReadOnlyException; -import org.keycloak.userprofile.utils.UserProfileUpdateHelper; +import org.keycloak.userprofile.utils.UserUpdateHelper; import org.keycloak.userprofile.profile.representations.UserRepresentationUserProfile; -import org.keycloak.userprofile.validation.UserUpdateEvent; import org.keycloak.utils.ProfileHelper; import javax.ws.rs.BadRequestException; @@ -192,7 +191,7 @@ public class UserResource { public static void updateUserFromRep(UserModel user, UserRepresentation rep, KeycloakSession session, boolean removeMissingRequiredActions) { - UserProfileUpdateHelper.update(UserUpdateEvent.UserResource, session, user, new UserRepresentationUserProfile(rep)); + UserUpdateHelper.updateUserResource(session.getContext().getRealm(), user, new UserRepresentationUserProfile(rep)); if (rep.isEnabled() != null) user.setEnabled(rep.isEnabled()); if (rep.isEmailVerified() != null) user.setEmailVerified(rep.isEmailVerified()); diff --git a/services/src/main/java/org/keycloak/userprofile/utils/UserProfileUpdateHelper.java b/services/src/main/java/org/keycloak/userprofile/utils/UserUpdateHelper.java similarity index 62% rename from services/src/main/java/org/keycloak/userprofile/utils/UserProfileUpdateHelper.java rename to services/src/main/java/org/keycloak/userprofile/utils/UserUpdateHelper.java index eb6ce1fbf1..b57c37a3d1 100644 --- a/services/src/main/java/org/keycloak/userprofile/utils/UserProfileUpdateHelper.java +++ b/services/src/main/java/org/keycloak/userprofile/utils/UserUpdateHelper.java @@ -17,7 +17,6 @@ package org.keycloak.userprofile.utils; -import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; import org.keycloak.models.utils.KeycloakModelUtils; @@ -33,15 +32,56 @@ import java.util.Set; /** * @author Markus Till */ -public class UserProfileUpdateHelper { +public class UserUpdateHelper { - public static void update(UserUpdateEvent userUpdateEvent, KeycloakSession session, UserModel currentUser, UserProfile updatedUser) { - update(userUpdateEvent, session, currentUser, updatedUser, true); + + public static void updateRegistrationProfile(RealmModel realm, UserModel currentUser, UserProfile updatedUser) { + register(UserUpdateEvent.RegistrationProfile, realm, currentUser, updatedUser); } + public static void updateRegistrationUserCreation(RealmModel realm, UserModel currentUser, UserProfile updatedUser) { + register(UserUpdateEvent.RegistrationUserCreation, realm, currentUser, updatedUser); + } - public static void update(UserUpdateEvent userUpdateEvent, KeycloakSession session, UserModel currentUser, UserProfile updatedUser, boolean removeMissingAttributes) { - RealmModel realm = session.getContext().getRealm(); + public static void updateIdpReview(RealmModel realm, UserModel userModelDelegate, UserProfile updatedProfile) { + update(UserUpdateEvent.IdpReview, realm, userModelDelegate, updatedProfile); + } + + public static void updateUserProfile(RealmModel realm, UserModel user, UserProfile updatedProfile) { + update(UserUpdateEvent.UpdateProfile, realm, user, updatedProfile); + } + + public static void updateAccount(RealmModel realm, UserModel user, UserProfile updatedProfile) { + update(UserUpdateEvent.Account, realm, user, updatedProfile); + } + + public static void updateUserResource(RealmModel realm, UserModel user, UserProfile userRepresentationUserProfile) { + update(UserUpdateEvent.UserResource, realm, user, userRepresentationUserProfile); + } + + /** + * will update the user model with the profile values, all missing attributes in the new profile will be removed on the user model + * @param userUpdateEvent + * @param realm + * @param currentUser + * @param updatedUser + */ + private static void update(UserUpdateEvent userUpdateEvent, RealmModel realm, UserModel currentUser, UserProfile updatedUser) { + update(userUpdateEvent, realm, currentUser, updatedUser, true); + } + + /** + * will update the user model with the profile values, attributes which are missing will be ignored + * @param userUpdateEvent + * @param realm + * @param currentUser + * @param updatedUser + */ + private static void register(UserUpdateEvent userUpdateEvent, RealmModel realm, UserModel currentUser, UserProfile updatedUser) { + update(userUpdateEvent, realm, currentUser, updatedUser, false); + } + + private static void update(UserUpdateEvent userUpdateEvent, RealmModel realm, UserModel currentUser, UserProfile updatedUser, boolean removeMissingAttributes) { if (updatedUser.getAttributes() == null || updatedUser.getAttributes().size() == 0) return; @@ -94,5 +134,4 @@ public class UserProfileUpdateHelper { return attr; } - }