ConversationState state = ConversationState.getCurrent();
String userName = ((User) state.getAttribute(CacheUserProfileFilter.USER_PROFILE)).getUserName();
User user = service.getUserHandler().findUserByName(userName);
if (user != null) {
UserProfile userProfile = (UserProfile)prContext.getAttribute(UserProfileLifecycle.USER_PROFILE_ATTRIBUTE_NAME);
String unlinkProviderKey = prContext.getRequestParameter(PARAM_PROVIDER_FOR_UNLINK);
OAuthProviderType<AccessTokenContext> oauthProviderTypeToUnlink = uiForm.getApplicationComponent(OAuthProviderTypeRegistry.class).getOAuthProvider(unlinkProviderKey, AccessTokenContext.class);
// Obtain current accessToken
AccessTokenContext accessToken = uiForm.getApplicationComponent(SocialNetworkService.class).getOAuthAccessToken(oauthProviderTypeToUnlink, userName);
// Unlink social account in userProfile (AccessTokenInvalidationListener will automatically remove accessToken)
if (oauthProviderTypeToUnlink != null) {
userProfile.setAttribute(oauthProviderTypeToUnlink.getUserNameAttrName(), null);
} else {
log.warn("Social account field to unlink not found");
}
service.getUserProfileHandler().saveUserProfile(userProfile, true);