Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,19 +20,17 @@ public AuditLogPresentationFactory(IUserService userService, IUserIdKeyResolver

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the user service does no extra validations when retrieving the user, I removed this call entirely, as it seemed unnecessary as we only need the key.

?? 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()),
},
};
}
}
10 changes: 6 additions & 4 deletions src/Umbraco.Core/Services/IUserIdKeyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}