Copied to clipboard

Flag this post as spam?

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


  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Jul 31, 2020 @ 14:10
    Matt Barlow | jacker.io
    0

    Get userId of the user that saved content in the ContentService.Saving event.

    I have a tool that imports content and saves it with userId -1 (importer tool user). When importing with this tool, there should be no content validation.

    I also have a subscription to the ContentSaving event:

       ContentService.Saving += OnSaving;
    

    and:

      private void OnSaving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var node in e.SavedEntities)
            {
                    if (node.WriterId != -1)
                    {
                        Validate(node, e);
                    }
            }
        }
    

    Which all works fine when importing, the WriterId is -1 and the validation doesn't occur, so the content is saved.

    Now when a I log in and and save that data (different userId), this event is hit again. I want the validation to occur. This time the expected behaviour is because the user isn't -1 then the Validate() function should be called.

    However I'm finding that the id is still -1 for the WriterId, so the Validate() function doesn't fire. This saves the user, then updates the WriterId to my userId, so when I save again this time the validation fires.

    Assuming this is because it's checking the current state and getting the writerId rather than getting the userId from user saving?

    So TLDR:

    How do I get the correct userId of the user performing the save action in the Content.Saving event?

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jul 31, 2020 @ 15:38
    Nik
    1

    Have you tried getting the current back office user? Off the top of my head I can't be certain how you do that though.

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Aug 04, 2020 @ 07:14
    Matt Barlow | jacker.io
    100

    Thanks Nik!

    In the end I went with the approach of locking it down by role, so I check the back office users role and use that to determine if validation should occur.

        private void OnSaving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var node in e.SavedEntities)
            {
                if (node.ContentType.Alias == MyContentType.ModelTypeAlias)
                {
                    using (var cref = _umbracoContext.EnsureUmbracoContext())
                    {
                        var isAdmin = cref.UmbracoContext.Security.CurrentUser.IsAdmin();
    
                        if (!isAdmin)
                        {
                            Validate(node, e);
                        }
                    }
    
                }
            }
        }
    

    Thanks, your suggestion pushed me in the right direction!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies