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?
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;
}
}
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 settingargs.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 theWriterId
come through as0
, even when I've triggered a deletion using a different user.Thanks
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
Thanks for your response Shasishav
My final solution used the following code
is working on a reply...