From 9c4f0e48a984f739cc97769e1324df899ccbd8fb Mon Sep 17 00:00:00 2001 From: Laura Neto <12862535+lauraneto@users.noreply.github.com> Date: Wed, 7 May 2025 10:09:14 +0200 Subject: [PATCH 1/3] Add special case for audit log mapping when user id is 0 --- .../Factories/AuditLogPresentationFactory.cs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs b/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs index d000a00255da..c039cbcff6d1 100644 --- a/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs +++ b/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs @@ -1,5 +1,6 @@ using Umbraco.Cms.Api.Management.ViewModels; using Umbraco.Cms.Api.Management.ViewModels.AuditLog; +using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.Membership; using Umbraco.Cms.Core.Services; @@ -19,19 +20,26 @@ public AuditLogPresentationFactory(IUserService userService, IUserIdKeyResolver public IEnumerable CreateAuditLogViewModel(IEnumerable auditItems) => auditItems.Select(CreateAuditLogViewModel); - private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem) - { - Guid userKey = _userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult(); - IUser user = _userService.GetAsync(userKey).GetAwaiter().GetResult() - ?? throw new ArgumentException($"Could not find user with id {auditItem.UserId}"); - - return new AuditLogResponseModel + private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem) => + new() { Comment = auditItem.Comment, LogType = auditItem.AuditType, Parameters = auditItem.Parameters, Timestamp = auditItem.CreateDate, - User = new ReferenceByIdModel(user.Key) + User = CreateUserReference(auditItem.UserId).GetAwaiter().GetResult(), }; + + private async Task CreateUserReference(int userId) + { + if (userId == Constants.Security.UnknownUserId) + { + return new ReferenceByIdModel(); + } + + Guid userKey = await _userIdKeyResolver.GetAsync(userId); + IUser user = await _userService.GetAsync(userKey) + ?? throw new ArgumentException($"Could not find user with id {userId}"); + return new ReferenceByIdModel(user.Key); } } From 8dd49eca4a69422b8a018b2753e0ebbad4fadf8b Mon Sep 17 00:00:00 2001 From: Laura Neto <12862535+lauraneto@users.noreply.github.com> Date: Wed, 7 May 2025 10:20:17 +0200 Subject: [PATCH 2/3] Correct the IUserIdKeyResolver documentation --- src/Umbraco.Core/Services/IUserIdKeyResolver.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Core/Services/IUserIdKeyResolver.cs b/src/Umbraco.Core/Services/IUserIdKeyResolver.cs index 30c775edb6a3..b308f16c01aa 100644 --- a/src/Umbraco.Core/Services/IUserIdKeyResolver.cs +++ b/src/Umbraco.Core/Services/IUserIdKeyResolver.cs @@ -5,14 +5,16 @@ public interface IUserIdKeyResolver /// /// Tries to resolve a user key to a user id without fetching the entire user. /// - /// The key of the user. - /// The id of the user, null if the user doesn't exist. + /// The key of the user. + /// The id of the user. + /// Thrown when no user was found with the specified key. public Task GetAsync(Guid key); /// /// Tries to resolve a user id to a user key without fetching the entire user. /// - /// The id of the user. - /// The key of the user, null if the user doesn't exist. + /// The id of the user. + /// The key of the user. + /// Thrown when no user was found with the specified id. public Task GetAsync(int id); } From af9f13075d330cfa25ec768a78e8aaaa9f5cb514 Mon Sep 17 00:00:00 2001 From: Laura Neto <12862535+lauraneto@users.noreply.github.com> Date: Wed, 7 May 2025 11:09:03 +0200 Subject: [PATCH 3/3] Remove unnecessary UserService call --- .../Factories/AuditLogPresentationFactory.cs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs b/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs index c039cbcff6d1..e2cc326a2f79 100644 --- a/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs +++ b/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs @@ -27,19 +27,10 @@ private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem) => LogType = auditItem.AuditType, Parameters = auditItem.Parameters, Timestamp = auditItem.CreateDate, - User = CreateUserReference(auditItem.UserId).GetAwaiter().GetResult(), + User = auditItem.UserId switch + { + Constants.Security.UnknownUserId => new ReferenceByIdModel(), + _ => new ReferenceByIdModel(_userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult()), + }, }; - - private async Task CreateUserReference(int userId) - { - if (userId == Constants.Security.UnknownUserId) - { - return new ReferenceByIdModel(); - } - - Guid userKey = await _userIdKeyResolver.GetAsync(userId); - IUser user = await _userService.GetAsync(userKey) - ?? throw new ArgumentException($"Could not find user with id {userId}"); - return new ReferenceByIdModel(user.Key); - } }