Search in sources :

Example 41 with User

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);
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 42 with User

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;
}
Also used : LocalDateTime(java.time.LocalDateTime) User(com.aidanwhiteley.books.domain.User)

Example 43 with 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;
}
Also used : User(com.aidanwhiteley.books.domain.User)

Example 44 with 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;
}
Also used : User(com.aidanwhiteley.books.domain.User)

Example 45 with 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();
    }
}
Also used : User(com.aidanwhiteley.books.domain.User) Book(com.aidanwhiteley.books.domain.Book) URI(java.net.URI) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

User (com.aidanwhiteley.books.domain.User)61 Test (org.junit.jupiter.api.Test)35 Book (com.aidanwhiteley.books.domain.Book)25 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)24 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)13 URI (java.net.URI)5 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)4 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)4 Principal (java.security.Principal)3 HttpHeaders (org.springframework.http.HttpHeaders)3 Comment (com.aidanwhiteley.books.domain.Comment)2 Around (org.aspectj.lang.annotation.Around)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 ClientRoles (com.aidanwhiteley.books.controller.dtos.ClientRoles)1 NotAuthorisedException (com.aidanwhiteley.books.controller.exceptions.NotAuthorisedException)1 Owner (com.aidanwhiteley.books.domain.Owner)1 Item (com.aidanwhiteley.books.domain.googlebooks.Item)1 Claims (io.jsonwebtoken.Claims)1 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)1 LocalDateTime (java.time.LocalDateTime)1