// Save OAuth username as part of user profile of new user
OrganizationService orgService = uiRegisterForm.getApplicationComponent(OrganizationService.class);
UserProfileHandler profileHandler = orgService.getUserProfileHandler();
UserProfile newUserProfile = profileHandler.findUserProfileByName(newUser.getUserName());
AuthenticationRegistry authRegistry = uiRegisterForm.getApplicationComponent(AuthenticationRegistry.class);
HttpServletRequest httpRequest = portalRequestContext.getRequest();
OAuthPrincipal oauthPrincipal = (OAuthPrincipal)authRegistry.getAttributeOfClient(httpRequest, OAuthConstants.ATTRIBUTE_AUTHENTICATED_OAUTH_PRINCIPAL);
if (newUserProfile == null) {
newUserProfile = orgService.getUserProfileHandler().createUserProfileInstance(newUser.getUserName());
}
newUserProfile.setAttribute(oauthPrincipal.getOauthProviderType().getUserNameAttrName(), oauthPrincipal.getUserName());
try {
profileHandler.saveUserProfile(newUserProfile, true);
} catch (OAuthException gtnOAuthException) {
// Show warning message if user with this facebookUsername (or googleUsername) already exists
// NOTE: It could happen only in case of parallel registration of same oauth user from more browser windows
if (gtnOAuthException.getExceptionCode() == OAuthExceptionCode.DUPLICATE_OAUTH_PROVIDER_USERNAME) {
// Drop new user
orgService.getUserHandler().removeUser(newUser.getUserName(), true);
// Clear previous message about successful creation of user because we dropped him. Add message about duplicate oauth username
Object[] args = new Object[] {gtnOAuthException.getExceptionAttribute(OAuthConstants.EXCEPTION_OAUTH_PROVIDER_USERNAME),
gtnOAuthException.getExceptionAttribute(OAuthConstants.EXCEPTION_OAUTH_PROVIDER_NAME)};
ApplicationMessage appMessage = new ApplicationMessage("UIAccountSocial.msg.failed-registration", args, ApplicationMessage.WARNING);
uiApp.addMessage(appMessage);
return;
} else {
throw gtnOAuthException;
}
}
// Clean portalUser from context as we don't need it anymore
authRegistry.removeAttributeOfClient(httpRequest, OAuthConstants.ATTRIBUTE_AUTHENTICATED_PORTAL_USER);
// Clear messages (message about successful registration of user)
uiApp.clearMessages();
// Close the registration popup