Skip to content

Commit

Permalink
Improve the Audit Trails views (#17104)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Dec 4, 2024
1 parent ef644be commit a7a6675
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
@model AuditTrailEventViewModel

<table class="table table-sm table-hover mb-0">
<h2 class="h3">@T["Event Info"]</h2>

<table class="table table-sm table-hover">
<tbody>
<tr>
<th class="border-top-0 w-25">@T["Category"]</th>
<td class="border-top-0">@Model.Descriptor.LocalizedCategory(Context.RequestServices)</td>
</tr>
<tr>
<th>@T["Event"]</th>
<th>@T["Event name"]</th>
<td>@Model.Descriptor.LocalizedName(Context.RequestServices)</td>
</tr>
<tr>
<th>@T["User Name"]</th>
<th>@T["Triggered by username"]</th>
<td>@Model.AuditTrailEvent.UserName</td>
</tr>
<tr>
<th>@T["User ID"]</th>
<th>@T["Triggered by user ID"]</th>
<td>@Model.AuditTrailEvent.UserId</td>
</tr>
<tr>
<th>@T["Timestamp"]</th>
<th>@T["Triggered at timestamp"]</th>
<td>@await DisplayAsync(await New.DateTime(Utc: Model.AuditTrailEvent.CreatedUtc, Format: "g"))</td>
</tr>
<tr class="border-bottom">
<th>@T["Client IP Address"]</th>
<th>@T["Client IP address"]</th>
<td>@Model.AuditTrailEvent.ClientIpAddress</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
title="@T["Category"]">@Model.Descriptor.LocalizedCategory(Context.RequestServices)</a>
</span>


<span class="badge ta-badge font-weight-normal">
<i class="fa-solid fa-bolt text-secondary" aria-hidden="true"></i>
<a href="?q=event:@Model.Descriptor.Name" data-bs-toggle="tooltip"
title="@T["Event Name"]">@Model.Descriptor.LocalizedName(Context.RequestServices)</a>
title="@T["Event Name"]">@Model.Descriptor.LocalizedName(Context.RequestServices)</a>
</span>

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
var oldText = Model.PreviousContentItem?.DisplayText;
}

<table class="table table-sm table-hover mb-0">
<h2 class="h3">@T["Content Item Info"]</h2>

<table class="table table-sm table-hover">
<tbody>
<tr>
<th class="border-top-0 w-25">@T["Content Item Id"]</th>
Expand Down Expand Up @@ -43,9 +45,9 @@
{
<td>
<a asp-route-area="OrchardCore.Contents"
asp-action="Display"
asp-controller="AuditTrailContent"
asp-route-auditTrailEventId="@Model.AuditTrailEvent.EventId">
asp-action="Display"
asp-controller="AuditTrailContent"
asp-route-auditTrailEventId="@Model.AuditTrailEvent.EventId">
@T["Version {0}", versionNumber]
</a>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ namespace OrchardCore.Users.AuditTrail.Drivers;

public sealed class AuditTrailUserEventDisplayDriver : AuditTrailEventSectionDisplayDriver<AuditTrailUserEvent>
{
public override Task<IDisplayResult> DisplayAsync(AuditTrailEvent auditTrailEvent, AuditTrailUserEvent userEvent, BuildDisplayContext context)
public override IDisplayResult Display(AuditTrailEvent auditTrailEvent, AuditTrailUserEvent userEvent, BuildDisplayContext context)
{
return Task.FromResult<IDisplayResult>(
Initialize<AuditTrailUserEventDetailViewModel>("AuditTrailUserEventDetail_DetailAdmin", m => BuildViewModel(m, auditTrailEvent, userEvent))
.Location("DetailAdmin", "Content:10")
);
}

private static void BuildViewModel(AuditTrailUserEventViewModel m, AuditTrailEvent auditTrailEvent, AuditTrailUserEvent userEvent)
{
m.AuditTrailEvent = auditTrailEvent;
m.Name = userEvent.Name;
m.UserId = userEvent.UserId;
m.UserName = userEvent.UserName;
return Initialize<AuditTrailUserEventViewModel>("AuditTrailUserEventDetail_DetailAdmin", m =>
{
m.AuditTrailEvent = auditTrailEvent;
m.UserEvent = userEvent;
}).Location("DetailAdmin", "Content:10");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class UserEventHandler : UserEventHandlerBase, ILoginFormEvent
private readonly IAuditTrailManager _auditTrailManager;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IServiceProvider _serviceProvider;

private UserManager<IUser> _userManager;

public UserEventHandler(
Expand Down Expand Up @@ -89,18 +90,20 @@ private async Task RecordAuditTrailEventAsync(string name, IUser user, string us
userNameActual = userName;
}

var userEvent = new AuditTrailUserEvent
{
UserName = userName,
UserId = userId,
};

var context = new AuditTrailContext<AuditTrailUserEvent>
(
name: name,
category: UserAuditTrailEventConfiguration.User,
correlationId: userId,
userId: userIdActual,
userName: userNameActual,
new AuditTrailUserEvent
{
UserId = userId,
UserName = userName
}
userEvent
);

await _auditTrailManager.RecordEventAsync(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace OrchardCore.Users.AuditTrail.Models;

// TODO a future version should also record the User state, enabling diff against users
public class AuditTrailUserEvent
{
// TODO a future version should also record the User state, enabling diff against users
public string Name { get; set; } = "User";

public string UserName { get; set; }

public string UserId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ private async Task RecordAuditTrailEventAsync(string name, IUser user)

var userId = await _userManager.GetUserIdAsync(user);

var userEvent = new AuditTrailUserEvent
{
UserName = userName,
UserId = userId,
};

await _auditTrailManager.RecordEventAsync(
new AuditTrailContext<AuditTrailUserEvent>
(
Expand All @@ -36,11 +42,7 @@ await _auditTrailManager.RecordEventAsync(
userId,
userId,
userName,
new AuditTrailUserEvent
{
UserId = userId,
UserName = userName
}
userEvent
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OrchardCore.AuditTrail.Services.Models;
using OrchardCore.Users.AuditTrail.Models;
using OrchardCore.Users.Events;
using OrchardCore.Users.Models;

namespace OrchardCore.Users.AuditTrail.ResetPassword;

Expand Down Expand Up @@ -37,23 +38,32 @@ public Task PasswordResetAsync(PasswordRecoveryContext context)
private async Task RecordAuditTrailEventAsync(string name, IUser user)
{
var userName = user.UserName;
_userManager ??= _serviceProvider.GetRequiredService<UserManager<IUser>>();

var userId = await _userManager.GetUserIdAsync(user);
var userEvent = new AuditTrailUserEvent
{
UserName = userName,
};

if (user is User u)
{
userEvent.UserId = u.UserId;
}

if (string.IsNullOrEmpty(userEvent.UserId))
{
_userManager ??= _serviceProvider.GetRequiredService<UserManager<IUser>>();
userEvent.UserId = await _userManager.GetUserIdAsync(user);
}

await _auditTrailManager.RecordEventAsync(
new AuditTrailContext<AuditTrailUserEvent>
(
name,
UserResetPasswordAuditTrailEventConfiguration.User,
userId,
userId,
userEvent.UserId,
userEvent.UserId,
userName,
new AuditTrailUserEvent
{
UserId = userId,
UserName = userName
}
userEvent
));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ namespace OrchardCore.Users.AuditTrail.ViewModels;

public class AuditTrailUserEventViewModel
{
public string Name { get; set; }
public string UserName { get; set; }
public string UserId { get; set; }

[BindNever]
public AuditTrailUserEvent UserEvent { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
@model AuditTrailUserEventDetailViewModel
@model AuditTrailUserEventViewModel

<table class="table table-sm table-hover mb-0">
<h2 class="h3">@T["User Info"]</h2>

<table class="table table-sm table-hover">
<tbody>
<tr>
<th class="border-top-0 w-25">@T["User Id"]</th>
<td class="border-top-0">@Model.UserId</td>
<th class="w-25">@T["User ID"]</th>
<td>@Model.UserEvent.UserId</td>
</tr>
<tr>
<th>@T["User Name"]</th>
<td>@Model.UserName</td>
<th>@T["Username"]</th>
<td>@Model.UserEvent.UserName</td>
</tr>
</tbody>
</table>

0 comments on commit a7a6675

Please # to comment.