Search in sources :

Example 6 with ClientDetailsEntity

use of org.orcid.persistence.jpa.entities.ClientDetailsEntity in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method processClient.

/**
     * Get a client and evaluates if it is new or it is an update and act
     * accordingly.
     * 
     * @param groupOrcid
     *            The client owner
     * @param clientDetailsEntities
     *            The cached list of clients
     * @param client
     *            The client that is being evaluated
     * @param clientType
     *            The type of client
     */
private void processClient(String groupOrcid, SortedSet<ClientDetailsEntity> clientDetailsEntities, OrcidClient client, ClientType clientType) {
    if (client.getClientId() == null) {
        // If the client ID in the incoming client is null, then create
        // a new client.
        // Use the client details service to create the client details
        ClientDetailsEntity clientDetailsEntity = createClientDetails(groupOrcid, client, clientType);
        // And link the client to the copy of the profile cached in
        // memory by Hibernate
        clientDetailsEntities.add(clientDetailsEntity);
    } else {
        // If the client ID in the incoming client is not null, then
        // look up the existing client.
        String clientId = client.getClientId();
        ClientDetailsEntity clientDetailsEntity = clientDetailsManager.findByClientId(clientId);
        if (clientDetailsEntity == null) {
            // error.
            throw new OrcidClientGroupManagementException("Unable to find client: " + clientId);
        } else {
            if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId()) && !clientDetailsEntity.getGroupProfileId().equals(groupOrcid)) {
                // error.
                throw new OrcidClientGroupManagementException(String.format("Client %s does not belong to group %s (actually belongs to group %s)", clientId, groupOrcid, clientDetailsEntity.getGroupProfileId()));
            }
            // If the existing client is found, then update the client
            // details from the incoming client
            updateClientDetailsEntityFromClient(client, clientDetailsEntity, true);
            clientDetailsManager.merge(clientDetailsEntity);
        }
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException)

Example 7 with ClientDetailsEntity

use of org.orcid.persistence.jpa.entities.ClientDetailsEntity in project ORCID-Source by ORCID.

the class NotificationManagerImpl method sendApiRecordCreationEmail.

@Override
@Transactional
public void sendApiRecordCreationEmail(String toEmail, OrcidProfile createdProfile) {
    Source source = null;
    CustomEmailEntity customEmail = null;
    if (createdProfile.getOrcidHistory() != null && createdProfile.getOrcidHistory().getSource() != null) {
        if (!PojoUtil.isEmpty(createdProfile.getOrcidHistory().getSource().retrieveSourcePath())) {
            source = createdProfile.getOrcidHistory().getSource();
            customEmail = getCustomizedEmail(createdProfile.getOrcidHistory().getSource().retrieveSourcePath(), EmailType.CLAIM);
        }
    }
    String email = createdProfile.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue().trim();
    String emailName = deriveEmailFriendlyName(createdProfile);
    String orcid = createdProfile.getOrcidIdentifier().getPath();
    String verificationUrl = createClaimVerificationUrl(email, orcidUrlManager.getBaseUrl());
    String creatorName = "";
    if (source != null) {
        if (source.getSourceName() != null && source.getSourceName().getContent() != null) {
            creatorName = source.getSourceName().getContent();
        } else if (!PojoUtil.isEmpty(source.retrieveSourcePath())) {
            creatorName = source.retrieveSourcePath();
        }
    }
    String subject = null;
    String body = null;
    String htmlBody = null;
    String sender = null;
    if (customEmail != null) {
        // Get the customized sender if available
        sender = PojoUtil.isEmpty(customEmail.getSender()) ? CLAIM_NOTIFY_ORCID_ORG : customEmail.getSender();
        // Get the customized subject is available
        subject = PojoUtil.isEmpty(customEmail.getSubject()) ? getSubject("email.subject.api_record_creation", createdProfile) : customEmail.getSubject();
        // Replace the wildcards
        subject = subject.replace(WILDCARD_USER_NAME, emailName);
        subject = subject.replace(WILDCARD_MEMBER_NAME, creatorName);
        if (customEmail.isHtml()) {
            htmlBody = customEmail.getContent();
            htmlBody = htmlBody.replace(WILDCARD_USER_NAME, emailName);
            htmlBody = htmlBody.replace(WILDCARD_MEMBER_NAME, creatorName);
            htmlBody = htmlBody.replace(EmailConstants.WILDCARD_VERIFICATION_URL, verificationUrl);
            if (htmlBody.contains(WILDCARD_WEBSITE) || htmlBody.contains(WILDCARD_DESCRIPTION)) {
                ClientDetailsEntity clientDetails = customEmail.getClientDetailsEntity();
                htmlBody = htmlBody.replace(WILDCARD_WEBSITE, clientDetails.getClientWebsite());
                htmlBody = htmlBody.replace(WILDCARD_DESCRIPTION, clientDetails.getClientDescription());
            }
        } else {
            body = customEmail.getContent();
            body = body.replace(WILDCARD_USER_NAME, emailName);
            body = body.replace(WILDCARD_MEMBER_NAME, creatorName);
            body = body.replace(EmailConstants.WILDCARD_VERIFICATION_URL, verificationUrl);
            if (body.contains(WILDCARD_WEBSITE) || body.contains(WILDCARD_DESCRIPTION)) {
                ClientDetailsEntity clientDetails = customEmail.getClientDetailsEntity();
                body = body.replace(WILDCARD_WEBSITE, clientDetails.getClientWebsite());
                body = body.replace(WILDCARD_DESCRIPTION, clientDetails.getClientDescription());
            }
        }
    } else {
        subject = getSubject("email.subject.api_record_creation", createdProfile);
        // Create map of template params
        Map<String, Object> templateParams = new HashMap<String, Object>();
        templateParams.put("emailName", emailName);
        templateParams.put("orcid", orcid);
        templateParams.put("subject", subject);
        templateParams.put("creatorName", creatorName);
        templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
        templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
        templateParams.put("verificationUrl", verificationUrl);
        addMessageParams(templateParams, createdProfile);
        // Generate body from template
        body = templateManager.processTemplate("api_record_creation_email.ftl", templateParams);
        htmlBody = templateManager.processTemplate("api_record_creation_email_html.ftl", templateParams);
    }
    // Send message
    if (apiRecordCreationEmailEnabled) {
        boolean isCustomEmail = customEmail != null ? true : false;
        // mailgun
        if (isCustomEmail) {
            mailGunManager.sendEmail(sender, email, subject, body, htmlBody, isCustomEmail);
        } else {
            mailGunManager.sendEmail(CLAIM_NOTIFY_ORCID_ORG, email, subject, body, htmlBody);
        }
    } else {
        LOGGER.debug("Not sending API record creation email, because option is disabled. Message would have been: {}", body);
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) HashMap(java.util.HashMap) CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) Source(org.orcid.jaxb.model.message.Source) MessageSource(org.springframework.context.MessageSource) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with ClientDetailsEntity

use of org.orcid.persistence.jpa.entities.ClientDetailsEntity in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method createOrUpdateOrcidClientGroup.

@Override
@Transactional
public OrcidClientGroup createOrUpdateOrcidClientGroup(OrcidClientGroup orcidClientGroup) {
    // For each client in the client group, validate the client type
    for (OrcidClient client : orcidClientGroup.getOrcidClient()) {
        checkAndSetClientType(client, orcidClientGroup.getType());
    }
    String groupOrcid = orcidClientGroup.getGroupOrcid();
    if (groupOrcid == null) {
        orcidClientGroup = createGroup(orcidClientGroup);
        groupOrcid = orcidClientGroup.getGroupOrcid();
    } else {
        updateGroup(orcidClientGroup);
    }
    // Use the profile DAO to link the clients to the group, so get the
    // group profile entity.
    ProfileEntity groupProfileEntity = profileDao.find(groupOrcid);
    SortedSet<ClientDetailsEntity> clientProfileEntities = groupProfileEntity.getClients();
    if (clientProfileEntities == null) {
        clientProfileEntities = new TreeSet<>(new OrcidEntityIdComparator<String>());
        groupProfileEntity.setClients(clientProfileEntities);
    }
    // For each client in the client group
    for (OrcidClient client : orcidClientGroup.getOrcidClient()) {
        processClient(groupOrcid, clientProfileEntities, client, getClientType(groupProfileEntity.getGroupType()));
    }
    // Regenerate client group and return.
    return retrieveOrcidClientGroup(groupOrcid);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidEntityIdComparator(org.orcid.persistence.jpa.entities.OrcidEntityIdComparator) OrcidClient(org.orcid.jaxb.model.clientgroup.OrcidClient) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with ClientDetailsEntity

use of org.orcid.persistence.jpa.entities.ClientDetailsEntity in project ORCID-Source by ORCID.

the class OrcidClientGroupManagerImpl method updateClient.

/**
     * Updates a client profile, updates can be adding or removing redirect uris
     * or updating the client fields
     * 
     * @param groupOrcid
     *            The group owner for this client
     * @param client
     *            The updated client
     * @return the updated OrcidClient
     */
public OrcidClient updateClient(String groupOrcid, OrcidClient client) {
    ClientDetailsEntity clientDetailsEntity = null;
    if (client.getClientId() != null) {
        // Look up the existing client.
        String clientId = client.getClientId();
        clientDetailsEntity = clientDetailsDao.find(clientId);
        if (clientDetailsEntity == null) {
            // error.
            throw new OrcidClientGroupManagementException("Unable to find client: " + clientId);
        } else {
            if (!PojoUtil.isEmpty(clientDetailsEntity.getGroupProfileId()) && !clientDetailsEntity.getGroupProfileId().equals(groupOrcid)) {
                // error.
                throw new OrcidClientGroupManagementException(String.format("Client %s does not belong to group %s (actually belongs to group %s)", clientId, groupOrcid, clientDetailsEntity.getGroupProfileId()));
            }
            // If the existing client is found, then update the client
            // details from the incoming client, and save using the client
            // details manager
            updateClientDetailsEntityFromClient(client, clientDetailsEntity, true);
            clientDetailsManager.merge(clientDetailsEntity);
        }
    }
    return adapter.toOrcidClient(clientDetailsEntity);
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) OrcidClientGroupManagementException(org.orcid.core.exception.OrcidClientGroupManagementException)

Example 10 with ClientDetailsEntity

use of org.orcid.persistence.jpa.entities.ClientDetailsEntity in project ORCID-Source by ORCID.

the class InstitutionalSignInManagerImpl method sendNotification.

@Override
public void sendNotification(String userOrcid, String providerId) throws UnsupportedEncodingException {
    try {
        ClientDetailsEntity clientDetails = clientDetailsEntityCacheManager.retrieveByIdP(providerId);
        boolean clientKnowsUser = orcidOauth2TokenDetailService.doesClientKnowUser(clientDetails.getClientId(), userOrcid);
        // notification
        if (!clientKnowsUser) {
            notificationManager.sendAcknowledgeMessage(userOrcid, clientDetails.getClientId());
        }
    } catch (IllegalArgumentException e) {
    // The provided IdP hasn't not been linked to any client yet.
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity)

Aggregations

ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)149 Test (org.junit.Test)75 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)57 BaseTest (org.orcid.core.BaseTest)51 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)33 Date (java.util.Date)23 Transactional (org.springframework.transaction.annotation.Transactional)16 HashSet (java.util.HashSet)15 DBUnitTest (org.orcid.test.DBUnitTest)15 HashMap (java.util.HashMap)14 Authentication (org.springframework.security.core.Authentication)13 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)13 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)11 Work (org.orcid.jaxb.model.record_v2.Work)9 Before (org.junit.Before)8 ArrayList (java.util.ArrayList)7 OrcidClient (org.orcid.jaxb.model.clientgroup.OrcidClient)7 ClientSecretEntity (org.orcid.persistence.jpa.entities.ClientSecretEntity)7 OrcidProfile (org.orcid.jaxb.model.message.OrcidProfile)6 Funding (org.orcid.jaxb.model.record_v2.Funding)6