Skip to content

Audit service rework #19346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: v17/improvement/introduce-audit-entry-service
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/Umbraco.Core/Events/RelateOnCopyNotificationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.

using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Core.Events;

public class RelateOnCopyNotificationHandler : INotificationHandler<ContentCopiedNotification>
public class RelateOnCopyNotificationHandler :
INotificationHandler<ContentCopiedNotification>,
INotificationAsyncHandler<ContentCopiedNotification>
{
private readonly IAuditService _auditService;
private readonly IUserIdKeyResolver _userIdKeyResolver;
private readonly IRelationService _relationService;

public RelateOnCopyNotificationHandler(IRelationService relationService, IAuditService auditService)
public RelateOnCopyNotificationHandler(
IRelationService relationService,
IAuditService auditService,
IUserIdKeyResolver userIdKeyResolver)
{
_relationService = relationService;
_auditService = auditService;
_userIdKeyResolver = userIdKeyResolver;
}

public void Handle(ContentCopiedNotification notification)
[Obsolete("Use the non-obsolete constructor instead. Scheduled for removal in V19.")]
public RelateOnCopyNotificationHandler(
IRelationService relationService,
IAuditService auditService)
: this(
relationService,
auditService,
StaticServiceProvider.Instance.GetRequiredService<IUserIdKeyResolver>())
{
}

/// <inheritdoc />
public async Task HandleAsync(ContentCopiedNotification notification, CancellationToken cancellationToken)
{
if (notification.RelateToOriginal == false)
{
return;
}

IRelationType? relationType = _relationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelateDocumentOnCopyAlias);
IRelationType? relationType =
_relationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelateDocumentOnCopyAlias);

if (relationType == null)
{
Expand All @@ -43,11 +65,16 @@ public void Handle(ContentCopiedNotification notification)
var relation = new Relation(notification.Original.Id, notification.Copy.Id, relationType);
_relationService.Save(relation);

_auditService.Add(
Guid writerKey = await _userIdKeyResolver.GetAsync(notification.Copy.WriterId);
await _auditService.AddAsync(
AuditType.Copy,
notification.Copy.WriterId,
writerKey,
notification.Copy.Id,
UmbracoObjectTypes.Document.GetName() ?? string.Empty,
$"Copied content with Id: '{notification.Copy.Id}' related to original content with Id: '{notification.Original.Id}'");
}

[Obsolete("Use the INotificationAsyncHandler.HandleAsync implementation instead. Scheduled for removal in V19.")]
public void Handle(ContentCopiedNotification notification) =>
HandleAsync(notification, CancellationToken.None).GetAwaiter().GetResult();
}
Loading