Ejemplo n.º 1
0
 public void createSocialUser(Connection<?> connection, String langKey) {
   if (connection == null) {
     log.error("Cannot create social user because connection is null");
     throw new IllegalArgumentException("Connection cannot be null");
   }
   UserProfile userProfile = connection.fetchUserProfile();
   String providerId = connection.getKey().getProviderId();
   User user = createUserIfNotExist(userProfile, langKey, providerId);
   createSocialConnection(user.getLogin(), connection);
   mailService.sendSocialRegistrationValidationEmail(user, providerId);
 }
Ejemplo n.º 2
0
  private User createUserIfNotExist(UserProfile userProfile, String langKey, String providerId) {
    String email = userProfile.getEmail();
    String userName = userProfile.getUsername();
    if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) {
      log.error("Cannot create social user because email and login are null");
      throw new IllegalArgumentException("Email and login cannot be null");
    }
    if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) {
      log.error(
          "Cannot create social user because email is null and login already exist, login -> {}",
          userName);
      throw new IllegalArgumentException("Email cannot be null with an existing login");
    }
    Optional<User> user = userRepository.findOneByEmail(email);
    if (user.isPresent()) {
      log.info("User already exist associate the connection to this account");
      return user.get();
    }

    String login = getLoginDependingOnProviderId(userProfile, providerId);
    String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10));
    Set<Authority> authorities = new HashSet<>(1);
    authorities.add(authorityRepository.findOne("ROLE_USER"));

    User newUser = new User();
    newUser.setLogin(login);
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(userProfile.getFirstName());
    newUser.setLastName(userProfile.getLastName());
    newUser.setEmail(email);
    newUser.setActivated(true);
    newUser.setAuthorities(authorities);
    newUser.setLangKey(langKey);

    return userRepository.save(newUser);
  }