2023-05-09 08:38:07 +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-02-20 11:08:22 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Umbraco.Cms.Api.Management.Factories;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Umbraco.Cms.Api.Management.Security.Authorization.Content;
|
2023-02-20 11:08:22 +01: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-02-20 11:08:22 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
|
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-02-20 11:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers.Document;
|
|
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
|
[ApiVersion("1.0")]
|
2023-02-20 11:08:22 +01:00
|
|
|
|
public class UpdateDocumentController : DocumentControllerBase
|
|
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
2023-02-20 11:08:22 +01:00
|
|
|
|
private readonly IContentEditingService _contentEditingService;
|
2023-03-13 10:49:21 +01:00
|
|
|
|
private readonly IDocumentEditingPresentationFactory _documentEditingPresentationFactory;
|
2023-02-20 11:08:22 +01:00
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
public UpdateDocumentController(
|
2023-12-11 08:25:29 +01:00
|
|
|
|
IAuthorizationService authorizationService,
|
2023-02-20 11:08:22 +01:00
|
|
|
|
IContentEditingService contentEditingService,
|
2023-03-13 10:49:21 +01:00
|
|
|
|
IDocumentEditingPresentationFactory documentEditingPresentationFactory,
|
2023-02-20 11:08:22 +01:00
|
|
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
|
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
_authorizationService = authorizationService;
|
2023-02-20 11:08:22 +01:00
|
|
|
|
_contentEditingService = contentEditingService;
|
2023-03-13 10:49:21 +01:00
|
|
|
|
_documentEditingPresentationFactory = documentEditingPresentationFactory;
|
2023-02-20 11:08:22 +01:00
|
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-04 13:20:29 +02:00
|
|
|
|
[HttpPut("{id:guid}")]
|
2023-02-20 11:08:22 +01:00
|
|
|
|
[MapToApiVersion("1.0")]
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2023-08-04 10:51:20 +02:00
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
2023-04-04 13:20:29 +02:00
|
|
|
|
public async Task<IActionResult> Update(Guid id, UpdateDocumentRequestModel requestModel)
|
2023-02-20 11:08:22 +01:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
|
|
|
|
|
|
User,
|
2024-01-22 21:08:20 +01:00
|
|
|
|
ContentPermissionResource.WithKeys(
|
|
|
|
|
|
ActionUpdate.ActionLetter,
|
|
|
|
|
|
id,
|
|
|
|
|
|
requestModel.Variants
|
|
|
|
|
|
.Where(v => v.Culture is not null)
|
|
|
|
|
|
.Select(v => v.Culture!)),
|
2023-12-11 08:25:29 +01:00
|
|
|
|
AuthorizationPolicies.ContentPermissionByResource);
|
|
|
|
|
|
|
|
|
|
|
|
if (!authorizationResult.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Forbidden();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-04 13:20:29 +02:00
|
|
|
|
IContent? content = await _contentEditingService.GetAsync(id);
|
2023-02-20 11:08:22 +01:00
|
|
|
|
if (content == null)
|
|
|
|
|
|
{
|
2023-03-13 15:02:30 +01:00
|
|
|
|
return DocumentNotFound();
|
2023-02-20 11:08:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-13 10:49:21 +01:00
|
|
|
|
ContentUpdateModel model = _documentEditingPresentationFactory.MapUpdateModel(requestModel);
|
2023-03-21 12:41:20 +01:00
|
|
|
|
Attempt<IContent, ContentEditingOperationStatus> result = await _contentEditingService.UpdateAsync(content, model, CurrentUserKey(_backOfficeSecurityAccessor));
|
2023-02-20 11:08:22 +01:00
|
|
|
|
|
|
|
|
|
|
return result.Success
|
|
|
|
|
|
? Ok()
|
|
|
|
|
|
: ContentEditingOperationStatusResult(result.Status);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|