protected Map<String, List<Map<String, Object>>> getConnectionsAttribute(Profile profile) { return profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); }
protected void updateProfile() { try { profile = profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to update profile of user '" + profile.getId() + "'", e); } }
/** * Adds the info from the provider profile to the specified profile. * * @param profile the target profile * @param providerProfile the provider profile where to get the info */ public static void addProviderProfileInfo(Profile profile, UserProfile providerProfile) { String email = providerProfile.getEmail(); if (StringUtils.isEmpty(email)) { throw new IllegalStateException("No email included in provider profile"); } String username = providerProfile.getUsername(); if (StringUtils.isEmpty(username)) { username = email; } String firstName = providerProfile.getFirstName(); String lastName = providerProfile.getLastName(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); }
protected Profile createProfile(String tenant, Connection<?> connection, Profile userData) { try { ConnectionUtils.addConnectionData(userData, connection.createData(), textEncryptor); return profileService.createProfile(tenant, userData.getUsername(), null, userData.getEmail(), true, userData.getRoles(), userData.getAttributes(), null); } catch (CryptoException | ProfileException e) { throw new AuthenticationException("Unable to create profile of user '" + userData.getUsername() + "' in tenant '" + tenant + "'", e); } }
Profile profile = new Profile(); UserProfile providerProfile = connection.fetchUserProfile(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); profile.setAttribute(DISPLAY_NAME_ATTRIBUTE_NAME, displayName); profile.setAttribute(AVATAR_LINK_ATTRIBUTE_NAME, connection.getImageUrl());
Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); List<Map<String, Object>> connectionsForProvider = null; allConnections = new HashMap<>(); profile.setAttribute(CONNECTIONS_ATTRIBUTE_NAME, allConnections); } else { connectionsForProvider = allConnections.get(connectionData.getProviderId());
protected Profile updateProfileConnectionData(String tenant, Connection<?> connection, Profile profile) { try { ConnectionUtils.addConnectionData(profile, connection.createData(), textEncryptor); return profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (CryptoException | ProfileException e) { throw new AuthenticationException("Unable to update connection data of user '" + profile.getUsername() + "' of tenant '" + tenant + "'", e); } }
@Override public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) { String providerUserIdsStr = StringUtils.join(providerUserIds, ", "); String query = String.format(FIND_PROFILES_BY_PROVIDER_ID_AND_USER_ID_QUERY, providerId, providerUserIdsStr); List<Profile> profiles = findProfilesByQuery(query); if (CollectionUtils.isNotEmpty(profiles)) { Set<String> ids = new LinkedHashSet<>(profiles.size()); for (Profile profile : profiles) { ids.add(profile.getId().toString()); } return ids; } else { return Collections.emptySet(); } }
@Override public Authentication complete(String tenant, String providerId, HttpServletRequest request, Set<String> newUserRoles, Map<String, Object> newUserAttributes, ConnectSupport connectSupport) throws AuthenticationException { if (connectSupport == null) { connectSupport = this.connectSupport; } Connection<?> connection = completeConnection(connectSupport, providerId, request); if (connection != null) { Profile userData = ConnectionUtils.createProfile(connection); Profile profile = getProfile(tenant, userData); if (profile == null) { if (CollectionUtils.isNotEmpty(newUserRoles)) { userData.getRoles().addAll(newUserRoles); } if (MapUtils.isNotEmpty(newUserAttributes)) { userData.getAttributes().putAll(newUserAttributes); } profile = createProfile(tenant, connection, userData); } else { profile = updateProfileConnectionData(tenant, connection, profile); } Authentication auth = authenticationManager.authenticateUser(profile); SecurityUtils.setAuthentication(request, auth); return auth; } else { return null; } }
/** * Creates a profile from the specified connection. * * @param connection the connection where to retrieve the profile info from * * @return */ public static Profile createProfileFromConnection(Connection<?> connection) { Profile profile = new Profile(); addProviderProfileInfo(profile, connection.fetchUserProfile()); return profile; }
@AfterReturning(value = "execution(* org.craftercms.profile.api.services.ProfileService.updateProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.verifyProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.enableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.disableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.addRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.updateAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.changePassword(..))", returning = "updatedProfile") public void refreshCurrentAuthentication(Profile updatedProfile) { Authentication auth = SecurityUtils.getCurrentAuthentication(); if (auth != null) { Profile profile = auth.getProfile(); if (profile.equals(updatedProfile)) { String ticket = auth.getTicket(); auth = new DefaultAuthentication(ticket, updatedProfile); // Put updated authentication in cache authenticationCache.putAuthentication(auth); // Update current authentication object SecurityUtils.setCurrentAuthentication(auth); } } }
Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); List<Map<String, Object>> connectionsForProvider = null; allConnections = new HashMap<>(); profile.setAttribute(CONNECTIONS_ATTRIBUTE_NAME, allConnections); } else { connectionsForProvider = allConnections.get(connectionData.getProviderId());
@Override public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) { String providerUserIdsStr = StringUtils.join(providerUserIds, ", "); String query = String.format(FIND_PROFILES_BY_PROVIDER_ID_AND_USER_ID_QUERY, providerId, providerUserIdsStr); List<Profile> profiles = findProfilesByQuery(query); if (CollectionUtils.isNotEmpty(profiles)) { Set<String> ids = new LinkedHashSet<>(profiles.size()); for (Profile profile : profiles) { ids.add(profile.getId().toString()); } return ids; } else { return Collections.emptySet(); } }
/** * Creates a profile from the specified connection. * * @param connection the connection where to retrieve the profile info from * * @return */ public static Profile createProfileFromConnection(Connection<?> connection) { Profile profile = new Profile(); addProviderProfileInfo(profile, connection.fetchUserProfile()); return profile; }
/** * Adds the info from the provider profile to the specified profile. * * @param profile the target profile * @param providerProfile the provider profile where to get the info */ public static void addProviderProfileInfo(Profile profile, UserProfile providerProfile) { String email = providerProfile.getEmail(); if (StringUtils.isEmpty(email)) { throw new IllegalStateException("No email included in provider profile"); } String username = providerProfile.getUsername(); if (StringUtils.isEmpty(username)) { username = email; } String firstName = providerProfile.getFirstName(); String lastName = providerProfile.getLastName(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); }
Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); List<Map<String, Object>> connectionsForProvider = null; allConnections = new HashMap<>(); profile.setAttribute(CONNECTIONS_ATTRIBUTE_NAME, allConnections); } else { connectionsForProvider = allConnections.get(connectionData.getProviderId());
protected Map<String, List<Map<String, Object>>> getConnectionsAttribute(Profile profile) { return profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); }
protected void updateProfile() { try { profile = profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to update profile of user '" + profile.getId() + "'", e); } }
@Override public List<String> findUserIdsWithConnection(Connection<?> connection) { String providerId = connection.getKey().getProviderId(); String providerUserId = connection.getKey().getProviderUserId(); String query = String.format(FIND_PROFILES_BY_PROVIDER_ID_AND_USER_ID_QUERY, providerId, providerUserId); List<Profile> profiles = findProfilesByQuery(query); if (CollectionUtils.isNotEmpty(profiles)) { List<String> ids = new ArrayList<>(profiles.size()); for (Profile profile : profiles) { ids.add(profile.getId().toString()); } return ids; } else { return Collections.emptyList(); } }
/** * Adds the info from the provider profile to the specified profile. * * @param profile the target profile * @param providerProfile the provider profile where to get the info */ public static void addProviderProfileInfo(Profile profile, UserProfile providerProfile) { String email = providerProfile.getEmail(); if (StringUtils.isEmpty(email)) { throw new IllegalStateException("No email included in provider profile"); } String username = providerProfile.getUsername(); if (StringUtils.isEmpty(username)) { username = email; } String firstName = providerProfile.getFirstName(); String lastName = providerProfile.getLastName(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); }