Skip to content

Commit

Permalink
twitter users integrated with db
Browse files Browse the repository at this point in the history
  • Loading branch information
ZheniaTrochun committed May 9, 2018
1 parent 74b159f commit 54538f5
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -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<Facebook> {
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<Facebook> {
// public FacebookConnectionFactory(String clientId, String clientSecret) {
// super("facebook", new FacebookServiceProvider(clientId, clientSecret, ""), new FacebookAdapter());
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
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;
import org.slf4j.LoggerFactory;
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;
Expand Down Expand Up @@ -114,7 +113,7 @@ public ResponseEntity<List<UserReview>> getReviews(@RequestParam @NotNull Long b
@RequestMapping(value = "/review", method = RequestMethod.POST)
public ResponseEntity<Void> addReviews(@RequestBody @NotNull UserReviewDto reviewDto) {

String username = ((Principal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
String username = SecurityUtils.getUsername();

UserReview review = new UserReview(
reviewDto.getRating(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public ResponseEntity<Void> 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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

This file was deleted.

33 changes: 26 additions & 7 deletions src/main/java/com/yevhenii/kpi/readmore/model/User.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
8 changes: 0 additions & 8 deletions src/main/resources/static/connect/twitterConnected.html

This file was deleted.

0 comments on commit 54538f5

Please sign in to comment.