Skip to content

Commit 82fcede

Browse files
authored
Management API authorization for requests to rollback. (#18240)
1 parent 57c3279 commit 82fcede

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
using Asp.Versioning;
2+
using Microsoft.AspNetCore.Authorization;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Extensions.DependencyInjection;
46
using Umbraco.Cms.Core;
7+
using Umbraco.Cms.Core.Actions;
8+
using Umbraco.Cms.Core.DependencyInjection;
9+
using Umbraco.Cms.Core.Models;
510
using Umbraco.Cms.Core.Security;
11+
using Umbraco.Cms.Core.Security.Authorization;
612
using Umbraco.Cms.Core.Services;
713
using Umbraco.Cms.Core.Services.OperationStatus;
14+
using Umbraco.Cms.Web.Common.Authorization;
15+
using Umbraco.Extensions;
816

917
namespace Umbraco.Cms.Api.Management.Controllers.DocumentVersion;
1018

@@ -13,13 +21,29 @@ public class RollbackDocumentVersionController : DocumentVersionControllerBase
1321
{
1422
private readonly IContentVersionService _contentVersionService;
1523
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
24+
private readonly IAuthorizationService _authorizationService;
1625

26+
[ActivatorUtilitiesConstructor]
1727
public RollbackDocumentVersionController(
1828
IContentVersionService contentVersionService,
19-
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
29+
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
30+
IAuthorizationService authorizationService)
2031
{
2132
_contentVersionService = contentVersionService;
2233
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
34+
_authorizationService = authorizationService;
35+
}
36+
37+
// TODO (V16): Remove this constructor.
38+
[Obsolete("Please use the constructor taking all parameters. This constructor will be removed in V16.")]
39+
public RollbackDocumentVersionController(
40+
IContentVersionService contentVersionService,
41+
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
42+
: this(
43+
contentVersionService,
44+
backOfficeSecurityAccessor,
45+
StaticServiceProvider.Instance.GetRequiredService<IAuthorizationService>())
46+
{
2347
}
2448

2549
[MapToApiVersion("1.0")]
@@ -29,11 +53,29 @@ public RollbackDocumentVersionController(
2953
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
3054
public async Task<IActionResult> Rollback(CancellationToken cancellationToken, Guid id, string? culture)
3155
{
32-
Attempt<ContentVersionOperationStatus> attempt =
56+
Attempt<IContent?, ContentVersionOperationStatus> getContentAttempt =
57+
await _contentVersionService.GetAsync(id);
58+
if (getContentAttempt.Success is false || getContentAttempt.Result is null)
59+
{
60+
return MapFailure(getContentAttempt.Status);
61+
}
62+
63+
IContent content = getContentAttempt.Result;
64+
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
65+
User,
66+
ContentPermissionResource.WithKeys(ActionRollback.ActionLetter, content.Key),
67+
AuthorizationPolicies.ContentPermissionByResource);
68+
69+
if (!authorizationResult.Succeeded)
70+
{
71+
return Forbidden();
72+
}
73+
74+
Attempt<ContentVersionOperationStatus> rollBackAttempt =
3375
await _contentVersionService.RollBackAsync(id, culture, CurrentUserKey(_backOfficeSecurityAccessor));
3476

35-
return attempt.Success
77+
return rollBackAttempt.Success
3678
? Ok()
37-
: MapFailure(attempt.Result);
79+
: MapFailure(rollBackAttempt.Result);
3880
}
3981
}

0 commit comments

Comments
 (0)