Skip to content

Commit

Permalink
Log short error for some exceptions and fix the affiliation start dat…
Browse files Browse the repository at this point in the history
…e validation on registration
  • Loading branch information
amontenegro committed Mar 20, 2024
1 parent 53e7616 commit 031819c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import org.orcid.core.exception.OrcidDuplicatedElementException;
import org.orcid.core.exception.OrcidInvalidScopeException;
import org.orcid.core.exception.OrcidNoBioException;
import org.orcid.core.exception.OrcidNoResultException;
import org.orcid.core.exception.OrcidNonPublicElementException;
import org.orcid.core.exception.OrcidNotClaimedException;
import org.orcid.core.exception.OrcidNotificationException;
import org.orcid.core.exception.OrcidUnauthorizedException;
import org.orcid.core.exception.OrcidValidationException;
import org.orcid.core.locale.LocaleManager;
import org.orcid.core.manager.OrcidSecurityManager;
Expand Down Expand Up @@ -138,6 +140,10 @@ public Response toResponse(Throwable t) {
logShortError(t, clientId);
} else if (t instanceof TokenMgrError) {
logShortError(t, clientId);
} else if (t instanceof OrcidNoResultException) {
logShortError(t, clientId);
} else if (t instanceof OrcidUnauthorizedException) {
logShortError(t, clientId);
} else {
LOGGER.error("An exception has occured processing request from client " + clientId, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.orcid.core.common.manager.EventManager;
import org.orcid.core.constants.EmailConstants;
import org.orcid.core.constants.OrcidOauth2Constants;
import org.orcid.core.manager.EncryptionManager;
import org.orcid.core.manager.ProfileEntityCacheManager;
import org.orcid.core.manager.RegistrationManager;
import org.orcid.core.common.manager.EventManager;
import org.orcid.core.manager.v3.OrcidSearchManager;
import org.orcid.core.manager.v3.ProfileHistoryEventManager;
import org.orcid.core.manager.v3.read_only.AffiliationsManagerReadOnly;
Expand All @@ -43,16 +43,12 @@
import org.orcid.frontend.spring.SocialAjaxAuthenticationSuccessHandler;
import org.orcid.frontend.spring.web.social.config.SocialSignInUtils;
import org.orcid.frontend.web.controllers.helper.OauthHelper;
import org.orcid.frontend.web.controllers.helper.SearchOrcidSolrCriteria;
import org.orcid.frontend.web.util.RecaptchaVerifier;
import org.orcid.jaxb.model.common.AvailableLocales;
import org.orcid.jaxb.model.message.CreationMethod;
import org.orcid.jaxb.model.v3.release.common.Visibility;
import org.orcid.jaxb.model.v3.release.record.Affiliation;
import org.orcid.jaxb.model.v3.release.record.AffiliationType;
import org.orcid.jaxb.model.v3.release.record.Employment;
import org.orcid.persistence.constants.SendEmailFrequency;
import org.orcid.pojo.OrgDisambiguated;
import org.orcid.pojo.Redirect;
import org.orcid.pojo.ajaxForm.AffiliationForm;
import org.orcid.pojo.ajaxForm.Date;
Expand Down Expand Up @@ -337,17 +333,17 @@ public void validateRegistrationFields(HttpServletRequest request, Registration
if (!AffiliationType.EMPLOYMENT.equals(AffiliationType.fromValue(affiliationForm.getAffiliationType().getValue()))) {
setError(affiliationForm.getAffiliationType(), "Invalid affiliation type");
}
if (reg.getAffiliationForm().getDepartmentName() != null) {
if (affiliationForm.getDepartmentName() != null) {
if (affiliationForm.getDepartmentName().getValue() != null && affiliationForm.getDepartmentName().getValue().trim().length() > 1000) {
setError(affiliationForm.getDepartmentName(), "common.length_less_1000");
}
}
if (reg.getAffiliationForm().getRoleTitle() != null) {
if (affiliationForm.getRoleTitle() != null) {
if (!PojoUtil.isEmpty(affiliationForm.getRoleTitle()) && affiliationForm.getRoleTitle().getValue().trim().length() > 1000) {
setError(affiliationForm.getRoleTitle(), "common.length_less_1000");
}
}
if (reg.getAffiliationForm().getStartDate() != null) {
if (affiliationForm.getStartDate() != null) {
if(!validDate(affiliationForm.getStartDate())) {
setError(affiliationForm.getStartDate(), "common.dates.invalid");
}
Expand All @@ -361,6 +357,9 @@ public void validateRegistrationFields(HttpServletRequest request, Registration
copyErrors(reg.getPassword(), reg);
copyErrors(reg.getPasswordConfirm(), reg);
copyErrors(reg.getTermsOfUse(), reg);
if(reg.getAffiliationForm() != null && reg.getAffiliationForm().getStartDate() != null) {
copyErrors(reg.getAffiliationForm().getStartDate(), reg);
}

additionalEmailsValidateOnRegister(request, reg);
for (Text emailAdditional : reg.getEmailsAdditional()) {
Expand Down Expand Up @@ -612,36 +611,36 @@ protected boolean validDate(Date date) {
.toFormatter(),
new DateTimeFormatterBuilder().appendPattern("yyyyMM").parseDefaulting(ChronoField.DAY_OF_MONTH, 1).toFormatter(),
new DateTimeFormatterBuilder().appendPattern("yyyyMMdd").parseStrict().toFormatter() };
String dateString = date.getYear();
// If the month is empty and day provided is an invalid date
if (StringUtils.isBlank(date.getMonth())) {
if (!StringUtils.isBlank(date.getDay())) {
String dateString = date.getYear();

// If year is blank and month or day is not, then it is invalid
if (StringUtils.isBlank(date.getYear())) {
if (!StringUtils.isBlank(date.getMonth()) || !StringUtils.isBlank(date.getDay())) {
return false;
}
}
else if (StringUtils.isBlank(date.getYear())) {
if (!StringUtils.isBlank(date.getDay()) && !StringUtils.isBlank(date.getMonth())) {
} else if (StringUtils.isBlank(date.getMonth())) {
// If the month is empty and day is not empty, then it is invalid
if (!StringUtils.isBlank(date.getDay())) {
return false;
}
}
else {
dateString += date.getMonth();
} else {
dateString += StringUtils.leftPad(date.getMonth(), 2, '0');
if (!StringUtils.isBlank(date.getDay())) {
dateString += date.getDay();
dateString += StringUtils.leftPad(date.getDay(), 2, '0');
}
}

for (DateTimeFormatter formatter : formatters) {
try {
LocalDate localDate = LocalDate.parse(dateString, formatter);
if (PojoUtil.isEmpty(date.getDay()) || localDate.getDayOfMonth() == Integer.parseInt(date.getDay())) {
// formatter will correct day to last valid day of month if
// it is too great
if(StringUtils.isBlank(dateString)) {
return true;
} else {
for (DateTimeFormatter formatter : formatters) {
try {
LocalDate.parse(dateString, formatter);
return true;
} catch (DateTimeParseException e) {
}
} catch (DateTimeParseException e) {
}
}
}
return false;
}

Expand Down

0 comments on commit 031819c

Please sign in to comment.