Skip to content

Commit

Permalink
Merge pull request #418 from khoa-nd/master
Browse files Browse the repository at this point in the history
Merged
  • Loading branch information
khoa-nd committed Sep 24, 2015
2 parents a8c1841 + b2dc576 commit 3cab991
Show file tree
Hide file tree
Showing 39 changed files with 1,513 additions and 1,293 deletions.
848 changes: 426 additions & 422 deletions src/main/java/com/techlooper/config/CoreConfiguration.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.techlooper.entity.UserProfile;
import com.techlooper.entity.VnwUserProfile;
import com.techlooper.entity.vnw.RoleName;
import com.techlooper.model.Language;
import com.techlooper.model.SocialProvider;
import com.techlooper.model.UserProfileDto;
import com.techlooper.repository.JsonConfigRepository;
import com.techlooper.service.SocialService;
import com.techlooper.service.UserService;
import com.techlooper.service.VietnamWorksUserService;
import org.dozer.Mapper;
import org.slf4j.Logger;
Expand All @@ -18,6 +20,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
Expand All @@ -36,7 +39,7 @@ public class SocialAuthenticationProvider implements AuthenticationProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SocialAuthenticationProvider.class);

@Resource
private JsonConfigRepository jsonConfigRepository;
private UserService userService;

@Resource
private VietnamWorksUserService vietnamWorksUserService;
Expand Down Expand Up @@ -71,6 +74,15 @@ public Authentication authenticate(Authentication authentication) throws Authent
LOGGER.error(ex.getMessage(), ex);
}

if (StringUtils.hasText(userProfileDto.getEmail())) {
try {
userService.sendOnBoardingEmail(userProfileDto.getEmail(), Language.en);
}
catch (Exception e) {
userProfileDto.getEmail();
}
}

return new UsernamePasswordAuthenticationToken(userProfileDto, null,
Arrays.asList(new SimpleGrantedAuthority(RoleName.JOB_SEEKER.name())));
}
Expand Down
104 changes: 57 additions & 47 deletions src/main/java/com/techlooper/controller/SocialController.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
package com.techlooper.controller;

import com.techlooper.entity.AccessGrant;
import com.techlooper.entity.UserEntity;
import com.techlooper.entity.VnwUserProfile;
import com.techlooper.model.Authentication;
import com.techlooper.model.Language;
import com.techlooper.model.SocialConfig;
import com.techlooper.model.SocialProvider;
import com.techlooper.model.SocialResponse;
import com.techlooper.repository.JsonConfigRepository;
import com.techlooper.repository.elasticsearch.ChallengeRegistrantRepository;
import com.techlooper.service.SocialService;
import com.techlooper.service.UserService;
import com.techlooper.service.VietnamWorksUserService;
import com.techlooper.service.impl.FacebookService;
import com.techlooper.service.impl.GoogleService;
import org.jasypt.util.text.TextEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.social.connect.UserProfile;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Created by phuonghqh on 12/10/14.
Expand All @@ -39,14 +32,14 @@ public class SocialController {

private static final Logger LOGGER = LoggerFactory.getLogger(SocialController.class);

@Resource
private ApplicationContext applicationContext;
// @Resource
// private ApplicationContext applicationContext;

@Resource
private JsonConfigRepository jsonConfigRepository;

@Resource
private TextEncryptor textEncryptor;
// @Resource
// private TextEncryptor textEncryptor;

@Resource
private FacebookService facebookService;
Expand All @@ -57,13 +50,16 @@ public class SocialController {
@Resource
private VietnamWorksUserService vietnamWorksUserService;

@Resource
private UserService userService;

@RequestMapping(value = "login/social/{social}", method = RequestMethod.GET)
public void loginBySocial(@PathVariable SocialProvider social, @RequestParam(required = false) String code, HttpServletResponse response) throws IOException {
response.sendRedirect(code == null ? "/#/?action=cancel" : String.format("/#/?action=loginBySocial&social=%s&code=%s", social, code));
}

@RequestMapping(value = "register/vnw/google", method = RequestMethod.GET)
public void registerVnwUserFromGoogle(@RequestParam(required = false) String code, HttpServletResponse response) throws IOException {
public void registerVnwUserFromGoogle(@RequestParam(required = false) String state, @RequestParam(required = false) String code, HttpServletResponse response) throws IOException {
if (code == null) {
response.sendRedirect("/#/?action=cancel");
return;
Expand All @@ -79,9 +75,22 @@ public void registerVnwUserFromGoogle(@RequestParam(required = false) String cod
response.sendRedirect(socialConfig.getApiUrl().get("login"));
return;
}

sendOnBoardingEmail(userProfile.getEmail(), state);
registerVnwUserAndRedirect(response, userProfile.getFirstName(), userProfile.getLastName(), userProfile.getEmail());
}

private void sendOnBoardingEmail(String email, String language) throws IOException {
if (!StringUtils.hasText(email)) return;

try {
userService.sendOnBoardingEmail(email, Language.parse(language));
}
catch (Exception e) {
LOGGER.debug("Not send on boarding email", e);
}
}

private void registerVnwUserAndRedirect(HttpServletResponse response, String firstName, String lastName, String email) throws IOException {
if (StringUtils.hasText(email)) {
try {
Expand All @@ -105,7 +114,7 @@ private void registerVnwUserAndRedirect(HttpServletResponse response, String fir


@RequestMapping(value = "register/vnw/fb", method = RequestMethod.GET)
public void registerVnwUserFromFB(@RequestParam(required = false) String code, HttpServletResponse response) throws IOException {
public void registerVnwUserFromFB(@RequestParam(required = false) String state, @RequestParam(required = false) String code, HttpServletResponse response) throws IOException {
if (code == null) {
response.sendRedirect("/#/?action=cancel");
return;
Expand All @@ -123,6 +132,7 @@ public void registerVnwUserFromFB(@RequestParam(required = false) String code, H
return;
}

sendOnBoardingEmail(userProfile.getEmail(), state);
registerVnwUserAndRedirect(response, userProfile.getFirstName(), userProfile.getLastName(), userProfile.getEmail());
}

Expand All @@ -146,35 +156,35 @@ public List<SocialConfig> getSocialConfig(@RequestParam("providers[]") List<Soci
return configs;
}

@ResponseBody
@RequestMapping("/auth/{provider}")
public SocialResponse auth(@PathVariable SocialProvider provider,
@CookieValue(value = "techlooper.key", required = false) String key,
@RequestBody(required = false) Authentication auth) {
SocialService service = applicationContext.getBean(provider + "Service", SocialService.class);
AccessGrant accessGrant = service.getAccessGrant(auth.getCode());
UserEntity userEntity = StringUtils.hasText(key) ? service.saveFootprint(accessGrant, key) : service.saveFootprint(accessGrant);
return SocialResponse.Builder.get()
.withToken(accessGrant.getAccessToken())
.withKey(textEncryptor.encrypt(userEntity.key())).build();
}

@ResponseBody
@RequestMapping("/auth/oath1/{provider}")
public SocialResponse auth1(@PathVariable SocialProvider provider, HttpServletResponse httpServletResponse,
@CookieValue(value = "techlooper.key", required = false) String key,
@RequestParam(value = "oauth_token", required = false) String token,
@RequestParam(value = "oauth_verifier", required = false) String verifier) throws IOException {
SocialService service = applicationContext.getBean(provider + "Service", SocialService.class);
if (Optional.ofNullable(token).isPresent()) {
AccessGrant accessGrant = service.getAccessGrant(token, verifier);
UserEntity userEntity = StringUtils.hasText(key) ? service.saveFootprint(accessGrant, key) : service.saveFootprint(accessGrant);
return SocialResponse.Builder.get()
.withToken(accessGrant.getValue())
.withKey(textEncryptor.encrypt(userEntity.key())).build();
}
AccessGrant accessGrant = service.getAccessGrant(null, null);
httpServletResponse.sendRedirect(accessGrant.getAuthorizeUrl());
return null;
}
// @ResponseBody
// @RequestMapping("/auth/{provider}")
// public SocialResponse auth(@PathVariable SocialProvider provider,
// @CookieValue(value = "techlooper.key", required = false) String key,
// @RequestBody(required = false) Authentication auth) {
// SocialService service = applicationContext.getBean(provider + "Service", SocialService.class);
// AccessGrant accessGrant = service.getAccessGrant(auth.getCode());
// UserEntity userEntity = StringUtils.hasText(key) ? service.saveFootprint(accessGrant, key) : service.saveFootprint(accessGrant);
// return SocialResponse.Builder.get()
// .withToken(accessGrant.getAccessToken())
// .withKey(textEncryptor.encrypt(userEntity.key())).build();
// }
//
// @ResponseBody
// @RequestMapping("/auth/oath1/{provider}")
// public SocialResponse auth1(@PathVariable SocialProvider provider, HttpServletResponse httpServletResponse,
// @CookieValue(value = "techlooper.key", required = false) String key,
// @RequestParam(value = "oauth_token", required = false) String token,
// @RequestParam(value = "oauth_verifier", required = false) String verifier) throws IOException {
// SocialService service = applicationContext.getBean(provider + "Service", SocialService.class);
// if (Optional.ofNullable(token).isPresent()) {
// AccessGrant accessGrant = service.getAccessGrant(token, verifier);
// UserEntity userEntity = StringUtils.hasText(key) ? service.saveFootprint(accessGrant, key) : service.saveFootprint(accessGrant);
// return SocialResponse.Builder.get()
// .withToken(accessGrant.getValue())
// .withKey(textEncryptor.encrypt(userEntity.key())).build();
// }
// AccessGrant accessGrant = service.getAccessGrant(null, null);
// httpServletResponse.sendRedirect(accessGrant.getAuthorizeUrl());
// return null;
// }
}
Loading

0 comments on commit 3cab991

Please sign in to comment.