1
1
using Asp . Versioning ;
2
+ using Microsoft . AspNetCore . Authorization ;
2
3
using Microsoft . AspNetCore . Http ;
3
4
using Microsoft . AspNetCore . Mvc ;
5
+ using Microsoft . Extensions . DependencyInjection ;
4
6
using Umbraco . Cms . Core ;
7
+ using Umbraco . Cms . Core . Actions ;
8
+ using Umbraco . Cms . Core . DependencyInjection ;
9
+ using Umbraco . Cms . Core . Models ;
5
10
using Umbraco . Cms . Core . Security ;
11
+ using Umbraco . Cms . Core . Security . Authorization ;
6
12
using Umbraco . Cms . Core . Services ;
7
13
using Umbraco . Cms . Core . Services . OperationStatus ;
14
+ using Umbraco . Cms . Web . Common . Authorization ;
15
+ using Umbraco . Extensions ;
8
16
9
17
namespace Umbraco . Cms . Api . Management . Controllers . DocumentVersion ;
10
18
@@ -13,13 +21,29 @@ public class RollbackDocumentVersionController : DocumentVersionControllerBase
13
21
{
14
22
private readonly IContentVersionService _contentVersionService ;
15
23
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor ;
24
+ private readonly IAuthorizationService _authorizationService ;
16
25
26
+ [ ActivatorUtilitiesConstructor ]
17
27
public RollbackDocumentVersionController (
18
28
IContentVersionService contentVersionService ,
19
- IBackOfficeSecurityAccessor backOfficeSecurityAccessor )
29
+ IBackOfficeSecurityAccessor backOfficeSecurityAccessor ,
30
+ IAuthorizationService authorizationService )
20
31
{
21
32
_contentVersionService = contentVersionService ;
22
33
_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
+ {
23
47
}
24
48
25
49
[ MapToApiVersion ( "1.0" ) ]
@@ -29,11 +53,29 @@ public RollbackDocumentVersionController(
29
53
[ ProducesResponseType ( typeof ( ProblemDetails ) , StatusCodes . Status400BadRequest ) ]
30
54
public async Task < IActionResult > Rollback ( CancellationToken cancellationToken , Guid id , string ? culture )
31
55
{
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 =
33
75
await _contentVersionService . RollBackAsync ( id , culture , CurrentUserKey ( _backOfficeSecurityAccessor ) ) ;
34
76
35
- return attempt . Success
77
+ return rollBackAttempt . Success
36
78
? Ok ( )
37
- : MapFailure ( attempt . Result ) ;
79
+ : MapFailure ( rollBackAttempt . Result ) ;
38
80
}
39
81
}
0 commit comments