-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Elements: Add rollback #21393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kjac
merged 8 commits into
v17/feature/global-elements
from
v17/feature/global-elements-add-rollback
Jan 15, 2026
Merged
Elements: Add rollback #21393
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ad0dbc
Services, repos and tests
kjac f8e0f96
Endpoints for Elements versioning
kjac 17287da
Add extra test to prove handling of pinned versions
kjac d99686e
Renaming from PR review
kjac b27be8d
Update tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Service…
kjac 9e4604d
More code clean-up after review
kjac 00e4057
Use correct deleting/deleted versions notifications
kjac 444db18
Merge remote-tracking branch 'origin/v17/feature/global-elements-add-…
kjac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Umbraco.Cms.Api.Management/Controllers/ElementVersion/AllElementVersionController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Asp.Versioning; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Api.Common.ViewModels.Pagination; | ||
| using Umbraco.Cms.Api.Management.Factories; | ||
| using Umbraco.Cms.Api.Management.ViewModels.Element; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.ElementVersion; | ||
|
|
||
| [ApiVersion("1.0")] | ||
| public class AllElementVersionController : ElementVersionControllerBase | ||
| { | ||
| private readonly IElementVersionService _elementVersionService; | ||
| private readonly IElementVersionPresentationFactory _elementVersionPresentationFactory; | ||
|
|
||
| public AllElementVersionController( | ||
| IElementVersionService elementVersionService, | ||
| IElementVersionPresentationFactory elementVersionPresentationFactory) | ||
| { | ||
| _elementVersionService = elementVersionService; | ||
| _elementVersionPresentationFactory = elementVersionPresentationFactory; | ||
| } | ||
|
|
||
| [MapToApiVersion("1.0")] | ||
| [HttpGet] | ||
| [ProducesResponseType(typeof(PagedViewModel<ElementVersionItemResponseModel>), StatusCodes.Status200OK)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> All( | ||
| CancellationToken cancellationToken, | ||
| [Required] Guid elementId, | ||
| string? culture, | ||
| int skip = 0, | ||
| int take = 100) | ||
| { | ||
| Attempt<PagedModel<ContentVersionMeta>?, ContentVersionOperationStatus> attempt = | ||
| await _elementVersionService.GetPagedContentVersionsAsync(elementId, culture, skip, take); | ||
|
|
||
| if (attempt.Success is false) | ||
| { | ||
| return MapFailure(attempt.Status); | ||
| } | ||
|
|
||
| var pagedViewModel = new PagedViewModel<ElementVersionItemResponseModel> | ||
| { | ||
| Total = attempt.Result!.Total, | ||
| Items = await _elementVersionPresentationFactory.CreateMultipleAsync(attempt.Result!.Items), | ||
| }; | ||
|
|
||
| return Ok(pagedViewModel); | ||
| } | ||
|
Check warning on line 56 in src/Umbraco.Cms.Api.Management/Controllers/ElementVersion/AllElementVersionController.cs
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
src/Umbraco.Cms.Api.Management/Controllers/ElementVersion/ByKeyElementVersionController.cs
|
kjac marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using Asp.Versioning; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Api.Management.ViewModels.Element; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Mapping; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.ElementVersion; | ||
|
|
||
| [ApiVersion("1.0")] | ||
| public class ByKeyElementVersionController : ElementVersionControllerBase | ||
| { | ||
| private readonly IElementVersionService _elementVersionService; | ||
| private readonly IUmbracoMapper _umbracoMapper; | ||
|
|
||
| public ByKeyElementVersionController( | ||
| IElementVersionService elementVersionService, | ||
| IUmbracoMapper umbracoMapper) | ||
| { | ||
| _elementVersionService = elementVersionService; | ||
| _umbracoMapper = umbracoMapper; | ||
| } | ||
|
|
||
| [MapToApiVersion("1.0")] | ||
| [HttpGet("{id:guid}")] | ||
| [ProducesResponseType(typeof(ElementVersionResponseModel), StatusCodes.Status200OK)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> ByKey(CancellationToken cancellationToken, Guid id) | ||
| { | ||
| Attempt<IElement?, ContentVersionOperationStatus> attempt = | ||
| await _elementVersionService.GetAsync(id); | ||
|
|
||
| return attempt.Success | ||
| ? Ok(_umbracoMapper.Map<ElementVersionResponseModel>(attempt.Result)) | ||
| : MapFailure(attempt.Status); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/Umbraco.Cms.Api.Management/Controllers/ElementVersion/ElementVersionControllerBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Api.Management.Controllers.DocumentVersion; | ||
| using Umbraco.Cms.Api.Management.Routing; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.ElementVersion; | ||
|
|
||
| [VersionedApiBackOfficeRoute($"{Constants.UdiEntityType.Element}-version")] | ||
| [ApiExplorerSettings(GroupName = $"{nameof(Constants.UdiEntityType.Element)} Version")] | ||
| public abstract class ElementVersionControllerBase : ManagementApiControllerBase | ||
| { | ||
| protected IActionResult MapFailure(ContentVersionOperationStatus status) | ||
| => DocumentVersionControllerBase.MapFailure(status); | ||
| } |
59 changes: 59 additions & 0 deletions
59
...Umbraco.Cms.Api.Management/Controllers/ElementVersion/RollbackElementVersionController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using Asp.Versioning; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Security; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.ElementVersion; | ||
|
|
||
| [ApiVersion("1.0")] | ||
| public class RollbackElementVersionController : ElementVersionControllerBase | ||
| { | ||
| private readonly IElementVersionService _elementVersionService; | ||
| private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
|
||
| public RollbackElementVersionController( | ||
| IElementVersionService elementVersionService, | ||
| IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
| { | ||
| _elementVersionService = elementVersionService; | ||
| _backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
| } | ||
|
|
||
| [MapToApiVersion("1.0")] | ||
| [HttpPost("{id:guid}/rollback")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> Rollback(CancellationToken cancellationToken, Guid id, string? culture) | ||
| { | ||
| Attempt<IElement?, ContentVersionOperationStatus> getContentAttempt = await _elementVersionService.GetAsync(id); | ||
| if (getContentAttempt.Success is false || getContentAttempt.Result is null) | ||
| { | ||
| return MapFailure(getContentAttempt.Status); | ||
| } | ||
|
|
||
|
|
||
| // TODO ELEMENTS: handle auth | ||
| // IElement element = getContentAttempt.Result; | ||
| // AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync( | ||
| // User, | ||
| // ContentPermissionResource.WithKeys(ActionRollback.ActionLetter, element.Key), | ||
| // AuthorizationPolicies.ContentPermissionByResource); | ||
| // | ||
| // if (!authorizationResult.Succeeded) | ||
| // { | ||
| // return Forbidden(); | ||
| // } | ||
|
|
||
| Attempt<ContentVersionOperationStatus> rollBackAttempt = | ||
| await _elementVersionService.RollBackAsync(id, culture, CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
|
||
| return rollBackAttempt.Success | ||
| ? Ok() | ||
| : MapFailure(rollBackAttempt.Result); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
...Api.Management/Controllers/ElementVersion/UpdatePreventCleanupElementVersionController.cs
|
kjac marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using Asp.Versioning; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Security; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.ElementVersion; | ||
|
|
||
| [ApiVersion("1.0")] | ||
| public class UpdatePreventCleanupElementVersionController : ElementVersionControllerBase | ||
| { | ||
| private readonly IElementVersionService _elementVersionService; | ||
| private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
|
||
| public UpdatePreventCleanupElementVersionController( | ||
| IElementVersionService elementVersionService, | ||
| IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
| { | ||
| _elementVersionService = elementVersionService; | ||
| _backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
| } | ||
|
|
||
| [MapToApiVersion("1.0")] | ||
| [HttpPut("{id:guid}/prevent-cleanup")] | ||
| [ProducesResponseType(StatusCodes.Status200OK)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> Set(CancellationToken cancellationToken, Guid id, bool preventCleanup) | ||
| { | ||
| Attempt<ContentVersionOperationStatus> attempt = | ||
| await _elementVersionService.SetPreventCleanupAsync(id, preventCleanup, CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
|
||
| return attempt.Success | ||
| ? Ok() | ||
| : MapFailure(attempt.Result); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Umbraco.Cms.Api.Management/Factories/ContentVersionPresentationFactoryBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Umbraco.Cms.Api.Management.ViewModels; | ||
| using Umbraco.Cms.Api.Management.ViewModels.Document; | ||
| using Umbraco.Cms.Api.Management.ViewModels.Element; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Extensions; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Factories; | ||
|
|
||
| internal abstract class ContentVersionPresentationFactoryBase<TVersionItemResponseModel> | ||
| { | ||
| private readonly IEntityService _entityService; | ||
| private readonly IUserIdKeyResolver _userIdKeyResolver; | ||
|
|
||
| protected abstract UmbracoObjectTypes ItemObjectType { get; } | ||
|
|
||
| protected ContentVersionPresentationFactoryBase(IEntityService entityService, IUserIdKeyResolver userIdKeyResolver) | ||
| { | ||
| _entityService = entityService; | ||
| _userIdKeyResolver = userIdKeyResolver; | ||
| } | ||
|
|
||
| protected abstract TVersionItemResponseModel VersionItemResponseModelFactory( | ||
|
kjac marked this conversation as resolved.
|
||
| Guid versionId, | ||
| ReferenceByIdModel item, | ||
| ReferenceByIdModel documentType, | ||
| ReferenceByIdModel user, | ||
| DateTimeOffset versionDate, | ||
| bool isCurrentPublishedVersion, | ||
| bool isCurrentDraftVersion, | ||
| bool preventCleanup); | ||
|
|
||
| public async Task<TVersionItemResponseModel> CreateAsync(ContentVersionMeta contentVersion) | ||
| { | ||
| ReferenceByIdModel userReference = contentVersion.UserId switch | ||
| { | ||
| Constants.Security.UnknownUserId => new ReferenceByIdModel(), | ||
| _ => new ReferenceByIdModel(await _userIdKeyResolver.GetAsync(contentVersion.UserId)), | ||
| }; | ||
|
|
||
| return VersionItemResponseModelFactory( | ||
| contentVersion.VersionId.ToGuid(), // this is a magic guid since versions do not have keys in the DB | ||
| new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentId, ItemObjectType).Result), | ||
| new ReferenceByIdModel( | ||
| _entityService.GetKey(contentVersion.ContentTypeId, UmbracoObjectTypes.DocumentType) | ||
| .Result), | ||
| userReference, | ||
| new DateTimeOffset(contentVersion.VersionDate), | ||
| contentVersion.CurrentPublishedVersion, | ||
| contentVersion.CurrentDraftVersion, | ||
| contentVersion.PreventCleanup); | ||
| } | ||
|
|
||
| public async Task<IEnumerable<TVersionItemResponseModel>> CreateMultipleAsync(IEnumerable<ContentVersionMeta> contentVersions) | ||
| => await CreateMultipleImplAsync(contentVersions).ToArrayAsync(); | ||
|
|
||
| private async IAsyncEnumerable<TVersionItemResponseModel> CreateMultipleImplAsync(IEnumerable<ContentVersionMeta> contentVersions) | ||
| { | ||
| foreach (ContentVersionMeta contentVersion in contentVersions) | ||
| { | ||
| yield return await CreateAsync(contentVersion); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.