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;
}
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;
}
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);
}
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;
}
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);
}
Aggregations