From be468953f1704871f12b286cb7c548a5e7df1972 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Tue, 13 Aug 2024 14:41:02 -0600 Subject: [PATCH] When more than one emails with the same domain are verified, the default visibility should match the less restrictive from the verified emails --- .../impl/ProfileEmailDomainManagerImpl.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/orcid-core/src/main/java/org/orcid/core/manager/v3/impl/ProfileEmailDomainManagerImpl.java b/orcid-core/src/main/java/org/orcid/core/manager/v3/impl/ProfileEmailDomainManagerImpl.java index dbb8db060e3..c2eb6ae2eb9 100644 --- a/orcid-core/src/main/java/org/orcid/core/manager/v3/impl/ProfileEmailDomainManagerImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/manager/v3/impl/ProfileEmailDomainManagerImpl.java @@ -8,7 +8,10 @@ import org.orcid.persistence.dao.EmailDomainDao; import org.orcid.persistence.dao.ProfileEmailDomainDao; import org.orcid.persistence.jpa.entities.EmailDomainEntity; +import org.orcid.persistence.jpa.entities.EmailEntity; import org.orcid.persistence.jpa.entities.ProfileEmailDomainEntity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -20,14 +23,16 @@ * */ public class ProfileEmailDomainManagerImpl extends ProfileEmailDomainManagerReadOnlyImpl implements ProfileEmailDomainManager { + private static final Logger LOGGER = LoggerFactory.getLogger(ProfileEmailDomainManagerImpl.class); + @Resource protected ProfileEmailDomainDao profileEmailDomainDao; @Resource protected EmailDomainDao emailDomainDao; - @Resource - protected EmailDao emailDao; + @Resource(name = "emailDaoReadOnly") + protected EmailDao emailDaoReadOnly; private static final String DEFAULT_DOMAIN_VISIBILITY = Visibility.PRIVATE.toString().toUpperCase(); @@ -66,13 +71,47 @@ public void updateEmailDomains(String orcid, org.orcid.pojo.ajaxForm.Emails newE public void processDomain(String orcid, String email) { String domain = email.split("@")[1]; EmailDomainEntity domainInfo = emailDomainDao.findByEmailDomain(domain); + String domainVisibility = DEFAULT_DOMAIN_VISIBILITY; // Check if email is professional if (domainInfo != null && domainInfo.getCategory().equals(EmailDomainEntity.DomainCategory.PROFESSIONAL)) { ProfileEmailDomainEntity existingDomain = profileEmailDomainDao.findByEmailDomain(orcid, domain); // ADD NEW DOMAIN IF ONE DOESN'T EXIST if (existingDomain == null) { - profileEmailDomainDao.addEmailDomain(orcid, domain, DEFAULT_DOMAIN_VISIBILITY); + // Verify the user doesn't have more emails with that domain + List existingEmails = emailDaoReadOnly.findByOrcid(orcid, System.currentTimeMillis()); + if(existingEmails != null && existingEmails.size() > 1) { + for(EmailEntity emailEntity : existingEmails) { + //If it is not the same emails that is being verified and it is verified + if(!email.equals(emailEntity.getEmail()) && emailEntity.getVerified()) { + try { + String emailEntityDomain = (emailEntity.getEmail() == null) ? null : (email.split("@")[1]); + // If one of the existing emails have the same domain as the email being verified check the visibility and select the less restrictive + if(domain.equals(emailEntityDomain)){ + String entityVisibility = emailEntity.getVisibility(); + domainVisibility = getLessRestrictiveVisibility(domainVisibility, entityVisibility); + } + } catch (Exception e) { + LOGGER.warn("Could not get email domain from email entity " + emailEntity.getEmail(), e); + } + } + } + } + profileEmailDomainDao.addEmailDomain(orcid, domain, domainVisibility); } } } + + private String getLessRestrictiveVisibility(String a, String b) { + String visibility = DEFAULT_DOMAIN_VISIBILITY; + if(Visibility.PUBLIC.name().equals(a) || Visibility.PUBLIC.name().equals(b)) { + visibility = Visibility.PUBLIC.name(); + } else if(a.equals(b)) { + visibility = a; + } else if(Visibility.PRIVATE.name().equals(a)) { + visibility = b; + } else if(Visibility.PRIVATE.name().equals(b)) { + visibility = a; + } + return visibility; + } }