Cannot view audit logs when they contain entries with user id 0 (#19263)

This commit is contained in:
Laura Neto
2025-05-12 12:31:16 +02:00
committed by GitHub
parent 2b00f15d1a
commit b42888dffa
2 changed files with 14 additions and 13 deletions

View File

@@ -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<AuditLogResponseModel> CreateAuditLogViewModel(IEnumerable<IAuditItem> 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()),
},
};
}
}

View File

@@ -5,14 +5,16 @@ public interface IUserIdKeyResolver
/// <summary>
/// Tries to resolve a user key to a user id without fetching the entire user.
/// </summary>
/// <param name="key">The key of the user. </param>
/// <returns>The id of the user, null if the user doesn't exist.</returns>
/// <param name="key">The key of the user.</param>
/// <returns>The id of the user.</returns>
/// <exception cref="InvalidOperationException">Thrown when no user was found with the specified key.</exception>
public Task<int> GetAsync(Guid key);
/// <summary>
/// Tries to resolve a user id to a user key without fetching the entire user.
/// </summary>
/// <param name="id">The id of the user. </param>
/// <returns>The key of the user, null if the user doesn't exist.</returns>
/// <param name="id">The id of the user.</param>
/// <returns>The key of the user.</returns>
/// <exception cref="InvalidOperationException">Thrown when no user was found with the specified id.</exception>
public Task<Guid> GetAsync(int id);
}