From 56b3f17355967e8f3570d844bd93ec4dda7924e7 Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:29:29 +0200 Subject: [PATCH] V14: Don't duplicate audit loggin for media trashing (#16903) * Remove auditlogging from media service * Refactor to use backofficeSecurityaccessor to get backoffice user * Fix constructor error * Reinstate media service move * Go back to audit type delete --------- Co-authored-by: Elitsa --- src/Umbraco.Core/Services/MediaService.cs | 1 - .../RelateOnTrashNotificationHandler.cs | 28 +++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 755b36ae82..c754aa244a 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -1002,7 +1002,6 @@ namespace Umbraco.Cms.Core.Services MoveToRecycleBinEventInfo[] moveInfo = moves.Select(x => new MoveToRecycleBinEventInfo(x.Item1, x.Item2)).ToArray(); scope.Notifications.Publish(new MediaMovedToRecycleBinNotification(moveInfo, messages).WithStateFrom(movingToRecycleBinNotification)); Audit(AuditType.Move, userId, media.Id, "Move Media to recycle bin"); - scope.Complete(); } diff --git a/src/Umbraco.Infrastructure/Events/RelateOnTrashNotificationHandler.cs b/src/Umbraco.Infrastructure/Events/RelateOnTrashNotificationHandler.cs index c2a434dd54..48113586b0 100644 --- a/src/Umbraco.Infrastructure/Events/RelateOnTrashNotificationHandler.cs +++ b/src/Umbraco.Infrastructure/Events/RelateOnTrashNotificationHandler.cs @@ -2,12 +2,14 @@ // See LICENSE for more details. using System.Globalization; +using Microsoft.Extensions.DependencyInjection; +using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Notifications; using Umbraco.Cms.Core.Scoping; +using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Services; using Umbraco.Extensions; -using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope; namespace Umbraco.Cms.Core.Events; @@ -21,21 +23,35 @@ public sealed class RelateOnTrashNotificationHandler : private readonly IAuditService _auditService; private readonly IEntityService _entityService; private readonly IRelationService _relationService; - private readonly IScopeProvider _scopeProvider; + private readonly ICoreScopeProvider _scopeProvider; + private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; private readonly ILocalizedTextService _textService; + [Obsolete("Use the new constructor instead, will be removed in V16")] public RelateOnTrashNotificationHandler( IRelationService relationService, IEntityService entityService, ILocalizedTextService textService, IAuditService auditService, IScopeProvider scopeProvider) + : this(relationService, entityService, textService, auditService, scopeProvider, StaticServiceProvider.Instance.GetRequiredService()) + { + } + + public RelateOnTrashNotificationHandler( + IRelationService relationService, + IEntityService entityService, + ILocalizedTextService textService, + IAuditService auditService, + IScopeProvider scopeProvider, + IBackOfficeSecurityAccessor backOfficeSecurityAccessor) { _relationService = relationService; _entityService = entityService; _textService = textService; _auditService = auditService; _scopeProvider = scopeProvider; + _backOfficeSecurityAccessor = backOfficeSecurityAccessor; } public void Handle(ContentMovedNotification notification) @@ -56,7 +72,7 @@ public sealed class RelateOnTrashNotificationHandler : public void Handle(ContentMovedToRecycleBinNotification notification) { - using (IScope scope = _scopeProvider.CreateScope()) + using (ICoreScope scope = _scopeProvider.CreateCoreScope()) { const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias; IRelationType? relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias); @@ -90,7 +106,7 @@ public sealed class RelateOnTrashNotificationHandler : _auditService.Add( AuditType.Delete, - item.Entity.WriterId, + _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? item.Entity.WriterId, item.Entity.Id, UmbracoObjectTypes.Document.GetName(), string.Format(_textService.Localize("recycleBin", "contentTrashed"), item.Entity.Id, originalParentId)); @@ -118,7 +134,7 @@ public sealed class RelateOnTrashNotificationHandler : public void Handle(MediaMovedToRecycleBinNotification notification) { - using (IScope scope = _scopeProvider.CreateScope()) + using (ICoreScope scope = _scopeProvider.CreateCoreScope()) { const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias; IRelationType? relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias); @@ -150,7 +166,7 @@ public sealed class RelateOnTrashNotificationHandler : _relationService.Save(relation); _auditService.Add( AuditType.Delete, - item.Entity.CreatorId, + _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? item.Entity.WriterId, item.Entity.Id, UmbracoObjectTypes.Media.GetName(), string.Format(_textService.Localize("recycleBin", "mediaTrashed"), item.Entity.Id, originalParentId));