87 lines
4.2 KiB
Java
87 lines
4.2 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.old.EmailConfirmation;
|
|
import eu.eudat.data.UserEntity;
|
|
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 eu.eudat.query.UserQuery;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
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;
|
|
private final QueryFactory queryFactory;
|
|
|
|
@Autowired
|
|
public EmailConfirmationManager(ApiContext apiContext, UserScope userScope, QueryFactory queryFactory) {
|
|
this.apiContext = apiContext;
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
this.userScope = userScope;
|
|
this.queryFactory = queryFactory;
|
|
}
|
|
|
|
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();
|
|
|
|
UserEntity user = this.queryFactory.query(UserQuery.class).ids(loginConfirmationEmail.getUserId()).first();
|
|
|
|
// if (user.getEmail() != null) //TODO
|
|
// 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 = this.queryFactory.query(UserQuery.class).emails(loginConfirmationEmail.getEmail()).count();
|
|
if (existingUsers > 0) {
|
|
// UserCredentialEntity credential = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userId"), user.getId())).getSingle();
|
|
// credential.setEmail(loginConfirmationEmail.getEmail()); //TODO
|
|
// databaseRepository.getCredentialDao().createOrUpdate(credential);
|
|
UserEntity oldUser = this.queryFactory.query(UserQuery.class).emails(loginConfirmationEmail.getEmail()).first();
|
|
mergeNewUserToOld(user, oldUser);
|
|
// expireUserToken(user); //TODO: Authn
|
|
databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail);
|
|
return;
|
|
}
|
|
|
|
// user.setEmail(loginConfirmationEmail.getEmail()); //TODO
|
|
// databaseRepository.getUserInfoDao().createOrUpdate(user);
|
|
// UserCredentialEntity credential = databaseRepository.getCredentialDao().asQueryable()
|
|
// .where((builder, root) -> builder.equal(root.get("userId"), user.getId())).getSingle();
|
|
// if(credential.getEmail() == null){//TODO
|
|
// credential.setEmail(user.getEmail());
|
|
// databaseRepository.getCredentialDao().createOrUpdate(credential);
|
|
// }
|
|
databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail);
|
|
}
|
|
|
|
public void sendConfirmationEmail(String email) throws HasConfirmedEmailException, InvalidApplicationException {
|
|
UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first();
|
|
// if (user.getEmail() != null) //TODO
|
|
// throw new HasConfirmedEmailException("User already has confirmed his Email.");
|
|
|
|
apiContext.getUtilitiesService().getConfirmationEmailService().sentConfirmationEmail(databaseRepository.getLoginConfirmationEmailDao(), email, user);
|
|
}
|
|
|
|
private void mergeNewUserToOld(UserEntity newUser, UserEntity oldUser) throws InvalidApplicationException {
|
|
// UserCredentialEntity credential = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userId"), newUser.getId())).getSingle();
|
|
// credential.setUserId(oldUser.getId());
|
|
// databaseRepository.getCredentialDao().createOrUpdate(credential);
|
|
}
|
|
}
|