Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Hastings 4 posts 85 karma points
    Nov 02, 2021 @ 17:14
    Nathan Hastings
    0

    ContentService event handler not firing for non Admins

    We have added an event handler that calls a webhook when a piece of content is published, something that I have implemented on a couple of Umbraco installs in the past. The client has complained a number of times that this has not worked but worked fine for me, I then was able to replicate the issue by using a different (non administrator) user account.

    I have read through documentation on this and I can't see anything that says you need to enable for certain user roles e.g. https://our.umbraco.com/documentation/reference/events/contentservice-events

    We are using version 8.16.0.

    Code is:

    using System.Linq;
    

    using Umbraco.Core; using Umbraco.Web; using Umbraco.Core.Composing; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; using Umbraco.Core.Services.Implement;

    namespace cms { [RuntimeLevel(MinLevel = RuntimeLevel.Run)] public class EventHandlerComposer : IUserComposer { public void Compose(Composition composition) { composition.Components().Append

    public class EventHandlerComponent : IComponent
    {
        public void Initialize()
        {
            ContentService.Published += ContentService_Published;
        }
    
        public void Terminate()
        {
            ContentService.Published -= ContentService_Published;
        }
    
        private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
        {
            FireCacheClearWebhook();
        }
    
        private void FireCacheClearWebhook() {
            UmbracoHelper helper = Umbraco.Web.Composing.Current.UmbracoHelper;
            string clientHost = helper.GetDictionaryValue("client_host");
    
            if(clientHost != "") {
                using (var client = new System.Net.WebClient())
                {
                    client.Headers.Add(System.Net.HttpRequestHeader.ContentType, "application/json");
                    client.Headers.Remove(System.Net.HttpRequestHeader.Expect);
                    client.UploadString(new System.Uri(clientHost + "/cache/clear"), "POST", "{ \"key\": \"secret key\"}");
                }
            }
        }
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft