Search in sources :

Example 1 with OrcidForbiddenException

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

the class T2OrcidApiServiceDelegatorImpl method unregisterWebhook.

/**
     * Unregister a webhook from a profile. As with all calls, if the message
     * contains any other elements, a 400 Bad Request will be returned.
     * 
     * @param orcid
     *            the identifier of the profile to unregister the webhook
     * @param uriInfo
     *            an uri object containing the webhook that will be unregistred
     * @return If successful, returns a 204 No content.
     * */
@Override
@AccessControl(requiredScope = ScopePathType.WEBHOOK)
public Response unregisterWebhook(UriInfo uriInfo, String orcid, String webhookUri) {
    ProfileEntity profile = profileEntityCacheManager.retrieve(orcid);
    if (profile != null) {
        WebhookEntityPk webhookPk = new WebhookEntityPk(profile, webhookUri);
        WebhookEntity webhook = webhookManager.find(webhookPk);
        if (webhook == null) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("orcid", orcid);
            params.put("uri", webhookUri);
            throw new OrcidWebhookNotFoundException(params);
        } else {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String clientId = null;
            if (OAuth2Authentication.class.isAssignableFrom(authentication.getClass())) {
                OAuth2Request authorizationRequest = ((OAuth2Authentication) authentication).getOAuth2Request();
                clientId = authorizationRequest.getClientId();
            }
            // Check if user can unregister this webhook
            if (webhook.getClientDetails().getId().equals(clientId)) {
                webhookManager.delete(webhookPk);
                return Response.noContent().build();
            } else {
                // that webhook
                throw new OrcidForbiddenException(localeManager.resolveMessage("apiError.forbidden_unregister_webhook.exception"));
            }
        }
    } else {
        Map<String, String> params = new HashMap<String, String>();
        params.put("orcid", orcid);
        throw new OrcidNotFoundException(params);
    }
}
Also used : OrcidWebhookNotFoundException(org.orcid.core.exception.OrcidWebhookNotFoundException) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OrcidForbiddenException(org.orcid.core.exception.OrcidForbiddenException) WebhookEntityPk(org.orcid.persistence.jpa.entities.keys.WebhookEntityPk) HashMap(java.util.HashMap) WebhookEntity(org.orcid.persistence.jpa.entities.WebhookEntity) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrcidNotFoundException(org.orcid.core.exception.OrcidNotFoundException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) AccessControl(org.orcid.core.security.visibility.aop.AccessControl)

Aggregations

HashMap (java.util.HashMap)1 OrcidForbiddenException (org.orcid.core.exception.OrcidForbiddenException)1 OrcidNotFoundException (org.orcid.core.exception.OrcidNotFoundException)1 OrcidWebhookNotFoundException (org.orcid.core.exception.OrcidWebhookNotFoundException)1 AccessControl (org.orcid.core.security.visibility.aop.AccessControl)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 WebhookEntity (org.orcid.persistence.jpa.entities.WebhookEntity)1 WebhookEntityPk (org.orcid.persistence.jpa.entities.keys.WebhookEntityPk)1 Authentication (org.springframework.security.core.Authentication)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)1