Handle user id 0 (Unknown/System) when building content version response (#19361)

Handle user id 0 (Unknown/System) when building content version response model

`IUserIdKeyResolver.GetAsync` throws an exception when a user is not found.
As user 0 does not really exist, the exception was being thrown.
We now handle this scenario to return an empty reference.
This commit is contained in:
Laura Neto
2025-05-20 17:01:03 +02:00
committed by GitHub
parent 419625a919
commit 225fed5f72

View File

@@ -1,5 +1,6 @@
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
@@ -19,17 +20,26 @@ internal sealed class DocumentVersionPresentationFactory : IDocumentVersionPrese
_userIdKeyResolver = userIdKeyResolver;
}
public async Task<DocumentVersionItemResponseModel> CreateAsync(ContentVersionMeta contentVersion) =>
new(
public async Task<DocumentVersionItemResponseModel> CreateAsync(ContentVersionMeta contentVersion)
{
ReferenceByIdModel userReference = contentVersion.UserId switch
{
Constants.Security.UnknownUserId => new ReferenceByIdModel(),
_ => new ReferenceByIdModel(await _userIdKeyResolver.GetAsync(contentVersion.UserId)),
};
return new DocumentVersionItemResponseModel(
contentVersion.VersionId.ToGuid(), // this is a magic guid since versions do not have keys in the DB
new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentId, UmbracoObjectTypes.Document).Result),
new ReferenceByIdModel(_entityService.GetKey(contentVersion.ContentTypeId, UmbracoObjectTypes.DocumentType)
.Result),
new ReferenceByIdModel(await _userIdKeyResolver.GetAsync(contentVersion.UserId)),
new ReferenceByIdModel(
_entityService.GetKey(contentVersion.ContentTypeId, UmbracoObjectTypes.DocumentType)
.Result),
userReference,
new DateTimeOffset(contentVersion.VersionDate),
contentVersion.CurrentPublishedVersion,
contentVersion.CurrentDraftVersion,
contentVersion.PreventCleanup);
}
public async Task<IEnumerable<DocumentVersionItemResponseModel>> CreateMultipleAsync(IEnumerable<ContentVersionMeta> contentVersions)
=> await CreateMultipleImplAsync(contentVersions).ToArrayAsync();