From 54538f56b92a9652f7b1632984cd3c995e7870d0 Mon Sep 17 00:00:00 2001 From: Zhenia Trochun Date: Wed, 9 May 2018 21:59:32 +0300 Subject: [PATCH] twitter users integrated with db --- .../readmore/FacebookConnectionFactory.java | 24 ++++---- .../controller/BookControllerImpl.java | 5 +- .../controller/BookStateControllerImpl.java | 2 +- .../controller/OAuthSignUpController.java | 30 ++++++++++ .../readmore/controller/SignUpController.java | 35 ------------ .../com/yevhenii/kpi/readmore/model/User.java | 33 ++++++++--- .../readmore/service/OAuthSignUpService.java | 56 +++++++++++++++++++ .../static/connect/twitterConnected.html | 8 --- 8 files changed, 127 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/yevhenii/kpi/readmore/controller/OAuthSignUpController.java delete mode 100644 src/main/java/com/yevhenii/kpi/readmore/controller/SignUpController.java create mode 100644 src/main/java/com/yevhenii/kpi/readmore/service/OAuthSignUpService.java delete mode 100644 src/main/resources/static/connect/twitterConnected.html diff --git a/src/main/java/com/yevhenii/kpi/readmore/FacebookConnectionFactory.java b/src/main/java/com/yevhenii/kpi/readmore/FacebookConnectionFactory.java index 63f0bcb..79abcbb 100644 --- a/src/main/java/com/yevhenii/kpi/readmore/FacebookConnectionFactory.java +++ b/src/main/java/com/yevhenii/kpi/readmore/FacebookConnectionFactory.java @@ -1,12 +1,12 @@ -package com.yevhenii.kpi.readmore; - -import org.springframework.social.connect.support.OAuth2ConnectionFactory; -import org.springframework.social.facebook.api.Facebook; -import org.springframework.social.facebook.connect.FacebookAdapter; -import org.springframework.social.facebook.connect.FacebookServiceProvider; - -public class FacebookConnectionFactory extends OAuth2ConnectionFactory { - public FacebookConnectionFactory(String clientId, String clientSecret) { - super("facebook", new FacebookServiceProvider(clientId, clientSecret, ""), new FacebookAdapter()); - } -} +//package com.yevhenii.kpi.readmore; +// +//import org.springframework.social.connect.support.OAuth2ConnectionFactory; +//import org.springframework.social.facebook.api.Facebook; +//import org.springframework.social.facebook.connect.FacebookAdapter; +//import org.springframework.social.facebook.connect.FacebookServiceProvider; +// +//public class FacebookConnectionFactory extends OAuth2ConnectionFactory { +// public FacebookConnectionFactory(String clientId, String clientSecret) { +// super("facebook", new FacebookServiceProvider(clientId, clientSecret, ""), new FacebookAdapter()); +// } +//} \ No newline at end of file diff --git a/src/main/java/com/yevhenii/kpi/readmore/controller/BookControllerImpl.java b/src/main/java/com/yevhenii/kpi/readmore/controller/BookControllerImpl.java index 7e79342..54291d5 100644 --- a/src/main/java/com/yevhenii/kpi/readmore/controller/BookControllerImpl.java +++ b/src/main/java/com/yevhenii/kpi/readmore/controller/BookControllerImpl.java @@ -6,6 +6,7 @@ import com.yevhenii.kpi.readmore.model.response.BookResponse; import com.yevhenii.kpi.readmore.service.BookService; import com.yevhenii.kpi.readmore.utils.ControllerUtils; +import com.yevhenii.kpi.readmore.utils.SecurityUtils; import com.yevhenii.kpi.readmore.utils.converter.BookToBookResponseConverter; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; @@ -13,11 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotNull; -import java.security.Principal; import java.util.List; import java.util.Optional; import java.util.concurrent.Callable; @@ -114,7 +113,7 @@ public ResponseEntity> getReviews(@RequestParam @NotNull Long b @RequestMapping(value = "/review", method = RequestMethod.POST) public ResponseEntity addReviews(@RequestBody @NotNull UserReviewDto reviewDto) { - String username = ((Principal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName(); + String username = SecurityUtils.getUsername(); UserReview review = new UserReview( reviewDto.getRating(), diff --git a/src/main/java/com/yevhenii/kpi/readmore/controller/BookStateControllerImpl.java b/src/main/java/com/yevhenii/kpi/readmore/controller/BookStateControllerImpl.java index f4142e7..850a3b1 100644 --- a/src/main/java/com/yevhenii/kpi/readmore/controller/BookStateControllerImpl.java +++ b/src/main/java/com/yevhenii/kpi/readmore/controller/BookStateControllerImpl.java @@ -128,7 +128,7 @@ public ResponseEntity addUserTodo(@RequestBody @Valid Book book) { boolean success = bookStateService.addTodoItem(book, username); - LOGGER.debug(String.format("Todo addition for user[%s], bookId[%d], result[%s]", + LOGGER.info(String.format("Todo addition for user[%s], bookId[%d], result[%s]", username, book.getId(), success ? "OK" : "FAILED")); diff --git a/src/main/java/com/yevhenii/kpi/readmore/controller/OAuthSignUpController.java b/src/main/java/com/yevhenii/kpi/readmore/controller/OAuthSignUpController.java new file mode 100644 index 0000000..8c23453 --- /dev/null +++ b/src/main/java/com/yevhenii/kpi/readmore/controller/OAuthSignUpController.java @@ -0,0 +1,30 @@ +package com.yevhenii.kpi.readmore.controller; + +import com.yevhenii.kpi.readmore.service.OAuthSignUpService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class OAuthSignUpController { + + private final OAuthSignUpService signUpService; + + @Autowired + public OAuthSignUpController(OAuthSignUpService signUpService) { + this.signUpService = signUpService; + } + +// todo add dao + @RequestMapping("/signup") + public ModelAndView signup(WebRequest request) { + + if (signUpService.signup(request)) { + return new ModelAndView("redirect:/"); + } else { + return new ModelAndView("redirect:/error"); + } + } +} diff --git a/src/main/java/com/yevhenii/kpi/readmore/controller/SignUpController.java b/src/main/java/com/yevhenii/kpi/readmore/controller/SignUpController.java deleted file mode 100644 index 6b0a631..0000000 --- a/src/main/java/com/yevhenii/kpi/readmore/controller/SignUpController.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.yevhenii.kpi.readmore.controller; - -import com.yevhenii.kpi.readmore.security.AuthUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.social.connect.Connection; -import org.springframework.social.connect.ConnectionFactoryLocator; -import org.springframework.social.connect.UsersConnectionRepository; -import org.springframework.social.connect.web.ProviderSignInUtils; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.context.request.WebRequest; - -@Controller -public class SignUpController { - - private final ProviderSignInUtils signInUtils; - - @Autowired - public SignUpController(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository connectionRepository) { - this.signInUtils = new ProviderSignInUtils(connectionFactoryLocator, connectionRepository); - } - -// todo add dao - @RequestMapping("/signup") - public String signup(WebRequest request) { - Connection connection = signInUtils.getConnectionFromSession(request); - - if (connection != null) { - AuthUtils.authenticate(connection); - signInUtils.doPostSignUp(connection.getDisplayName(), request); - } - - return "redirect:/"; - } -} diff --git a/src/main/java/com/yevhenii/kpi/readmore/model/User.java b/src/main/java/com/yevhenii/kpi/readmore/model/User.java index 4ebdf65..43f866e 100644 --- a/src/main/java/com/yevhenii/kpi/readmore/model/User.java +++ b/src/main/java/com/yevhenii/kpi/readmore/model/User.java @@ -1,15 +1,20 @@ package com.yevhenii.kpi.readmore.model; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; -import org.hibernate.validator.constraints.Email; -import javax.persistence.*; -//import javax.validation.constraints.Email; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; import javax.validation.constraints.NotNull; +//import javax.validation.constraints.Email; + @Entity @Data +@Builder @AllArgsConstructor @Table(name = "users") public class User { @@ -20,21 +25,35 @@ public class User { @NotNull private String name; - @Email - @NotNull + private String email; private String role; - @NotNull + private String hashedPass; + private String strategy = "password"; + public User() { } - public User(@NotNull String name, @Email @NotNull String email, @NotNull String hashedPass, String role) { + public User(@NotNull String name, String email, String hashedPass, String role) { + this.name = name; + this.email = email; + this.role = role; + this.hashedPass = hashedPass; + } + + public User(String name, String email, String role, String hashedPass, String strategy) { this.name = name; this.email = email; this.role = role; this.hashedPass = hashedPass; + this.strategy = strategy; + } + + public User(String name, String hashedPass) { + this.name = name; + this.hashedPass = hashedPass; } } diff --git a/src/main/java/com/yevhenii/kpi/readmore/service/OAuthSignUpService.java b/src/main/java/com/yevhenii/kpi/readmore/service/OAuthSignUpService.java new file mode 100644 index 0000000..13dae67 --- /dev/null +++ b/src/main/java/com/yevhenii/kpi/readmore/service/OAuthSignUpService.java @@ -0,0 +1,56 @@ +package com.yevhenii.kpi.readmore.service; + +import com.yevhenii.kpi.readmore.model.User; +import com.yevhenii.kpi.readmore.repository.UserRepository; +import com.yevhenii.kpi.readmore.security.AuthUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.social.connect.Connection; +import org.springframework.social.connect.ConnectionFactoryLocator; +import org.springframework.social.connect.UsersConnectionRepository; +import org.springframework.social.connect.web.ProviderSignInUtils; +import org.springframework.stereotype.Service; +import org.springframework.web.context.request.WebRequest; + +import java.util.Objects; + +@Service +public class OAuthSignUpService { + + private final ProviderSignInUtils signInUtils; + private final UserRepository userRepository; + + @Autowired + public OAuthSignUpService(ConnectionFactoryLocator connectionFactoryLocator, + UsersConnectionRepository connectionRepository, + UserRepository userRepository) { + this.userRepository = userRepository; + this.signInUtils = new ProviderSignInUtils(connectionFactoryLocator, connectionRepository); + } + + // todo add dao + // todo think about duplicated usernames + public boolean signup(WebRequest request) { + Connection connection = signInUtils.getConnectionFromSession(request); + + if (Objects.isNull(connection)){ + return false; + } + + String name = connection.getDisplayName().substring(1); + + userRepository.findUserByName(name).orElseGet(() -> { + User user = User.builder() + .name(name) + .strategy("twitter") + .build(); + + return userRepository.save(user); + }); + + AuthUtils.authenticate(connection); + signInUtils.doPostSignUp(connection.getDisplayName(), request); + + return true; + } + +} diff --git a/src/main/resources/static/connect/twitterConnected.html b/src/main/resources/static/connect/twitterConnected.html deleted file mode 100644 index ceda975..0000000 --- a/src/main/resources/static/connect/twitterConnected.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - -

test

- - \ No newline at end of file