package eu.eudat.logic.managers; import eu.eudat.commons.scope.user.UserScope; import eu.eudat.data.CredentialEntity; import eu.eudat.data.old.EmailConfirmation; import eu.eudat.data.old.UserInfo; import eu.eudat.exceptions.emailconfirmation.HasConfirmedEmailException; import eu.eudat.exceptions.emailconfirmation.TokenExpiredException; import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.operations.DatabaseRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.management.InvalidApplicationException; import java.util.Date; import java.util.UUID; @Component public class EmailConfirmationManager { private ApiContext apiContext; private DatabaseRepository databaseRepository; private final UserScope userScope; @Autowired public EmailConfirmationManager(ApiContext apiContext, UserScope userScope) { this.apiContext = apiContext; this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.userScope = userScope; } public void confirmEmail(String token) throws TokenExpiredException, HasConfirmedEmailException, InvalidApplicationException { EmailConfirmation loginConfirmationEmail = apiContext.getOperationsContext() .getDatabaseRepository().getLoginConfirmationEmailDao().asQueryable() .where((builder, root) -> builder.equal(root.get("token"), UUID.fromString(token))).getSingle(); UserInfo user = databaseRepository.getUserInfoDao().asQueryable() .where((builder, root) -> builder.equal(root.get("id"), loginConfirmationEmail.getUserId())).getSingle(); if (user.getEmail() != null) throw new HasConfirmedEmailException("User already has confirmed his Email."); if (loginConfirmationEmail.getExpiresAt().compareTo(new Date()) < 0) throw new TokenExpiredException("Token has expired."); loginConfirmationEmail.setIsConfirmed(true); // Checks if mail is used by another user. If it is, merges the new the old. Long existingUsers = databaseRepository.getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), loginConfirmationEmail.getEmail())).count(); if (existingUsers > 0) { CredentialEntity credential = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userId"), user.getId())).getSingle(); credential.setEmail(loginConfirmationEmail.getEmail()); databaseRepository.getCredentialDao().createOrUpdate(credential); UserInfo oldUser = databaseRepository.getUserInfoDao().asQueryable().where((builder, root) -> builder.equal(root.get("email"), loginConfirmationEmail.getEmail())).getSingle(); mergeNewUserToOld(user, oldUser); // expireUserToken(user); //TODO: Authn databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail); return; } user.setEmail(loginConfirmationEmail.getEmail()); databaseRepository.getUserInfoDao().createOrUpdate(user); CredentialEntity credential = databaseRepository.getCredentialDao().asQueryable() .where((builder, root) -> builder.equal(root.get("userId"), user.getId())).getSingle(); if(credential.getEmail() == null){ credential.setEmail(user.getEmail()); databaseRepository.getCredentialDao().createOrUpdate(credential); } databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail); } public void sendConfirmationEmail(String email) throws HasConfirmedEmailException, InvalidApplicationException { UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(this.userScope.getUserId()); if (user.getEmail() != null) throw new HasConfirmedEmailException("User already has confirmed his Email."); apiContext.getUtilitiesService().getConfirmationEmailService().createConfirmationEmail( databaseRepository.getLoginConfirmationEmailDao(), apiContext.getUtilitiesService().getMailService(), email, this.userScope.getUserId() ); } private void mergeNewUserToOld(UserInfo newUser, UserInfo oldUser) throws InvalidApplicationException { CredentialEntity credential = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userId"), newUser.getId())).getSingle(); credential.setUserId(oldUser.getId()); databaseRepository.getCredentialDao().createOrUpdate(credential); } }