From 997b70870b1b04f61804379cf818dec14f7aa458 Mon Sep 17 00:00:00 2001 From: Daniel Palafox Date: Thu, 14 Mar 2024 16:37:56 -0600 Subject: [PATCH] feature: Update claim and reactivate methods to receive orcid and send email to associated email --- .../web/controllers/ClaimController.java | 45 ++++++++++++------ .../controllers/PasswordResetController.java | 46 ++++++++++++------- .../web/controllers/ClaimControllerTest.java | 16 +++++++ 3 files changed, 76 insertions(+), 31 deletions(-) diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ClaimController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ClaimController.java index 5d53a07009b..2714fd2e775 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ClaimController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/ClaimController.java @@ -6,10 +6,12 @@ import java.util.Locale; import javax.annotation.Resource; +import javax.persistence.NoResultException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; +import org.apache.jena.sparql.function.library.e; import org.jasypt.exceptions.EncryptionOperationNotPossibleException; import org.orcid.core.exception.OrcidBadRequestException; import org.orcid.core.manager.EncryptionManager; @@ -27,6 +29,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -201,23 +204,35 @@ public ModelAndView viewResendClaimEmail(@RequestParam(value = "email", required String email = resendClaimRequest.getEmail(); List errors = new ArrayList<>(); resendClaimRequest.setErrors(errors); - - if (!OrcidStringUtils.isEmailValid(email)) { - errors.add(getMessage("Email.resetPasswordForm.invalidEmail")); - return resendClaimRequest; - } - if (!emailManager.emailExists(email)) { - String message = getMessage("orcid.frontend.reset.password.email_not_found_1") + " " + email + " " + getMessage("orcid.frontend.reset.password.email_not_found_2"); - message += ""; - message += getMessage("orcid.frontend.reset.password.email_not_found_3"); - message += ""; - message += getMessage("orcid.frontend.reset.password.email_not_found_4"); - errors.add(message); - return resendClaimRequest; + String orcid = null; + if (OrcidStringUtils.isValidOrcid(email)) { + try{ + orcid = email; + email = emailManager.findPrimaryEmail(orcid).getEmail(); + } catch(NoResultException nre) { + errors.add(getMessage("Email.resetPasswordForm.error")); + return resendClaimRequest; + } + } else { + if (!OrcidStringUtils.isEmailValid(email)) { + errors.add(getMessage("Email.resetPasswordForm.invalidEmail")); + return resendClaimRequest; + } + + if (!emailManager.emailExists(email)) { + String message = getMessage("orcid.frontend.reset.password.email_not_found_1") + " " + email + " " + getMessage("orcid.frontend.reset.password.email_not_found_2"); + message += ""; + message += getMessage("orcid.frontend.reset.password.email_not_found_3"); + message += ""; + message += getMessage("orcid.frontend.reset.password.email_not_found_4"); + errors.add(message); + return resendClaimRequest; + } + + orcid = emailManager.findOrcidIdByEmail(email); } - - String orcid = emailManager.findOrcidIdByEmail(email); + ProfileEntity profile = profileEntityCacheManager.retrieve(orcid); if (profile != null && profile.getClaimed()) { diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PasswordResetController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PasswordResetController.java index 50cf734c34a..e2f5e4bcd8b 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PasswordResetController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PasswordResetController.java @@ -32,6 +32,7 @@ import org.orcid.frontend.spring.web.social.config.SocialSignInUtils; import org.orcid.frontend.web.forms.OneTimeResetPasswordForm; import org.orcid.frontend.web.util.CommonPasswords; +import org.orcid.jaxb.model.v3.release.record.Email; import org.orcid.jaxb.model.v3.release.record.Emails; import org.orcid.persistence.jpa.entities.ProfileEntity; import org.orcid.pojo.EmailRequest; @@ -343,24 +344,37 @@ private boolean isTokenExpired(PasswordResetToken passwordResetToken) { } @RequestMapping(value = "/sendReactivation.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON) - public ResponseEntity sendReactivation(@RequestParam("email") String email) throws UnsupportedEncodingException { + public ResponseEntity sendReactivation(@RequestParam(name="email", required=false) String email, @RequestParam(name="orcid", required=false) String orcid) throws UnsupportedEncodingException { - if (!email.contains("@")) { - email = URLDecoder.decode(email, "UTF-8"); - } - - email = OrcidStringUtils.filterEmailAddress(email); - String orcid = null; - - if (!validateEmailAddress(email)) { - String error = getMessage("Email.personalInfoForm.email"); - return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + if (email != null) { + if (!email.contains("@")) { + email = URLDecoder.decode(email, "UTF-8"); + } + + email = OrcidStringUtils.filterEmailAddress(email); + + if (!validateEmailAddress(email)) { + String error = getMessage("Email.personalInfoForm.email"); + return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + } else { + try{ + orcid = emailManager.findOrcidIdByEmail(email); + } catch(NoResultException nre) { + String error = getMessage("Email.resendClaim.invalidEmail"); + return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + } + } } else { - try{ - orcid = emailManager.findOrcidIdByEmail(email); - } catch(NoResultException nre) { - String error = getMessage("Email.resendClaim.invalidEmail"); - return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + if (!OrcidStringUtils.isValidOrcid(orcid)) { + String error = getMessage("Email.resetPasswordForm.error"); + return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + } else { + try{ + email = emailManager.findPrimaryEmail(orcid).getEmail(); + } catch(NoResultException nre) { + String error = getMessage("Email.resetPasswordForm.error"); + return ResponseEntity.ok("{\"sent\":false, \"error\":\"" + error + "\"}"); + } } } diff --git a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/ClaimControllerTest.java b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/ClaimControllerTest.java index 8aa63db0901..79b29e3d599 100644 --- a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/ClaimControllerTest.java +++ b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/ClaimControllerTest.java @@ -29,6 +29,7 @@ import org.orcid.frontend.email.RecordEmailSender; import org.orcid.jaxb.model.common.AvailableLocales; import org.orcid.jaxb.model.v3.release.common.Visibility; +import org.orcid.jaxb.model.v3.release.record.Email; import org.orcid.persistence.jpa.entities.ProfileEntity; import org.orcid.pojo.EmailRequest; import org.orcid.pojo.ajaxForm.Checkbox; @@ -96,6 +97,21 @@ public void testResendEmailFailIfTheProfileIsAlreadyClaimed() { assertFalse(emailRequest.getErrors().isEmpty()); } + @Test + public void testResendClaimEmailByOrcid() { + BindingResult bindingResult = mock(BindingResult.class); + when(bindingResult.hasErrors()).thenReturn(false); + Email email = new Email(); + email.setEmail("billie@holiday.com"); + when(emailManager.findPrimaryEmail("0000-0000-0000-0000")).thenReturn(email); + when(profileEntityCacheManager.retrieve(Mockito.anyString())).thenReturn(getProfileEntityToTestClaimResend(false)); + EmailRequest emailRequest = new EmailRequest(); + emailRequest.setEmail("0000-0000-0000-0000"); + emailRequest = claimController.resendClaimEmail(emailRequest); + assertNotNull(emailRequest); + assertNotNull(emailRequest.getSuccessMessage()); + } + @Test public void testResendClaimEmail() { BindingResult bindingResult = mock(BindingResult.class);