use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.
the class RestApiExceptionHandlerTest method getPostRequestBuilder.
@SuppressWarnings("SameParameterValue")
private RequestBuilder getPostRequestBuilder(String url, Book book) {
User user = BookControllerTestUtils.getTestUser();
String token = jwtUtils.createTokenForUser(user);
String xsrfToken = BookControllerTestUtils.getXsrfToken(testRestTemplate);
HttpEntity<Book> entity = BookControllerTestUtils.getBookHttpEntity(book, token, xsrfToken);
return MockMvcRequestBuilders.post(url).content("{\"title\":\"The Travelling Hornplayer\",\"foundOnGoogle\":true,\"googleBookId\":\"pbFgLK91crUC\",\"author\":\"xzczx\",\"genre\":\"zcxzx\",\"summary\":\"xzcxzczxc\",\"rating\":4,\"createdDateTime\":\"2018-08-12T17:28:25.435Z\"}").headers(entity.getHeaders()).accept(MediaType.APPLICATION_JSON);
}
use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.
the class UserService method createUser.
private User createUser(Map<String, Object> userDetails, User.AuthenticationProvider provider) {
User user;
LocalDateTime now = LocalDateTime.now();
switch(provider) {
case GOOGLE:
{
user = createGoogleUser(userDetails, now);
break;
}
case FACEBOOK:
{
user = createFacebookUser(userDetails);
break;
}
case LOCAL:
{
user = createLocalActuatorUser(userDetails, now);
break;
}
default:
{
LOGGER.error("Unexpected oauth user type {}", provider);
throw new IllegalArgumentException("Unexpected oauth type: " + provider);
}
}
userRepository.insert(user);
LOGGER.info("User created in repository: {}", user);
return user;
}
use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.
the class UserService method createFacebookUser.
private User createFacebookUser(Map<String, Object> userDetails) {
User user;
user = User.builder().authenticationServiceId((String) userDetails.get("id")).firstName((String) userDetails.get(FIRST_NAME_PROPERTY)).lastName((String) userDetails.get(LAST_NAME_PROPERTY)).fullName((String) userDetails.get(NAME_PROPERTY)).link((String) userDetails.get("link")).email((String) userDetails.get(EMAIL)).lastLogon(LocalDateTime.now()).firstLogon(LocalDateTime.now()).authProvider(FACEBOOK).build();
setDefaultAdminUser(user);
user.addRole(User.Role.ROLE_USER);
String url = extractFaceBookPictureUrl(userDetails);
if (url != null) {
user.setPicture(url);
}
return user;
}
use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.
the class UserService method createLocalActuatorUser.
private User createLocalActuatorUser(Map<String, Object> userDetails, LocalDateTime now) {
User user;
user = User.builder().authenticationServiceId(LOCAL_ACTUATOR_USER).firstName((String) userDetails.get(FIRST_NAME_PROPERTY)).lastName((String) userDetails.get(LAST_NAME_PROPERTY)).fullName((String) userDetails.get(NAME_PROPERTY)).lastLogon(now).firstLogon(now).authProvider(LOCAL).build();
user.addRole(User.Role.ROLE_ACTUATOR);
return user;
}
use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.
the class BookSecureController method createBook.
@PostMapping(value = "/books")
public ResponseEntity<Book> createBook(@Valid @RequestBody Book book, Principal principal) throws MalformedURLException, URISyntaxException {
LOGGER.debug("createBook in BookSecureController called");
Optional<User> user = authUtils.extractUserFromPrincipal(principal, false);
if (user.isPresent()) {
Book insertedBook = bookRepository.insert(book);
// go and get the full details from Google and then update the Mongo document for the book
if (book.getGoogleBookId() != null && book.getGoogleBookId().length() > 0) {
googleBooksDaoAsync.updateBookWithGoogleBookDetails(insertedBook, book.getGoogleBookId());
}
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(insertedBook.getId()).toUri();
// Basic GET of book details are not on a secure API
location = new URI(location.toURL().toString().replace("/secure", ""));
LOGGER.debug("createBook existed. New Book created in store - accessible at {}", location);
return ResponseEntity.created(location).build();
} else {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Couldnt create a book as user to own book not found! Principal: {}", logMessageDetaint(principal));
}
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
Aggregations