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:
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?
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!
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:
and:
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?
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.
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.
Thanks, your suggestion pushed me in the right direction!
is working on a reply...