Search in sources :

Example 21 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;
            }
        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 22 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")).lastName((String) userDetails.get("last_name")).fullName((String) userDetails.get("name")).link((String) userDetails.get("link")).email((String) userDetails.get(EMAIL)).lastLogon(LocalDateTime.now()).firstLogon(LocalDateTime.now()).authProvider(FACEBOOK).build();
    user = 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 23 with User

use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.

the class JwtAuthenticationUtils method getUserIfExists.

public Optional<User> getUserIfExists(JwtAuthentication auth) {
    if (auth == null) {
        return Optional.empty();
    }
    String authenticationServiceId = auth.getAuthenticationServiceId();
    String authenticationProviderId = auth.getAuthProvider();
    LOGGER.debug("Query user repository with id of {} and provider of {}", authenticationServiceId, authenticationProviderId);
    List<User> users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(authenticationServiceId, authenticationProviderId);
    User user;
    switch(users.size()) {
        case 0:
            user = null;
            break;
        case 1:
            user = users.get(0);
            break;
        default:
            LOGGER.error("More than one user found for JwtAuthentication: {}", auth);
            throw new IllegalStateException("More that one user found for a given Jwt Authentication");
    }
    return Optional.ofNullable(user);
}
Also used : User(com.aidanwhiteley.books.domain.User)

Example 24 with User

use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.

the class MailClient method sendEmailsToAdminsForNewUsers.

public boolean sendEmailsToAdminsForNewUsers(List<User> newUsers) {
    boolean emailSent = true;
    MimeMessagePreparator messagePreparator = mimeMessage -> {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(registrationAdminEmailFrom);
        messageHelper.setTo(registrationAdminEmailTo);
        messageHelper.setSubject(registrationAdminEmailTitle);
        messageHelper.setText(prepareAdminNewUsersNotificationEmailContent(newUsers));
    };
    try {
        mailSender.send(messagePreparator);
    } catch (MailException me) {
        emailSent = false;
        LOGGER.error("Failed to send user registration emails for {}", newUsers, me);
    }
    return emailSent;
}
Also used : Value(org.springframework.beans.factory.annotation.Value) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) List(java.util.List) Logger(org.slf4j.Logger) User(com.aidanwhiteley.books.domain.User) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Service(org.springframework.stereotype.Service) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MailException(org.springframework.mail.MailException) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 25 with User

use of com.aidanwhiteley.books.domain.User in project books by aidanwhiteley.

the class Oauth2AuthenticationUtils method getUserIfExists.

public Optional<User> getUserIfExists(OAuth2AuthenticationToken authentication) {
    OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
    String authenticationProviderId = authorizedClient.getPrincipalName();
    List<User> users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(authenticationProviderId, this.getAuthenticationProvider(authentication).toString());
    User user;
    switch(users.size()) {
        case 0:
            user = null;
            break;
        case 1:
            user = users.get(0);
            break;
        default:
            LOGGER.error("More than one user found for Authentication: {}", authentication);
            throw new IllegalStateException("More that one user found for a given Authentication");
    }
    return Optional.ofNullable(user);
}
Also used : User(com.aidanwhiteley.books.domain.User) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient)

Aggregations

User (com.aidanwhiteley.books.domain.User)36 Test (org.junit.Test)19 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)16 Book (com.aidanwhiteley.books.domain.Book)15 BookRepositoryTest (com.aidanwhiteley.books.repository.BookRepositoryTest)9 URI (java.net.URI)3 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)3 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)3 Principal (java.security.Principal)2 Around (org.aspectj.lang.annotation.Around)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ClientRoles (com.aidanwhiteley.books.controller.dtos.ClientRoles)1 AccessForbiddenException (com.aidanwhiteley.books.controller.exceptions.AccessForbiddenException)1 Comment (com.aidanwhiteley.books.domain.Comment)1 Owner (com.aidanwhiteley.books.domain.Owner)1 Claims (io.jsonwebtoken.Claims)1 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)1 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Cookie (javax.servlet.http.Cookie)1