use of javax.persistence.NoResultException in project ORCID-Source by ORCID.
the class MemberV3ApiServiceDelegator_ServicesTest method testDeleteService.
@Test
public void testDeleteService() {
SecurityContextTestUtils.setUpSecurityContext("0000-0000-0000-0002", ScopePathType.READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
Response response = serviceDelegator.viewService("0000-0000-0000-0002", 1006L);
assertNotNull(response);
Service service = (Service) response.getEntity();
assertNotNull(service);
response = serviceDelegator.deleteAffiliation("0000-0000-0000-0002", 1006L);
assertNotNull(response);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
try {
serviceDelegator.viewService("0000-0000-0000-0002", 1006L);
fail();
} catch (NoResultException nre) {
} catch (Exception e) {
fail();
}
}
use of javax.persistence.NoResultException in project ORCID-Source by ORCID.
the class PasswordResetController method issuePasswordResetRequest.
@RequestMapping(value = "/reset-password.json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<EmailRequest> issuePasswordResetRequest(HttpServletRequest request, @RequestBody EmailRequest passwordResetRequest) {
for (String param : request.getParameterMap().keySet()) {
if (!RESET_PASSWORD_PARAMS_WHITELIST.contains(param)) {
// found parameter that has not been white-listed
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
}
List<String> errors = new ArrayList<>();
passwordResetRequest.setErrors(errors);
if (!validateEmailAddress(passwordResetRequest.getEmail())) {
errors.add(getMessage("Email.resetPasswordForm.invalidEmail"));
return new ResponseEntity<>(passwordResetRequest, HttpStatus.OK);
}
try {
String orcid = emailManager.findOrcidIdByEmail(passwordResetRequest.getEmail());
ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
if (profile == null) {
String message = getMessage("orcid.frontend.reset.password.email_not_found_1") + " " + passwordResetRequest.getEmail() + " " + getMessage("orcid.frontend.reset.password.email_not_found_2");
message += "<a href=\"mailto: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 new ResponseEntity<>(passwordResetRequest, HttpStatus.OK);
}
if (profile.getDeactivationDate() != null) {
String message = getMessage("orcid.frontend.reset.password.disabled_account_1");
message += "<a href=\"/help/contact-us\">";
message += getMessage("orcid.frontend.reset.password.disabled_account_2");
message += "</a>";
errors.add(message);
return new ResponseEntity<>(passwordResetRequest, HttpStatus.OK);
}
registrationManager.resetUserPassword(passwordResetRequest.getEmail(), orcid, profile.getClaimed());
passwordResetRequest.setSuccessMessage(getMessage("orcid.frontend.reset.password.successfulReset") + " " + passwordResetRequest.getEmail());
} catch (NoResultException nre) {
errors.add(getMessage("Email.resetPasswordForm.error"));
}
return new ResponseEntity<>(passwordResetRequest, HttpStatus.OK);
}
use of javax.persistence.NoResultException in project ORCID-Source by ORCID.
the class OrcidRefreshTokenChecker method validateRequest.
public void validateRequest(String grantType, TokenRequest tokenRequest, Long requestTimeInMillis) {
String authorization = tokenRequest.getRequestParameters().get(OrcidOauth2Constants.AUTHORIZATION);
String clientId = tokenRequest.getClientId();
String scopes = tokenRequest.getRequestParameters().get(OAuth2Utils.SCOPE);
Long expireIn = tokenRequest.getRequestParameters().containsKey(OrcidOauth2Constants.EXPIRES_IN) ? Long.valueOf(tokenRequest.getRequestParameters().get(OrcidOauth2Constants.EXPIRES_IN)) : 0L;
String refreshToken = tokenRequest.getRequestParameters().get(OrcidOauth2Constants.REFRESH_TOKEN);
OrcidOauth2TokenDetail token = null;
try {
token = orcidOauth2TokenDetailDao.findByRefreshTokenValue(refreshToken);
} catch (NoResultException e) {
throw new InvalidTokenException("Unable to find refresh token", e);
}
// Verify the token belongs to this client
if (!clientId.equals(token.getClientDetailsId())) {
throw new IllegalArgumentException("This token does not belong to the given client");
}
// Verify client is enabled
ClientDetailsEntity clientDetails = clientDetailsEntityCacheManager.retrieve(clientId);
orcidOAuth2RequestValidator.validateClientIsEnabled(clientDetails);
// Verify the token is not expired
if (token.getTokenExpiration() != null) {
if (token.getTokenExpiration().before(new Date())) {
throw new InvalidTokenException("Access token expired: " + authorization);
}
}
// Verify access token and refresh token are linked
if (!refreshToken.equals(token.getRefreshTokenValue())) {
throw new InvalidTokenException("Token and refresh token does not match");
}
// Verify the token is not disabled
if (token.getTokenDisabled() != null && token.getTokenDisabled()) {
throw new InvalidTokenException("Parent token is disabled");
}
// Verify scopes are not wider than the token scopes
if (PojoUtil.isEmpty(scopes)) {
scopes = token.getScope();
} else {
Set<ScopePathType> requiredScopes = ScopePathType.getScopesFromSpaceSeparatedString(scopes);
Set<ScopePathType> simpleTokenScopes = ScopePathType.getScopesFromSpaceSeparatedString(token.getScope());
// This collection contains all tokens that should be allowed given
// the scopes that the parent token contains
Set<ScopePathType> combinedTokenScopes = new HashSet<ScopePathType>();
for (ScopePathType scope : simpleTokenScopes) {
combinedTokenScopes.addAll(scope.combined());
}
// combinedTokenScopes
for (ScopePathType scope : requiredScopes) {
if (!combinedTokenScopes.contains(scope)) {
throw new InvalidScopeException("The given scope '" + scope.value() + "' is not allowed for the parent token");
}
}
}
// Validate the expiration for the new token is no later than the parent
// token expiration.
long parentTokenExpiration = token.getTokenExpiration() == null ? System.currentTimeMillis() : token.getTokenExpiration().getTime();
if (expireIn > parentTokenExpiration) {
throw new IllegalArgumentException("Token expiration can't be after " + token.getTokenExpiration());
}
}
use of javax.persistence.NoResultException in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method checkProfile.
/**
* Checks a record status and throw an exception indicating if the profile
* have any of the following conditions: - The record is not claimed and is
* not old enough nor being accessed by its creator - It is locked - It is
* deprecated - It is deactivated
*
* @throws OrcidDeprecatedException
* in case the account is deprecated
* @throws OrcidNotClaimedException
* in case the account is not claimed
* @throws LockedException
* in the case the account is locked
*/
@Override
public void checkProfile(String orcid) throws NoResultException, OrcidDeprecatedException, OrcidNotClaimedException, LockedException, DeactivatedException {
ProfileEntity profile = null;
try {
profile = profileEntityCacheManager.retrieve(orcid);
} catch (IllegalArgumentException e) {
throw new NoResultException();
}
// Check if the user record is deprecated
if (profile.getPrimaryRecord() != null) {
StringBuffer primary = new StringBuffer(baseUrl).append("/").append(profile.getPrimaryRecord().getId());
Map<String, String> params = new HashMap<String, String>();
params.put(OrcidDeprecatedException.ORCID, primary.toString());
if (profile.getDeprecatedDate() != null) {
XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(profile.getDeprecatedDate());
params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
}
throw new OrcidDeprecatedException(params);
}
// Check if the user record is not claimed and not old enough
if ((profile.getClaimed() == null || Boolean.FALSE.equals(profile.getClaimed())) && !isOldEnough(profile)) {
// Let the creator access the profile even if it is not claimed and
// not old enough
SourceEntity currentSourceEntity = sourceManager.retrieveSourceEntity();
String profileSource = profile.getSource() == null ? null : profile.getSource().getSourceId();
String currentSource = currentSourceEntity == null ? null : currentSourceEntity.getSourceId();
// the profile source, throw an exception
if (profileSource == null || !Objects.equals(profileSource, currentSource)) {
throw new OrcidNotClaimedException();
}
}
// Check if the user record is locked
if (!profile.isAccountNonLocked()) {
LockedException lockedException = new LockedException();
lockedException.setOrcid(profile.getId());
throw lockedException;
}
// Check if the user record is deactivated
if (profile.getDeactivationDate() != null) {
DeactivatedException exception = new DeactivatedException();
exception.setOrcid(orcid);
throw exception;
}
}
use of javax.persistence.NoResultException in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method checkProfile.
/**
* Checks a record status and throw an exception indicating if the profile
* have any of the following conditions: - The record is not claimed and is
* not old enough nor being accessed by its creator - It is locked - It is
* deprecated - It is deactivated
*
* @throws OrcidDeprecatedException
* in case the account is deprecated
* @throws OrcidNotClaimedException
* in case the account is not claimed
* @throws LockedException
* in the case the account is locked
*/
@Override
public void checkProfile(String orcid) throws NoResultException, OrcidDeprecatedException, OrcidNotClaimedException, LockedException, DeactivatedException {
ProfileEntity profile = null;
try {
profile = profileEntityCacheManager.retrieve(orcid);
} catch (IllegalArgumentException e) {
throw new NoResultException();
}
// Check if the user record is deprecated
if (profile.getPrimaryRecord() != null) {
StringBuffer primary = new StringBuffer(baseUrl).append("/").append(profile.getPrimaryRecord().getId());
Map<String, String> params = new HashMap<String, String>();
params.put(OrcidDeprecatedException.ORCID, primary.toString());
if (profile.getDeprecatedDate() != null) {
XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(profile.getDeprecatedDate());
params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
}
StringBuffer deprecated = new StringBuffer(baseUrl).append("/").append(profile.getId());
params.put(OrcidDeprecatedException.DEPRECATED_ORCID, deprecated.toString());
throw new OrcidDeprecatedException(params);
}
// Check if the user record is not claimed and not old enough
if ((profile.getClaimed() == null || Boolean.FALSE.equals(profile.getClaimed())) && !isOldEnough(profile)) {
// Let the creator access the profile even if it is not claimed and
// not old enough
SourceEntity currentSourceEntity = sourceManager.retrieveSourceEntity();
String profileSource = profile.getSource() == null ? null : profile.getSource().getSourceId();
String currentSource = currentSourceEntity == null ? null : currentSourceEntity.getSourceId();
// the profile source, throw an exception
if (profileSource == null || !Objects.equals(profileSource, currentSource)) {
throw new OrcidNotClaimedException();
}
}
// Check if the user record is locked
if (!profile.isAccountNonLocked()) {
LockedException lockedException = new LockedException();
StringBuffer orcidId = new StringBuffer(baseUrl).append("/").append(profile.getId());
lockedException.setOrcid(orcidId.toString());
throw lockedException;
}
// Check if the user record is deactivated
if (profile.getDeactivationDate() != null) {
DeactivatedException exception = new DeactivatedException();
exception.setOrcid(orcid);
throw exception;
}
}
Aggregations