diff --git a/src/main/java/edu/harvard/iq/dataverse/authorization/groups/impl/affiliation/AffiliationGroupServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/authorization/groups/impl/affiliation/AffiliationGroupServiceBean.java index c42db14099e..dae9c51907f 100644 --- a/src/main/java/edu/harvard/iq/dataverse/authorization/groups/impl/affiliation/AffiliationGroupServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/authorization/groups/impl/affiliation/AffiliationGroupServiceBean.java @@ -3,11 +3,13 @@ import edu.harvard.iq.dataverse.RoleAssigneeServiceBean; import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord; import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean; +import org.apache.commons.lang.StringUtils; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.inject.Named; import javax.persistence.*; +import java.util.Arrays; import java.util.List; import java.util.logging.Logger; @@ -50,14 +52,24 @@ public AffiliationGroup getByDisplayName(String displayname) { } } - public AffiliationGroup getByEmailDomain(String emaildomain) { - try { - TypedQuery namedQuery = em.createNamedQuery("AffiliationGroup.findByEmailDomain", AffiliationGroup.class); - namedQuery.setParameter("emailDomain", "%" + emaildomain.toUpperCase() + "%"); - return namedQuery.getSingleResult(); - } catch (NoResultException nre) { - return null; + public AffiliationGroup find(String userEmail) { + String topLevelDomain = null; + String domain = userEmail.substring(userEmail.indexOf("@")+1).trim(); + long count = domain.chars().filter(ch -> ch == '.').count(); + if (count > 1) { + int secondLastIndex = StringUtils.lastIndexOf(domain, '.', domain.lastIndexOf(".") - 1); + topLevelDomain = domain.substring(secondLastIndex + 1); + } else { + topLevelDomain = domain; } + AffiliationGroup group = matchByTopLevelEmailDomain(topLevelDomain); + if (group != null) { + String emaildomain = group.getEmaildomain(); + List domainValues = Arrays.asList(emaildomain.split("\\s*,\\s*")); + if (domainValues.contains(topLevelDomain)) + return group; + } + return null; } public List findAll() { @@ -121,4 +133,14 @@ public Long getAffiliationGroupsCount() { public AffiliationGroup find(Object pk) { return (AffiliationGroup) em.find(AffiliationGroup.class, pk); } + + private AffiliationGroup matchByTopLevelEmailDomain(String emaildomain) { + try { + TypedQuery namedQuery = em.createNamedQuery("AffiliationGroup.findByEmailDomain", AffiliationGroup.class); + namedQuery.setParameter("emailDomain", "%" + emaildomain.toUpperCase() + "%"); + return namedQuery.getSingleResult(); + } catch (NoResultException nre) { + return null; + } + } } diff --git a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/builtin/DataverseUserPage.java b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/builtin/DataverseUserPage.java index 724b165e2e3..39661b10f4b 100644 --- a/src/main/java/edu/harvard/iq/dataverse/authorization/providers/builtin/DataverseUserPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/authorization/providers/builtin/DataverseUserPage.java @@ -230,20 +230,8 @@ public void validateUserEmail(FacesContext context, UIComponent toValidate, Obje logger.info("Email is not valid: " + userEmail); return; } - - String topLevelDomain = null; - String domain = userEmail.substring(userEmail.indexOf("@")+1).trim(); - long count = domain.chars().filter(ch -> ch == '.').count(); - if (count > 1) { - int secondLastIndex = StringUtils.lastIndexOf(domain, '.', domain.lastIndexOf(".") - 1); - topLevelDomain = domain.substring(secondLastIndex + 1); - } else { - topLevelDomain = domain; - } - AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(topLevelDomain); - String emaildomain = group.getEmaildomain(); - List domainValues = Arrays.asList(emaildomain.split("\\s*,\\s*")); - if (group == null || !domainValues.contains(topLevelDomain)) { + AffiliationGroup group = affiliationGroupServiceBean.find(userEmail); + if (group == null) { ((UIInput) toValidate).setValid(true); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, BundleUtil.getStringFromBundle("user.email.domain.invalid"), null); context.addMessage(toValidate.getClientId(context), message); @@ -324,8 +312,7 @@ public String save() { PasswordEncryption.getLatestVersionNumber()); String userEmail = userDisplayInfo.getEmailAddress(); - String domain = userEmail.substring(userEmail.indexOf("@")+1).trim(); - AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(domain); + AffiliationGroup group = affiliationGroupServiceBean.find(userEmail); String affiliation = (group == null) ? "OTHER" : group.getDisplayName(); userDisplayInfo.setAffiliation(affiliation); @@ -760,8 +747,7 @@ public List getAffiliationList() { if (editMode == EditMode.SUPPORT) { if (sendFeedbackDialog.isLoggedIn()) { String userEmail = sendFeedbackDialog.loggedInUserEmail(); - String domain = userEmail.substring(userEmail.indexOf("@")+1).trim(); - AffiliationGroup group = affiliationGroupServiceBean.getByEmailDomain(domain); + AffiliationGroup group = affiliationGroupServiceBean.find(userEmail); if(group != null) { affiliation = group.getDisplayName(); }