2025-05-13 10:00:17 +02:00
|
|
|
using Asp.Versioning;
|
2023-12-11 08:25:29 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-05-09 08:38:07 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-04-14 09:44:52 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-12-11 08:25:29 +01:00
|
|
|
using Umbraco.Cms.Api.Management.Security.Authorization.Content;
|
2023-04-14 09:44:52 +02:00
|
|
|
using Umbraco.Cms.Api.Management.ViewModels.Document;
|
|
|
|
|
using Umbraco.Cms.Core;
|
2023-12-11 08:25:29 +01:00
|
|
|
using Umbraco.Cms.Core.Actions;
|
2023-04-14 09:44:52 +02:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
2024-02-29 09:41:56 +00:00
|
|
|
using Umbraco.Cms.Core.Security.Authorization;
|
2023-04-14 09:44:52 +02:00
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Services.OperationStatus;
|
2023-12-11 08:25:29 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
using Umbraco.Extensions;
|
2023-04-14 09:44:52 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers.Document;
|
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
[ApiVersion("1.0")]
|
2023-04-14 09:44:52 +02:00
|
|
|
public class CopyDocumentController : DocumentControllerBase
|
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
private readonly IAuthorizationService _authorizationService;
|
2023-04-14 09:44:52 +02:00
|
|
|
private readonly IContentEditingService _contentEditingService;
|
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
|
|
|
|
|
2023-12-11 08:25:29 +01:00
|
|
|
public CopyDocumentController(
|
|
|
|
|
IAuthorizationService authorizationService,
|
|
|
|
|
IContentEditingService contentEditingService,
|
|
|
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
2023-04-14 09:44:52 +02:00
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
_authorizationService = authorizationService;
|
2023-04-14 09:44:52 +02:00
|
|
|
_contentEditingService = contentEditingService;
|
|
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("{id:guid}/copy")]
|
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
2023-08-04 10:51:20 +02:00
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
2024-04-09 08:18:45 +02:00
|
|
|
public async Task<IActionResult> Copy(
|
|
|
|
|
CancellationToken cancellationToken,
|
|
|
|
|
Guid id,
|
|
|
|
|
CopyDocumentRequestModel copyDocumentRequestModel)
|
2023-04-14 09:44:52 +02:00
|
|
|
{
|
2025-05-13 10:00:17 +02:00
|
|
|
AuthorizationResult sourceAuthorizationResult = await _authorizationService.AuthorizeResourceAsync(
|
2023-12-11 08:25:29 +01:00
|
|
|
User,
|
2025-05-13 10:00:17 +02:00
|
|
|
ContentPermissionResource.WithKeys(ActionCopy.ActionLetter, [id]),
|
|
|
|
|
AuthorizationPolicies.ContentPermissionByResource);
|
|
|
|
|
AuthorizationResult destinationAuthorizationResult = await _authorizationService.AuthorizeResourceAsync(
|
|
|
|
|
User,
|
|
|
|
|
ContentPermissionResource.WithKeys(ActionNew.ActionLetter, [copyDocumentRequestModel.Target?.Id]),
|
2023-12-11 08:25:29 +01:00
|
|
|
AuthorizationPolicies.ContentPermissionByResource);
|
|
|
|
|
|
2025-05-13 10:00:17 +02:00
|
|
|
if (sourceAuthorizationResult.Succeeded is false || destinationAuthorizationResult.Succeeded is false)
|
2023-12-11 08:25:29 +01:00
|
|
|
{
|
|
|
|
|
return Forbidden();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 09:44:52 +02:00
|
|
|
Attempt<IContent?, ContentEditingOperationStatus> result = await _contentEditingService.CopyAsync(
|
|
|
|
|
id,
|
2024-01-30 12:19:05 +01:00
|
|
|
copyDocumentRequestModel.Target?.Id,
|
2023-04-14 09:44:52 +02:00
|
|
|
copyDocumentRequestModel.RelateToOriginal,
|
|
|
|
|
copyDocumentRequestModel.IncludeDescendants,
|
|
|
|
|
CurrentUserKey(_backOfficeSecurityAccessor));
|
|
|
|
|
|
|
|
|
|
return result.Success
|
2024-01-22 08:20:45 +01:00
|
|
|
? CreatedAtId<ByKeyDocumentController>(controller => nameof(controller.ByKey), result.Result!.Key)
|
2023-04-14 09:44:52 +02:00
|
|
|
: ContentEditingOperationStatusResult(result.Status);
|
|
|
|
|
}
|
|
|
|
|
}
|