Skip to content

Commit

Permalink
feature: Update claim and reactivate methods to receive orcid and sen…
Browse files Browse the repository at this point in the history
…d email to associated email
  • Loading branch information
Daniel Palafox committed Mar 14, 2024
1 parent fa9dd3e commit 997b708
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -201,23 +204,35 @@ public ModelAndView viewResendClaimEmail(@RequestParam(value = "email", required
String email = resendClaimRequest.getEmail();
List<String> 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 += "<a href=\"https://support.orcid.org/\">";
message += getMessage("orcid.frontend.reset.password.email_not_found_3");
message += "</a>";
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 += "<a href=\"https://support.orcid.org/\">";
message += getMessage("orcid.frontend.reset.password.email_not_found_3");
message += "</a>";
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "\"}");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("[email protected]");
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);
Expand Down

0 comments on commit 997b708

Please sign in to comment.