@ApiOperation(
      value = "SNS 기반 회원 가입시 필요한 회원 프로필 정보",
      produces = "application/json",
      response = UserProfileForm.class)
  @RequestMapping(value = "/social/attempted", method = RequestMethod.GET)
  public UserProfileForm loginSocialUser(NativeWebRequest request) {

    Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);

    if (Objects.isNull(connection)) throw new ServiceException(ServiceError.CANNOT_GET_SNS_PROFILE);

    ConnectionKey connectionKey = connection.getKey();

    CommonConst.ACCOUNT_TYPE convertProviderId =
        CommonConst.ACCOUNT_TYPE.valueOf(connectionKey.getProviderId().toUpperCase());
    UserProfile existUser =
        userService.findUserProfileByProviderIdAndProviderUserId(
            convertProviderId, connectionKey.getProviderUserId());
    org.springframework.social.connect.UserProfile socialProfile = connection.fetchUserProfile();

    String username = null;
    if (Objects.nonNull(socialProfile.getName())) {
      username = socialProfile.getName();
    } else if (Objects.nonNull(socialProfile.getUsername())) {
      username = socialProfile.getUsername();
    } else {
      if (Objects.nonNull(socialProfile.getFirstName())) {
        username = socialProfile.getFirstName();
      }
      if (Objects.nonNull(socialProfile.getLastName())) {
        username =
            Objects.isNull(username)
                ? socialProfile.getLastName()
                : ' ' + socialProfile.getLastName();
      }
    }

    UserProfileForm user = new UserProfileForm();
    user.setEmail(socialProfile.getEmail());
    user.setUsername(username);

    if (Objects.nonNull(existUser)) {
      user.setId(existUser.getId());
      user.setAbout(existUser.getAbout());

      if (Objects.nonNull(existUser.getSupportFC()))
        user.setFootballClub(existUser.getSupportFC().getId());
    }

    return user;
  }
  @RequestMapping(value = "/user/register.html", method = RequestMethod.GET)
  public String registerOrLoginSocialUser(WebRequest request) {
    Connection<?> connection = ProviderSignInUtils.getConnection(request);

    if (connection != null) {
      String providerId = connection.getKey().getProviderId();
      String providerUserId = connection.getKey().getProviderUserId();

      Usuario usuario = usuarioService.buscarPorProviderYuserId(providerId, providerUserId);

      if (usuario == null) {
        UserProfile profile = connection.fetchUserProfile();
        usuario = new Usuario();
        usuario.setNombre(profile.getFirstName());
        usuario.setApellidos(profile.getLastName());
        usuario.setCorreo(profile.getEmail());
        usuario.setFoto(connection.getImageUrl());
        usuario.setClave(null);
        usuario.setProviderid(providerId);
        usuario.setProvideruserid(providerUserId);
        usuario.setRol(new Rol(1));

        usuarioService.save(usuario);
      }

      SecurityUtil.logInUserSocial(usuario);
    }

    return "redirect:/index.html";
  }
Пример #3
0
 /**
  * Attempt to retrieve signup from social provider.
  *
  * @param contextId
  * @param request
  */
 public Signup socialSignUpAttempt(int contextId, WebRequest request) {
   Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
   if (connection != null) {
     UserProfile providerUser = connection.fetchUserProfile();
     if (providerUser != null) {
       return new Signup(
           contextId,
           providerUser.getEmail(),
           providerUser.getFirstName(),
           providerUser.getLastName());
     }
   }
   return new Signup(contextId, "");
 }
Пример #4
0
  private Connection<?> createConnection(
      String login, String email, String firstName, String lastName, String providerId) {
    UserProfile userProfile = mock(UserProfile.class);
    when(userProfile.getEmail()).thenReturn(email);
    when(userProfile.getUsername()).thenReturn(login);
    when(userProfile.getFirstName()).thenReturn(firstName);
    when(userProfile.getLastName()).thenReturn(lastName);

    Connection<?> connection = mock(Connection.class);
    ConnectionKey key = new ConnectionKey(providerId, "PROVIDER_USER_ID");
    when(connection.fetchUserProfile()).thenReturn(userProfile);
    when(connection.getKey()).thenReturn(key);

    return connection;
  }
  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);
  }