2021-03-04 07:14:54 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2021-09-14 22:13:39 +02:00
|
|
|
using System.Globalization;
|
2024-08-13 12:29:29 +02:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-05-11 14:33:49 +02:00
|
|
|
using Umbraco.Cms.Core.Notifications;
|
2021-02-15 11:41:12 +01:00
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2024-08-13 12:29:29 +02:00
|
|
|
using Umbraco.Cms.Core.Security;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Services;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
namespace Umbraco.Cms.Core.Events;
|
|
|
|
|
|
|
|
|
|
// TODO: lots of duplicate code in this one, refactor
|
|
|
|
|
public sealed class RelateOnTrashNotificationHandler :
|
|
|
|
|
INotificationHandler<ContentMovedNotification>,
|
|
|
|
|
INotificationHandler<ContentMovedToRecycleBinNotification>,
|
|
|
|
|
INotificationHandler<MediaMovedNotification>,
|
|
|
|
|
INotificationHandler<MediaMovedToRecycleBinNotification>
|
2015-01-04 20:46:00 +00:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly IAuditService _auditService;
|
|
|
|
|
private readonly IEntityService _entityService;
|
|
|
|
|
private readonly IRelationService _relationService;
|
2024-08-13 12:29:29 +02:00
|
|
|
private readonly ICoreScopeProvider _scopeProvider;
|
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
2022-06-02 08:18:31 +02:00
|
|
|
private readonly ILocalizedTextService _textService;
|
|
|
|
|
|
2024-08-13 12:29:29 +02:00
|
|
|
[Obsolete("Use the new constructor instead, will be removed in V16")]
|
2022-06-02 08:18:31 +02:00
|
|
|
public RelateOnTrashNotificationHandler(
|
|
|
|
|
IRelationService relationService,
|
|
|
|
|
IEntityService entityService,
|
|
|
|
|
ILocalizedTextService textService,
|
|
|
|
|
IAuditService auditService,
|
|
|
|
|
IScopeProvider scopeProvider)
|
2024-08-13 12:29:29 +02:00
|
|
|
: this(relationService, entityService, textService, auditService, scopeProvider, StaticServiceProvider.Instance.GetRequiredService<IBackOfficeSecurityAccessor>())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RelateOnTrashNotificationHandler(
|
|
|
|
|
IRelationService relationService,
|
|
|
|
|
IEntityService entityService,
|
|
|
|
|
ILocalizedTextService textService,
|
|
|
|
|
IAuditService auditService,
|
|
|
|
|
IScopeProvider scopeProvider,
|
|
|
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
2015-01-04 20:46:00 +00:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
_relationService = relationService;
|
|
|
|
|
_entityService = entityService;
|
|
|
|
|
_textService = textService;
|
|
|
|
|
_auditService = auditService;
|
|
|
|
|
_scopeProvider = scopeProvider;
|
2024-08-13 12:29:29 +02:00
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2019-12-05 14:03:09 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(ContentMovedNotification notification)
|
|
|
|
|
{
|
|
|
|
|
foreach (MoveEventInfo<IContent> item in notification.MoveInfoCollection.Where(x =>
|
|
|
|
|
x.OriginalPath.Contains(Constants.System.RecycleBinContentString)))
|
2020-09-04 00:28:38 +10:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
|
|
|
|
|
IEnumerable<IRelation> relations = _relationService.GetByChildId(item.Entity.Id);
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
foreach (IRelation relation in
|
|
|
|
|
relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
|
|
|
|
|
{
|
|
|
|
|
_relationService.Delete(relation);
|
2015-01-04 20:46:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(ContentMovedToRecycleBinNotification notification)
|
|
|
|
|
{
|
2024-08-13 12:29:29 +02:00
|
|
|
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
|
2015-01-04 20:46:00 +00:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
|
|
|
|
|
IRelationType? relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias);
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
// check that the relation-type exists, if not, then recreate it
|
|
|
|
|
if (relationType == null)
|
|
|
|
|
{
|
|
|
|
|
Guid documentObjectType = Constants.ObjectTypes.Document;
|
|
|
|
|
const string relationTypeName = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName;
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, documentObjectType, false);
|
|
|
|
|
_relationService.Save(relationType);
|
|
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
|
2023-02-27 08:40:17 +01:00
|
|
|
foreach (MoveToRecycleBinEventInfo<IContent> item in notification.MoveInfoCollection)
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
|
|
|
|
IList<string> originalPath = item.OriginalPath.ToDelimitedList();
|
|
|
|
|
var originalParentId = originalPath.Count > 2
|
|
|
|
|
? int.Parse(originalPath[originalPath.Count - 2], CultureInfo.InvariantCulture)
|
|
|
|
|
: Constants.System.Root;
|
|
|
|
|
|
|
|
|
|
// before we can create this relation, we need to ensure that the original parent still exists which
|
|
|
|
|
// may not be the case if the encompassing transaction also deleted it when this item was moved to the bin
|
|
|
|
|
if (_entityService.Exists(originalParentId))
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
|
|
|
|
|
IRelation relation =
|
|
|
|
|
_relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ??
|
|
|
|
|
new Relation(originalParentId, item.Entity.Id, relationType);
|
|
|
|
|
_relationService.Save(relation);
|
|
|
|
|
|
|
|
|
|
_auditService.Add(
|
|
|
|
|
AuditType.Delete,
|
2024-08-13 12:29:29 +02:00
|
|
|
_backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? item.Entity.WriterId,
|
2022-06-02 08:18:31 +02:00
|
|
|
item.Entity.Id,
|
|
|
|
|
UmbracoObjectTypes.Document.GetName(),
|
|
|
|
|
string.Format(_textService.Localize("recycleBin", "contentTrashed"), item.Entity.Id, originalParentId));
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
|
|
|
|
|
scope.Complete();
|
2015-01-04 20:46:00 +00:00
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2021-03-01 17:05:59 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(MediaMovedNotification notification)
|
|
|
|
|
{
|
|
|
|
|
foreach (MoveEventInfo<IMedia> item in notification.MoveInfoCollection.Where(x =>
|
|
|
|
|
x.OriginalPath.Contains(Constants.System.RecycleBinMediaString)))
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
|
|
|
|
|
IEnumerable<IRelation> relations = _relationService.GetByChildId(item.Entity.Id);
|
|
|
|
|
foreach (IRelation relation in
|
|
|
|
|
relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
|
2021-03-01 17:05:59 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
_relationService.Delete(relation);
|
2021-03-01 17:05:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-02 08:18:31 +02:00
|
|
|
}
|
2018-07-27 14:56:18 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
public void Handle(MediaMovedToRecycleBinNotification notification)
|
|
|
|
|
{
|
2024-08-13 12:29:29 +02:00
|
|
|
using (ICoreScope scope = _scopeProvider.CreateCoreScope())
|
2018-07-27 14:56:18 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
const string relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
|
|
|
|
|
IRelationType? relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias);
|
|
|
|
|
|
|
|
|
|
// check that the relation-type exists, if not, then recreate it
|
|
|
|
|
if (relationType == null)
|
2018-07-27 14:56:18 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
Guid documentObjectType = Constants.ObjectTypes.Document;
|
|
|
|
|
const string relationTypeName = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName;
|
|
|
|
|
relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType, documentObjectType, false);
|
|
|
|
|
_relationService.Save(relationType);
|
|
|
|
|
}
|
2020-10-12 15:26:18 +02:00
|
|
|
|
2023-02-27 08:40:17 +01:00
|
|
|
foreach (MoveToRecycleBinEventInfo<IMedia> item in notification.MoveInfoCollection)
|
2022-06-02 08:18:31 +02:00
|
|
|
{
|
|
|
|
|
IList<string> originalPath = item.OriginalPath.ToDelimitedList();
|
|
|
|
|
var originalParentId = originalPath.Count > 2
|
|
|
|
|
? int.Parse(originalPath[originalPath.Count - 2], CultureInfo.InvariantCulture)
|
|
|
|
|
: Constants.System.Root;
|
|
|
|
|
|
|
|
|
|
// before we can create this relation, we need to ensure that the original parent still exists which
|
|
|
|
|
// may not be the case if the encompassing transaction also deleted it when this item was moved to the bin
|
|
|
|
|
if (_entityService.Exists(originalParentId))
|
2018-07-27 14:56:18 +01:00
|
|
|
{
|
2022-06-02 08:18:31 +02:00
|
|
|
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
|
|
|
|
|
IRelation relation =
|
|
|
|
|
_relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ??
|
|
|
|
|
new Relation(originalParentId, item.Entity.Id, relationType);
|
|
|
|
|
_relationService.Save(relation);
|
|
|
|
|
_auditService.Add(
|
|
|
|
|
AuditType.Delete,
|
2024-08-13 12:29:29 +02:00
|
|
|
_backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? item.Entity.WriterId,
|
2022-06-02 08:18:31 +02:00
|
|
|
item.Entity.Id,
|
|
|
|
|
UmbracoObjectTypes.Media.GetName(),
|
|
|
|
|
string.Format(_textService.Localize("recycleBin", "mediaTrashed"), item.Entity.Id, originalParentId));
|
2018-07-27 14:56:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-01 20:12:36 +01:00
|
|
|
|
2022-06-02 08:18:31 +02:00
|
|
|
scope.Complete();
|
2018-07-27 14:56:18 +01:00
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|