-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathPublishElementController.cs
More file actions
79 lines (71 loc) · 3.45 KB
/
PublishElementController.cs
File metadata and controls
79 lines (71 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Api.Management.ViewModels.Element;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Actions;
using Umbraco.Cms.Core.Models.ContentPublishing;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Security.Authorization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Extensions;
namespace Umbraco.Cms.Api.Management.Controllers.Element;
[ApiVersion("1.0")]
public class PublishElementController : ElementControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IElementPublishingService _elementPublishingService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly IDocumentPresentationFactory _documentPresentationFactory;
public PublishElementController(
IAuthorizationService authorizationService,
IElementPublishingService elementPublishingService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDocumentPresentationFactory documentPresentationFactory)
{
_authorizationService = authorizationService;
_elementPublishingService = elementPublishingService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_documentPresentationFactory = documentPresentationFactory;
}
[HttpPut("{id:guid}/publish")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Publish(CancellationToken cancellationToken, Guid id, PublishElementRequestModel requestModel)
{
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
User,
ElementPermissionResource.WithKeys(
ActionElementPublish.ActionLetter,
id,
requestModel.PublishSchedules
.Where(x => x.Culture is not null)
.Select(x => x.Culture!)),
AuthorizationPolicies.ElementPermissionByResource);
if (!authorizationResult.Succeeded)
{
return Forbidden();
}
// TODO ELEMENTS: IDocumentPresentationFactory carries the implementation of this mapping - it should probably be renamed
var tempModel = new PublishDocumentRequestModel { PublishSchedules = requestModel.PublishSchedules };
Attempt<List<CulturePublishScheduleModel>, ContentPublishingOperationStatus> modelResult = _documentPresentationFactory.CreateCulturePublishScheduleModels(tempModel);
if (modelResult.Success is false)
{
return ElementPublishingOperationStatusResult(modelResult.Status);
}
Attempt<ContentPublishingResult, ContentPublishingOperationStatus> attempt = await _elementPublishingService.PublishAsync(
id,
modelResult.Result,
CurrentUserKey(_backOfficeSecurityAccessor));
return attempt.Success
? Ok()
: ElementPublishingOperationStatusResult(attempt.Status, attempt.Result.InvalidPropertyAliases);
}
}