Skip to content

Commit b42888d

Browse files
authored
Cannot view audit logs when they contain entries with user id 0 (#19263)
1 parent 2b00f15 commit b42888d

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/Umbraco.Cms.Api.Management/Factories/AuditLogPresentationFactory.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Umbraco.Cms.Api.Management.ViewModels;
22
using Umbraco.Cms.Api.Management.ViewModels.AuditLog;
3+
using Umbraco.Cms.Core;
34
using Umbraco.Cms.Core.Models;
45
using Umbraco.Cms.Core.Models.Membership;
56
using Umbraco.Cms.Core.Services;
@@ -19,19 +20,17 @@ public AuditLogPresentationFactory(IUserService userService, IUserIdKeyResolver
1920

2021
public IEnumerable<AuditLogResponseModel> CreateAuditLogViewModel(IEnumerable<IAuditItem> auditItems) => auditItems.Select(CreateAuditLogViewModel);
2122

22-
private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem)
23-
{
24-
Guid userKey = _userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult();
25-
IUser user = _userService.GetAsync(userKey).GetAwaiter().GetResult()
26-
?? throw new ArgumentException($"Could not find user with id {auditItem.UserId}");
27-
28-
return new AuditLogResponseModel
23+
private AuditLogResponseModel CreateAuditLogViewModel(IAuditItem auditItem) =>
24+
new()
2925
{
3026
Comment = auditItem.Comment,
3127
LogType = auditItem.AuditType,
3228
Parameters = auditItem.Parameters,
3329
Timestamp = auditItem.CreateDate,
34-
User = new ReferenceByIdModel(user.Key)
30+
User = auditItem.UserId switch
31+
{
32+
Constants.Security.UnknownUserId => new ReferenceByIdModel(),
33+
_ => new ReferenceByIdModel(_userIdKeyResolver.GetAsync(auditItem.UserId).GetAwaiter().GetResult()),
34+
},
3535
};
36-
}
3736
}

src/Umbraco.Core/Services/IUserIdKeyResolver.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ public interface IUserIdKeyResolver
55
/// <summary>
66
/// Tries to resolve a user key to a user id without fetching the entire user.
77
/// </summary>
8-
/// <param name="key">The key of the user. </param>
9-
/// <returns>The id of the user, null if the user doesn't exist.</returns>
8+
/// <param name="key">The key of the user.</param>
9+
/// <returns>The id of the user.</returns>
10+
/// <exception cref="InvalidOperationException">Thrown when no user was found with the specified key.</exception>
1011
public Task<int> GetAsync(Guid key);
1112

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

0 commit comments

Comments
 (0)