diff --git a/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs b/src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs index d000a00255..e2cc326a2f 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,17 @@ public class AuditLogPresentationFactory : IAuditLogPresentationFactory 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 = auditItem.UserId switch + { + Constants.Security.UnknownUserId => new ReferenceByIdModel(), + _ => new ReferenceByIdModel(_userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult()), + }, }; - } } diff --git a/src/Umbraco.Core/Services/IUserIdKeyResolver.cs b/src/Umbraco.Core/Services/IUserIdKeyResolver.cs index 30c775edb6..b308f16c01 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); }