refactore userupdate helper api
This commit is contained in:
parent
802a670cc5
commit
7da619385c
8 changed files with 60 additions and 32 deletions
|
@ -39,9 +39,8 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
import org.keycloak.userprofile.UserProfileProvider;
|
||||||
import org.keycloak.userprofile.profile.DefaultUserProfileContext;
|
import org.keycloak.userprofile.profile.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.profile.representations.AttributeUserProfile;
|
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.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
@ -124,7 +123,7 @@ public class IdpReviewProfileAuthenticator extends AbstractIdpAuthenticator {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserProfileUpdateHelper.update(UserUpdateEvent.IdpReview, context.getSession(), new UserModelDelegate(null) {
|
UserUpdateHelper.updateIdpReview(context.getRealm(), new UserModelDelegate(null) {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, List<String>> getAttributes() {
|
public Map<String, List<String>> getAttributes() {
|
||||||
return userCtx.getAttributes();
|
return userCtx.getAttributes();
|
||||||
|
|
|
@ -38,11 +38,9 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfile;
|
import org.keycloak.userprofile.UserProfile;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
import org.keycloak.userprofile.UserProfileProvider;
|
||||||
import org.keycloak.userprofile.profile.representations.AttributeUserProfile;
|
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.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.profile.representations.UserModelUserProfile;
|
|
||||||
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -97,7 +95,7 @@ public class RegistrationProfile implements FormAction, FormActionFactory {
|
||||||
public void success(FormContext context) {
|
public void success(FormContext context) {
|
||||||
UserModel user = context.getUser();
|
UserModel user = context.getUser();
|
||||||
AttributeUserProfile updatedProfile = AttributeFormDataProcessor.toUserProfile(context.getHttpRequest().getDecodedFormParameters());
|
AttributeUserProfile updatedProfile = AttributeFormDataProcessor.toUserProfile(context.getHttpRequest().getDecodedFormParameters());
|
||||||
UserProfileUpdateHelper.update(UserUpdateEvent.RegistrationProfile, context.getSession(), user, updatedProfile, false);
|
UserUpdateHelper.updateRegistrationProfile(context.getRealm(), user, updatedProfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -41,11 +41,9 @@ import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfile;
|
import org.keycloak.userprofile.UserProfile;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
import org.keycloak.userprofile.UserProfileProvider;
|
||||||
import org.keycloak.userprofile.profile.representations.AttributeUserProfile;
|
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.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.profile.representations.UserModelUserProfile;
|
|
||||||
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -127,7 +125,7 @@ public class RegistrationUserCreation implements FormAction, FormActionFactory {
|
||||||
|
|
||||||
UserModel user = context.getSession().users().addUser(context.getRealm(), username);
|
UserModel user = context.getSession().users().addUser(context.getRealm(), username);
|
||||||
user.setEnabled(true);
|
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);
|
context.getAuthenticationSession().setClientNote(OIDCLoginProtocol.LOGIN_HINT_PARAM, username);
|
||||||
|
|
||||||
|
|
|
@ -36,11 +36,9 @@ import org.keycloak.services.validation.Validation;
|
||||||
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
import org.keycloak.userprofile.UserProfileProvider;
|
||||||
import org.keycloak.userprofile.profile.representations.AttributeUserProfile;
|
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.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.profile.representations.UserModelUserProfile;
|
|
||||||
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.MultivaluedMap;
|
import javax.ws.rs.core.MultivaluedMap;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
@ -92,7 +90,7 @@ public class UpdateProfile implements RequiredActionProvider, RequiredActionFact
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserProfileUpdateHelper.update(UserUpdateEvent.UpdateProfile, context.getSession(), user, updatedProfile);
|
UserUpdateHelper.updateUserProfile(context.getRealm(), user, updatedProfile);
|
||||||
if (result.hasAttributeChanged(UserModel.EMAIL)) {
|
if (result.hasAttributeChanged(UserModel.EMAIL)) {
|
||||||
user.setEmailVerified(false);
|
user.setEmailVerified(false);
|
||||||
event.clone().event(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, oldEmail).detail(Details.UPDATED_EMAIL, newEmail).success();
|
event.clone().event(EventType.UPDATE_EMAIL).detail(Details.PREVIOUS_EMAIL, oldEmail).detail(Details.UPDATED_EMAIL, newEmail).success();
|
||||||
|
|
|
@ -76,11 +76,9 @@ import org.keycloak.storage.ReadOnlyException;
|
||||||
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
import org.keycloak.userprofile.UserProfileProvider;
|
||||||
import org.keycloak.userprofile.profile.representations.AttributeUserProfile;
|
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.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.profile.representations.UserModelUserProfile;
|
|
||||||
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
import org.keycloak.util.JsonSerialization;
|
import org.keycloak.util.JsonSerialization;
|
||||||
import org.keycloak.utils.CredentialHelper;
|
import org.keycloak.utils.CredentialHelper;
|
||||||
|
|
||||||
|
@ -387,7 +385,7 @@ public class AccountFormService extends AbstractSecuredLocalService {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
UserProfileUpdateHelper.update(UserUpdateEvent.Account, session, user, updatedProfile);
|
UserUpdateHelper.updateAccount(realm, user, updatedProfile);
|
||||||
} catch (ReadOnlyException e) {
|
} catch (ReadOnlyException e) {
|
||||||
setReferrerOnPage();
|
setReferrerOnPage();
|
||||||
return account.setError(Response.Status.BAD_REQUEST, Messages.READ_ONLY_USER).setProfileFormData(formData).createResponse(AccountPages.ACCOUNT);
|
return account.setError(Response.Status.BAD_REQUEST, Messages.READ_ONLY_USER).setProfileFormData(formData).createResponse(AccountPages.ACCOUNT);
|
||||||
|
|
|
@ -50,11 +50,10 @@ import org.keycloak.theme.Theme;
|
||||||
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
import org.keycloak.userprofile.LegacyUserProfileProviderFactory;
|
||||||
import org.keycloak.userprofile.UserProfile;
|
import org.keycloak.userprofile.UserProfile;
|
||||||
import org.keycloak.userprofile.UserProfileProvider;
|
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.representations.AccountUserRepresentationUserProfile;
|
||||||
import org.keycloak.userprofile.profile.DefaultUserProfileContext;
|
import org.keycloak.userprofile.profile.DefaultUserProfileContext;
|
||||||
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
import org.keycloak.userprofile.validation.UserProfileValidationResult;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
|
@ -187,7 +186,7 @@ public class AccountRestService {
|
||||||
return ErrorResponse.exists(Messages.EMAIL_EXISTS);
|
return ErrorResponse.exists(Messages.EMAIL_EXISTS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
UserProfileUpdateHelper.update(UserUpdateEvent.Account, session, user, updatedUser);
|
UserUpdateHelper.updateAccount(realm, user, updatedUser);
|
||||||
event.success();
|
event.success();
|
||||||
|
|
||||||
return Cors.add(request, Response.noContent()).auth().allowedOrigins(auth.getToken()).build();
|
return Cors.add(request, Response.noContent()).auth().allowedOrigins(auth.getToken()).build();
|
||||||
|
|
|
@ -71,9 +71,8 @@ import org.keycloak.services.resources.account.AccountFormService;
|
||||||
import org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator;
|
import org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator;
|
||||||
import org.keycloak.services.validation.Validation;
|
import org.keycloak.services.validation.Validation;
|
||||||
import org.keycloak.storage.ReadOnlyException;
|
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.profile.representations.UserRepresentationUserProfile;
|
||||||
import org.keycloak.userprofile.validation.UserUpdateEvent;
|
|
||||||
import org.keycloak.utils.ProfileHelper;
|
import org.keycloak.utils.ProfileHelper;
|
||||||
|
|
||||||
import javax.ws.rs.BadRequestException;
|
import javax.ws.rs.BadRequestException;
|
||||||
|
@ -192,7 +191,7 @@ public class UserResource {
|
||||||
|
|
||||||
public static void updateUserFromRep(UserModel user, UserRepresentation rep, KeycloakSession session, boolean removeMissingRequiredActions) {
|
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.isEnabled() != null) user.setEnabled(rep.isEnabled());
|
||||||
if (rep.isEmailVerified() != null) user.setEmailVerified(rep.isEmailVerified());
|
if (rep.isEmailVerified() != null) user.setEmailVerified(rep.isEmailVerified());
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
|
|
||||||
package org.keycloak.userprofile.utils;
|
package org.keycloak.userprofile.utils;
|
||||||
|
|
||||||
import org.keycloak.models.KeycloakSession;
|
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.models.UserModel;
|
import org.keycloak.models.UserModel;
|
||||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||||
|
@ -33,15 +32,56 @@ import java.util.Set;
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:markus.till@bosch.io">Markus Till</a>
|
* @author <a href="mailto:markus.till@bosch.io">Markus Till</a>
|
||||||
*/
|
*/
|
||||||
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) {
|
public static void updateIdpReview(RealmModel realm, UserModel userModelDelegate, UserProfile updatedProfile) {
|
||||||
RealmModel realm = session.getContext().getRealm();
|
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)
|
if (updatedUser.getAttributes() == null || updatedUser.getAttributes().size() == 0)
|
||||||
return;
|
return;
|
||||||
|
@ -94,5 +134,4 @@ public class UserProfileUpdateHelper {
|
||||||
return attr;
|
return attr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue