Search in sources :

Example 1 with OrcidType

use of org.orcid.jaxb.model.common_v2.OrcidType in project ORCID-Source by ORCID.

the class SourceManagerImpl method isDelegatedByAnAdmin.

@Override
public boolean isDelegatedByAnAdmin() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (authorities != null) {
            for (GrantedAuthority authority : authorities) {
                if (authority instanceof SwitchUserGrantedAuthority) {
                    SwitchUserGrantedAuthority suga = (SwitchUserGrantedAuthority) authority;
                    Authentication sourceAuthentication = suga.getSource();
                    if (sourceAuthentication instanceof UsernamePasswordAuthenticationToken && sourceAuthentication.getPrincipal() instanceof OrcidProfileUserDetails) {
                        org.orcid.jaxb.model.message.OrcidType legacyOrcidType = ((OrcidProfileUserDetails) sourceAuthentication.getPrincipal()).getOrcidType();
                        OrcidType sourceUserType = legacyOrcidType == null ? null : OrcidType.fromValue(legacyOrcidType.value());
                        return OrcidType.ADMIN.equals(sourceUserType);
                    }
                }
            }
        }
    }
    return false;
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OrcidType(org.orcid.jaxb.model.common_v2.OrcidType) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority) OrcidProfileUserDetails(org.orcid.core.oauth.OrcidProfileUserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SwitchUserGrantedAuthority(org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority)

Example 2 with OrcidType

use of org.orcid.jaxb.model.common_v2.OrcidType in project ORCID-Source by ORCID.

the class NotificationManagerImpl method sendAmendEmail.

@Override
public void sendAmendEmail(String userOrcid, AmendedSection amendedSection, Collection<Item> items) {
    String amenderOrcid = sourceManager.retrieveSourceOrcid();
    ProfileEntity record = profileEntityCacheManager.retrieve(userOrcid);
    Locale locale = getUserLocaleFromProfileEntity(record);
    if (amenderOrcid == null) {
        LOGGER.info("Not sending amend email to {} because amender is null", userOrcid);
        return;
    }
    if (amenderOrcid.equals(userOrcid)) {
        LOGGER.debug("Not sending amend email, because self edited: {}", userOrcid);
        return;
    }
    Boolean sendChangeNotifications = record.getSendChangeNotifications();
    if (sendChangeNotifications == null || !sendChangeNotifications) {
        LOGGER.debug("Not sending amend email, because option to send change notifications is disabled: {}", userOrcid);
        return;
    }
    org.orcid.jaxb.model.common_v2.OrcidType amenderType = profileDao.retrieveOrcidType(amenderOrcid);
    if (amenderType != null && OrcidType.ADMIN.equals(OrcidType.fromValue(amenderType.value()))) {
        LOGGER.debug("Not sending amend email, because modified by admin ({}): {}", amenderOrcid, userOrcid);
        return;
    }
    String subject = getSubject("email.subject.amend", locale);
    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("emailName", deriveEmailFriendlyName(record));
    templateParams.put("orcid", userOrcid);
    templateParams.put("amenderName", extractAmenderName(userOrcid, amenderOrcid));
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("subject", subject);
    addMessageParams(templateParams, locale);
    // Generate body from template
    String body = templateManager.processTemplate("amend_email.ftl", templateParams);
    // Generate html from template
    String html = templateManager.processTemplate("amend_email_html.ftl", templateParams);
    boolean notificationsEnabled = record.getEnableNotifications();
    if (notificationsEnabled) {
        NotificationAmended notification = new NotificationAmended();
        notification.setNotificationType(NotificationType.AMENDED);
        notification.setAmendedSection(amendedSection);
        if (items != null) {
            notification.setItems(new Items(new ArrayList<>(items)));
        }
        createNotification(userOrcid, notification);
    } else {
        String primaryEmail = emailManager.findPrimaryEmail(userOrcid).getEmail();
        mailGunManager.sendEmail(AMEND_NOTIFY_ORCID_ORG, primaryEmail, subject, body, html);
    }
}
Also used : Locale(java.util.Locale) OrcidType(org.orcid.jaxb.model.common_v2.OrcidType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Items(org.orcid.jaxb.model.notification.permission_v2.Items) NotificationAmended(org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)

Aggregations

OrcidType (org.orcid.jaxb.model.common_v2.OrcidType)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)1 NotificationAmended (org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)1 Items (org.orcid.jaxb.model.notification.permission_v2.Items)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 SwitchUserGrantedAuthority (org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority)1