Search in sources :

Example 6 with DeactivatedException

use of org.orcid.core.exception.DeactivatedException 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;
    }
}
Also used : LockedException(org.orcid.core.security.aop.LockedException) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) NoResultException(javax.persistence.NoResultException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) DeactivatedException(org.orcid.core.exception.DeactivatedException) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) OrcidNotClaimedException(org.orcid.core.exception.OrcidNotClaimedException)

Example 7 with DeactivatedException

use of org.orcid.core.exception.DeactivatedException in project ORCID-Source by ORCID.

the class OrcidSearchManagerImpl method buildSearchResultsFromPublicProfile.

private List<OrcidSearchResult> buildSearchResultsFromPublicProfile(List<OrcidSolrResult> solrResults) {
    List<OrcidSearchResult> orcidSearchResults = new ArrayList<OrcidSearchResult>();
    for (OrcidSolrResult solrResult : solrResults) {
        OrcidMessage orcidMessage = null;
        String orcid = solrResult.getOrcid();
        try {
            orcidSecurityManager.checkProfile(orcid);
        } catch (DeactivatedException | LockedException | OrcidDeprecatedException x) {
            OrcidSearchResult orcidSearchResult = new OrcidSearchResult();
            RelevancyScore relevancyScore = new RelevancyScore();
            relevancyScore.setValue(solrResult.getRelevancyScore());
            orcidSearchResult.setRelevancyScore(relevancyScore);
            OrcidProfile orcidProfile = new OrcidProfile();
            orcidProfile.setOrcidIdentifier(new OrcidIdentifier(jpaJaxbAdapter.getOrcidIdBase(orcid)));
            OrcidHistory history = new OrcidHistory();
            Date recordLastModified = profileDaoReadOnly.retrieveLastModifiedDate(orcid);
            history.setLastModifiedDate(new LastModifiedDate(DateUtils.convertToXMLGregorianCalendar(recordLastModified)));
            orcidProfile.setOrcidHistory(history);
            orcidSearchResult.setOrcidProfile(orcidProfile);
            orcidSearchResults.add(orcidSearchResult);
            continue;
        }
        if (cachingSource.equals(SOLR)) {
            try (Reader reader = solrDao.findByOrcidAsReader(orcid)) {
                if (reader != null) {
                    BufferedReader br = new BufferedReader(reader);
                    orcidMessage = OrcidMessage.unmarshall(br);
                }
            } catch (IOException e) {
                throw new OrcidSearchException("Error closing record stream from solr search results for orcid: " + orcid, e);
            }
        }
        OrcidProfile orcidProfile = null;
        if (orcidMessage == null) {
            // Fall back to DB
            orcidProfile = orcidProfileCacheManager.retrievePublicBio(orcid);
        } else {
            orcidProfile = orcidMessage.getOrcidProfile();
        }
        if (orcidProfile != null) {
            OrcidSearchResult orcidSearchResult = new OrcidSearchResult();
            RelevancyScore relevancyScore = new RelevancyScore();
            relevancyScore.setValue(solrResult.getRelevancyScore());
            orcidSearchResult.setRelevancyScore(relevancyScore);
            OrcidWorks orcidWorksTitlesOnly = new OrcidWorks();
            OrcidWorks fullOrcidWorks = orcidProfile.retrieveOrcidWorks();
            if (fullOrcidWorks != null && !fullOrcidWorks.getOrcidWork().isEmpty()) {
                for (OrcidWork fullOrcidWork : fullOrcidWorks.getOrcidWork()) {
                    OrcidWork orcidWorkSubset = new OrcidWork();
                    orcidWorkSubset.setVisibility(fullOrcidWork.getVisibility());
                    orcidWorkSubset.setWorkTitle(fullOrcidWork.getWorkTitle());
                    orcidWorkSubset.setWorkExternalIdentifiers(fullOrcidWork.getWorkExternalIdentifiers());
                    orcidWorksTitlesOnly.getOrcidWork().add(orcidWorkSubset);
                }
            }
            FundingList reducedFundings = new FundingList();
            FundingList fullOrcidFundings = orcidProfile.retrieveFundings();
            if (fullOrcidFundings != null && !fullOrcidFundings.getFundings().isEmpty()) {
                for (Funding fullOrcidFunding : fullOrcidFundings.getFundings()) {
                    Funding reducedFunding = new Funding();
                    reducedFunding.setVisibility(fullOrcidFunding.getVisibility());
                    reducedFunding.setDescription(fullOrcidFunding.getDescription());
                    reducedFunding.setTitle(fullOrcidFunding.getTitle());
                    reducedFundings.getFundings().add(reducedFunding);
                }
            }
            orcidProfile.setOrcidWorks(orcidWorksTitlesOnly);
            orcidProfile.setFundings(reducedFundings);
            orcidSearchResult.setOrcidProfile(orcidProfile);
            orcidSearchResults.add(orcidSearchResult);
        }
    }
    return orcidSearchResults;
}
Also used : LastModifiedDate(org.orcid.jaxb.model.message.LastModifiedDate) LockedException(org.orcid.core.security.aop.LockedException) OrcidSearchException(org.orcid.core.exception.OrcidSearchException) Funding(org.orcid.jaxb.model.message.Funding) ArrayList(java.util.ArrayList) OrcidWork(org.orcid.jaxb.model.message.OrcidWork) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Date(java.util.Date) LastModifiedDate(org.orcid.jaxb.model.message.LastModifiedDate) DeactivatedException(org.orcid.core.exception.DeactivatedException) OrcidWorks(org.orcid.jaxb.model.message.OrcidWorks) OrcidSearchResult(org.orcid.jaxb.model.message.OrcidSearchResult) RelevancyScore(org.orcid.jaxb.model.message.RelevancyScore) OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) OrcidSolrResult(org.orcid.utils.solr.entities.OrcidSolrResult) FundingList(org.orcid.jaxb.model.message.FundingList) OrcidIdentifier(org.orcid.jaxb.model.message.OrcidIdentifier) OrcidHistory(org.orcid.jaxb.model.message.OrcidHistory) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) BufferedReader(java.io.BufferedReader)

Example 8 with DeactivatedException

use of org.orcid.core.exception.DeactivatedException in project ORCID-Source by ORCID.

the class OrcidSearchManagerImplTest method recordDeactivatedTest.

@Test
public void recordDeactivatedTest() {
    OrcidProfile orcidProfile = getOrcidProfileAllIndexFieldsPopulated();
    orcidProfile.getOrcidIdentifier().setPath("0000");
    when(mockSolrDao.findByDocumentCriteria("rndQuery", null, null)).thenReturn(invalidRecordSearchResult());
    when(mockOrcidProfileCacheManager.retrievePublicBio("0000")).thenReturn(orcidProfile);
    doThrow(new DeactivatedException()).when(mockOrcidSecurityManager).checkProfile("0000");
    OrcidMessage retrievedOrcidMessage = orcidSearchManager.findOrcidsByQuery("rndQuery");
    assertNotNull(retrievedOrcidMessage);
    assertTrue(retrievedOrcidMessage.getOrcidSearchResults() != null && retrievedOrcidMessage.getOrcidSearchResults().getOrcidSearchResult().size() == 1);
    OrcidSearchResult searchResult = retrievedOrcidMessage.getOrcidSearchResults().getOrcidSearchResult().get(0);
    OrcidProfile profileReturnedFromSearch = searchResult.getOrcidProfile();
    assertEquals("0000", profileReturnedFromSearch.getOrcidIdentifier().getPath());
    assertNotNull(profileReturnedFromSearch.getOrcidHistory().getLastModifiedDate().getValue());
    assertNull(profileReturnedFromSearch.getOrcidActivities());
    assertNull(profileReturnedFromSearch.getOrcidBio());
}
Also used : OrcidProfile(org.orcid.jaxb.model.message.OrcidProfile) OrcidSearchResult(org.orcid.jaxb.model.message.OrcidSearchResult) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) DeactivatedException(org.orcid.core.exception.DeactivatedException) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Example 9 with DeactivatedException

use of org.orcid.core.exception.DeactivatedException in project ORCID-Source by ORCID.

the class OAuthErrorUtilsTest method testGetOAuthErrorForDeactivatedException.

@Test
public void testGetOAuthErrorForDeactivatedException() {
    OAuthError error = OAuthErrorUtils.getOAuthError(new DeactivatedException("message here"));
    assertEquals(OAuthError.UNAUTHORIZED_CLIENT, error.getError());
    assertEquals(Status.BAD_REQUEST, error.getResponseStatus());
    assertEquals("message here", error.getErrorDescription());
}
Also used : DeactivatedException(org.orcid.core.exception.DeactivatedException) Test(org.junit.Test)

Aggregations

DeactivatedException (org.orcid.core.exception.DeactivatedException)9 OrcidDeprecatedException (org.orcid.core.exception.OrcidDeprecatedException)6 LockedException (org.orcid.core.security.aop.LockedException)6 HashMap (java.util.HashMap)4 OrcidNotClaimedException (org.orcid.core.exception.OrcidNotClaimedException)4 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)4 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)4 OrcidSearchResult (org.orcid.jaxb.model.message.OrcidSearchResult)4 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)3 Test (org.junit.Test)3 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)3 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 Date (java.util.Date)2 NoResultException (javax.persistence.NoResultException)2 BaseTest (org.orcid.core.BaseTest)2 OrcidSearchException (org.orcid.core.exception.OrcidSearchException)2