145 lines
6.3 KiB
Java
145 lines
6.3 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.commons.scope.user.UserScope;
|
|
import eu.eudat.data.DmpUserEntity;
|
|
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.model.UserContactInfo;
|
|
import eu.eudat.query.UserContactInfoQuery;
|
|
import eu.eudat.query.UserQuery;
|
|
import gr.cite.tools.data.query.Ordering;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
import java.util.*;
|
|
|
|
@Component
|
|
public class MergeEmailConfirmationManager {
|
|
private static Logger logger = LoggerFactory.getLogger(MergeEmailConfirmationManager.class);
|
|
private ApiContext apiContext;
|
|
private DatabaseRepository databaseRepository;
|
|
// private DmpRepository dmpRepository;
|
|
private final UserScope userScope;
|
|
private final QueryFactory queryFactory;
|
|
|
|
@Autowired
|
|
public MergeEmailConfirmationManager(ApiContext apiContext, UserScope userScope, QueryFactory queryFactory) {
|
|
this.apiContext = apiContext;
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
// this.dmpRepository = apiContext.getOperationsContext().getElasticRepository().getDmpRepository();
|
|
this.userScope = userScope;
|
|
this.queryFactory = queryFactory;
|
|
}
|
|
|
|
@Transactional
|
|
public String 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();
|
|
|
|
if (loginConfirmationEmail.getExpiresAt().compareTo(new Date()) < 0)
|
|
throw new TokenExpiredException("Token has expired.");
|
|
|
|
UserEntity userToBeMerged = this.queryFactory.query(UserQuery.class).ids(loginConfirmationEmail.getUserId()).first();
|
|
String userToBeMergedEmail = ""; //TODO userToBeMerged.getEmail();
|
|
try {
|
|
Map<String, Object> map = new ObjectMapper().readValue(loginConfirmationEmail.getData(), HashMap.class);
|
|
UUID otherUserId = UUID.fromString((String) map.get("userId"));
|
|
UserEntity user = this.queryFactory.query(UserQuery.class).ids(otherUserId).first();
|
|
|
|
// Checks if mail is used by another user. If it is, merges the new the old.
|
|
mergeNewUserToOld(user, userToBeMerged, Integer.valueOf((String) map.get("provider")));
|
|
//expireUserToken(userToBeMerged); //TODO: Authn
|
|
loginConfirmationEmail.setIsConfirmed(true);
|
|
databaseRepository.getLoginConfirmationEmailDao().createOrUpdate(loginConfirmationEmail);
|
|
} catch (Exception e) {
|
|
logger.error(e.getMessage(), e);
|
|
}
|
|
|
|
return userToBeMergedEmail;
|
|
}
|
|
|
|
public void sendMergeConfirmationEmail(String email, Integer provider) throws HasConfirmedEmailException, InvalidApplicationException {
|
|
UserEntity user = this.queryFactory.query(UserQuery.class).ids(this.userScope.getUserId()).first();
|
|
UserContactInfoQuery query = this.queryFactory.query(UserContactInfoQuery.class).userIds(user.getId());
|
|
query.setOrder(new Ordering().addAscending(UserContactInfo._ordinal));
|
|
if (query.first().getValue() != null && !query.first().getValue().equals(email)) {
|
|
apiContext.getUtilitiesService().getConfirmationEmailService().sentMergeConfirmationEmail(
|
|
databaseRepository.getLoginConfirmationEmailDao(),
|
|
email,
|
|
user,
|
|
provider
|
|
);
|
|
}
|
|
}
|
|
|
|
@Transactional
|
|
private void mergeNewUserToOld(UserEntity newUser, UserEntity oldUser, Integer provider) throws InvalidApplicationException {
|
|
// UserCredentialEntity credential = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.and(builder.equal(root.get("userId"), oldUser.getId()), builder.equal(root.get("provider"), provider))).getSingle();
|
|
// credential.setUserId(newUser.getId());
|
|
// databaseRepository.getCredentialDao().createOrUpdate(credential);
|
|
// List<DmpUserEntity> userDmps = databaseRepository.getUserDmpDao().asQueryable().where((builder, root) -> builder.equal(root.get("user"), oldUser)).toList();
|
|
// userDmps.forEach(userDmp -> {
|
|
// userDmp.setUserId(newUser.getId());
|
|
// databaseRepository.getUserDmpDao().createOrUpdate(userDmp);
|
|
// });
|
|
//TODO
|
|
// try {
|
|
//
|
|
// DmpCriteria dmpCriteria = new DmpCriteria();
|
|
// dmpCriteria.setCollaborators(Collections.singletonList(oldUser.getId()));
|
|
// List<Dmp> elasticDmpsIds = dmpRepository.query(dmpCriteria);
|
|
// for(Dmp dmpId: elasticDmpsIds){
|
|
// Dmp dmp = dmpRepository.findDocument(dmpId.getId().toString());
|
|
// if(dmp.getDatasets() != null) {
|
|
// dmp.getDatasets().forEach(dataset -> {
|
|
// if(dataset.getCollaborators() != null) {
|
|
// for (Collaborator collaborator : dataset.getCollaborators()) {
|
|
// if (collaborator.getId().equals(oldUser.getId().toString())) {
|
|
// collaborator.setId(newUser.getId().toString());
|
|
// collaborator.setName(newUser.getName());
|
|
// }
|
|
// }
|
|
// }
|
|
// });
|
|
// }
|
|
// if(dmp.getCollaborators() != null) {
|
|
// for (Collaborator collaborator : dmp.getCollaborators()) {
|
|
// if (collaborator.getId().equals(oldUser.getId().toString())) {
|
|
// collaborator.setId(newUser.getId().toString());
|
|
// collaborator.setName(newUser.getName());
|
|
// }
|
|
// }
|
|
// }
|
|
// dmpRepository.createOrUpdate(dmp);
|
|
// }
|
|
// }
|
|
// catch (IOException e){
|
|
// logger.warn("Warning: Could not fetch dmps from elastic.", e);
|
|
// }
|
|
oldUser.setIsActive(IsActive.Inactive);
|
|
//TODO oldUser.setEmail(null);
|
|
// List<UserCredentialEntity> credentials = databaseRepository.getCredentialDao().asQueryable().where((builder, root) -> builder.equal(root.get("userId"), oldUser.getId())).toList();
|
|
// credentials.forEach(cred -> {
|
|
// if (cred.getId() != credential.getId()) {
|
|
// databaseRepository.getCredentialDao().delete(cred);
|
|
// }
|
|
// });
|
|
// databaseRepository.getUserInfoDao().createOrUpdate(oldUser);
|
|
}
|
|
}
|