Copied to clipboard

Flag this post as spam?

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


  • Peter Bridger 2 posts 72 karma points
    Sep 12, 2019 @ 11:21
    Peter Bridger
    0

    ContentService - Trashing event, determining the user that triggered it

    I have a requirement to prevent all users from deleting documents of a certain document type, expect for the admin user.

    My first thought was to attach an event to the ContentService.Trashing event handler to achieve this by setting args.Cancel

    A Google turned up https://our.umbraco.com/forum/using-umbraco-and-getting-started/75966-contentservice-how-to-get-the-user-who-triggered-the-event that suggests e.Entity.WriterId could be used.

    Unfortunately for the Trashing event this doesn't seem to work correctly. I have seen the WriterId come through as 0, even when I've triggered a deletion using a different user.

    • Is there a way to retrieve the user who triggered the Trashing event
    • What other approaches can I use to implement this requirement?

    Thanks

  • Shaishav Karnani from digitallymedia.com 349 posts 1631 karma points
    Sep 12, 2019 @ 18:31
    Shaishav Karnani from digitallymedia.com
    0

    Hi,

    You can get the current logged in user from below link. https://stackoverflow.com/questions/25476810/how-to-check-if-user-is-logged-on-not-member

    Now, you can check if this user has permission and if yes then only allow trashing event.

    Cheers,

    Shaishav

  • Peter Bridger 2 posts 72 karma points
    Sep 20, 2019 @ 15:19
    Peter Bridger
    0

    Thanks for your response Shasishav

    My final solution used the following code

    private static bool PrincipleHasPermission(ApplicationContext context, ContentServiceEventAction eventAction )
         {
            // Taken from https://stackoverflow.com/questions/35364220/get-current-user-in-umbraco-version-7-3-5 after trying
            // several more elegant approaches that just didn't work
            var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
            if (userTicket != null)
            {
                var currentUser = context.Services.UserService.GetByUsername(userTicket.Name);
    
                if (currentUser.IsAdmin())
                {
                    return false;
                }
            }
    
            return true;
         }
     }
    
  • 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