diff --git a/orcid-api-web/src/main/java/org/orcid/api/memberV2/server/delegator/impl/MemberV2ApiServiceDelegatorImpl.java b/orcid-api-web/src/main/java/org/orcid/api/memberV2/server/delegator/impl/MemberV2ApiServiceDelegatorImpl.java index e9ca5a58e3a..87dcc0474d4 100644 --- a/orcid-api-web/src/main/java/org/orcid/api/memberV2/server/delegator/impl/MemberV2ApiServiceDelegatorImpl.java +++ b/orcid-api-web/src/main/java/org/orcid/api/memberV2/server/delegator/impl/MemberV2ApiServiceDelegatorImpl.java @@ -257,6 +257,9 @@ public Response viewRecord(String orcid) { ActivityUtils.cleanEmptyFields(record.getActivitiesSummary()); sourceUtils.setSourceName(record.getActivitiesSummary()); } + if(record.getPerson() != null && record.getPerson().getEmails() != null) { + processProfessionalEmails(record.getPerson().getEmails()); + } ElementUtils.setPathToRecord(record, orcid); Api2_0_LastModifiedDatesHelper.calculateLastModified(record); return Response.ok(record).build(); @@ -1066,6 +1069,7 @@ public Response viewPersonalDetails(String orcid) { public Response viewPerson(String orcid) { Person person = personDetailsManagerReadOnly.getPersonDetails(orcid); orcidSecurityManager.checkAndFilter(orcid, person); + processProfessionalEmails(person.getEmails()); ElementUtils.setPathToPerson(person, orcid); Api2_0_LastModifiedDatesHelper.calculateLastModified(person); sourceUtils.setSourceName(person); @@ -1114,6 +1118,9 @@ private void validateSearchParams(Map> queryMap) { } private void processProfessionalEmails(Emails emails) { + if(emails == null || emails.getEmails() == null) { + return; + } for (Email email : emails.getEmails()) { if (email.isVerified()) { String domain = email.getEmail().split("@")[1]; diff --git a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_EmailsTest.java b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_EmailsTest.java index 3768a1cb475..f97c2b1db61 100644 --- a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_EmailsTest.java +++ b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_EmailsTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -69,13 +70,51 @@ public void testViewEmailsWrongToken() { } @Test - public void testViewEmailsReadPublic() { + public void testViewEmailsReadPublic_withSourceClient() { SecurityContextTestUtils.setUpSecurityContextForClientOnly("APP-5555555555555555", ScopePathType.READ_PUBLIC); Response r = serviceDelegator.viewEmails(ORCID); Emails element = (Emails) r.getEntity(); assertNotNull(element); assertEquals("/0000-0000-0000-0003/email", element.getPath()); - Utils.assertIsPublicOrSource(element, "APP-5555555555555555"); + assertEquals(4, element.getEmails().size()); + + List emails = new ArrayList<>(); + emails.add("public_0000-0000-0000-0003@test.orcid.org"); + emails.add("public_0000-0000-0000-0003@orcid.org"); + emails.add("limited_0000-0000-0000-0003@test.orcid.org"); + emails.add("private_0000-0000-0000-0003@test.orcid.org"); + + for(Email e : element.getEmails()) { + if(!emails.contains(e.getEmail())) { + fail(e.getEmail() + " is not in the email list"); + } + emails.remove(e.getEmail()); + } + + assertTrue(emails.isEmpty()); + } + + @Test + public void testViewEmailsReadPublic_withOtherClient() { + SecurityContextTestUtils.setUpSecurityContextForClientOnly("APP-5555555555555556", ScopePathType.READ_PUBLIC); + Response r = serviceDelegator.viewEmails(ORCID); + Emails element = (Emails) r.getEntity(); + assertNotNull(element); + assertEquals("/0000-0000-0000-0003/email", element.getPath()); + assertEquals(2, element.getEmails().size()); + + List emails = new ArrayList<>(); + emails.add("public_0000-0000-0000-0003@test.orcid.org"); + emails.add("public_0000-0000-0000-0003@orcid.org"); + + for(Email e : element.getEmails()) { + if(!emails.contains(e.getEmail())) { + fail(e.getEmail() + " is not in the email list"); + } + emails.remove(e.getEmail()); + } + + assertTrue(emails.isEmpty()); } @Test diff --git a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadPersonTest.java b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadPersonTest.java index 863aefb1011..1d261320698 100644 --- a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadPersonTest.java +++ b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadPersonTest.java @@ -84,6 +84,7 @@ public void testViewPersonReadPublic() { assertNotNull(element); assertEquals("/0000-0000-0000-0003/person", element.getPath()); Utils.assertIsPublicOrSource(element, "APP-5555555555555555"); + fail("TEST EMAILS"); } @Test diff --git a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadRecordTest.java b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadRecordTest.java index 411910f718e..5e9d6b77b92 100644 --- a/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadRecordTest.java +++ b/orcid-api-web/src/test/java/org/orcid/api/memberV2/server/delegator/MemberV2ApiServiceDelegator_ReadRecordTest.java @@ -102,6 +102,7 @@ public void testViewRecordWrongScope() { Utils.assertIsPublicOrSource(record.getActivitiesSummary(), SecurityContextTestUtils.DEFAULT_CLIENT_ID); assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath()); Utils.assertIsPublicOrSource(record.getPerson(), SecurityContextTestUtils.DEFAULT_CLIENT_ID); + } @Test @@ -115,6 +116,7 @@ public void testViewRecordReadPublic() { Utils.assertIsPublicOrSource(record.getActivitiesSummary(), "APP-5555555555555555"); assertEquals("/0000-0000-0000-0003/person", record.getPerson().getPath()); Utils.assertIsPublicOrSource(record.getPerson(), "APP-5555555555555555"); + fail("TEST EMAILS"); } @Test(expected = OrcidUnauthorizedException.class) diff --git a/orcid-api-web/src/test/java/org/orcid/test/helper/Utils.java b/orcid-api-web/src/test/java/org/orcid/test/helper/Utils.java index 291879b117d..8f1750d0391 100644 --- a/orcid-api-web/src/test/java/org/orcid/test/helper/Utils.java +++ b/orcid-api-web/src/test/java/org/orcid/test/helper/Utils.java @@ -151,12 +151,12 @@ public static void assertIsPublicOrSource(Person p, String sourceId) { } assertIsPublicOrSource(p.getAddresses(), sourceId); assertIsPublicOrSource(p.getBiography(), sourceId); - assertIsPublicOrSource(p.getEmails(), sourceId); assertIsPublicOrSource(p.getExternalIdentifiers(), sourceId); assertIsPublicOrSource(p.getKeywords(), sourceId); assertIsPublicOrSource(p.getName(), sourceId); assertIsPublicOrSource(p.getOtherNames(), sourceId); assertIsPublicOrSource(p.getResearcherUrls(), sourceId); + // EMAILS ARE NOT TESTED! Source on professional emails is overwritten, so, you must verify the email on each test! } public static void verifyLastModified(LastModifiedDate l) { diff --git a/orcid-core/src/main/java/org/orcid/core/manager/read_only/impl/PersonDetailsManagerReadOnlyImpl.java b/orcid-core/src/main/java/org/orcid/core/manager/read_only/impl/PersonDetailsManagerReadOnlyImpl.java index 04b72cfee70..2663944909d 100644 --- a/orcid-core/src/main/java/org/orcid/core/manager/read_only/impl/PersonDetailsManagerReadOnlyImpl.java +++ b/orcid-core/src/main/java/org/orcid/core/manager/read_only/impl/PersonDetailsManagerReadOnlyImpl.java @@ -142,7 +142,6 @@ public Person getPersonDetails(String orcid) { if (emails.getEmails() != null) { Emails filteredEmails = new Emails(); filteredEmails.setEmails(new ArrayList(emails.getEmails().stream().filter(e -> e.isVerified()).collect(Collectors.toList()))); - processProfessionalEmails(filteredEmails); person.setEmails(filteredEmails); } return person; @@ -201,35 +200,9 @@ public Person getPublicPersonDetails(String orcid) { if (emails.getEmails() != null) { Emails filteredEmails = new Emails(); filteredEmails.setEmails(new ArrayList(emails.getEmails())); - processProfessionalEmails(filteredEmails); person.setEmails(filteredEmails); } return person; } - - private void processProfessionalEmails(Emails emails) { - for (Email email : emails.getEmails()) { - if (email.isVerified()) { - String domain = email.getEmail().split("@")[1]; - List domainsInfo = emailDomainManager.findByEmailDomain(domain); - String category = EmailDomainEntity.DomainCategory.UNDEFINED.name(); - // Set appropriate source name and source id for professional emails - if (domainsInfo != null) { - for(EmailDomainEntity domainInfo: domainsInfo) { - category = domainInfo.getCategory().name(); - if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) { - break; - } - } - if(StringUtils.equalsIgnoreCase(category, EmailDomainEntity.DomainCategory.PROFESSIONAL.name())) { - if(email.getSource() == null) { - email.setSource(new Source()); - } - email.setSource(sourceEntityUtils.convertEmailSourceToOrcidValidator(email.getSource())); - } - } - } - } - } }