2026-05-23 13:57:27 +00:00
|
|
|
using Elsa.Diagnostics.ConsoleLogs.Permissions;
|
|
|
|
|
using FastEndpoints.Security;
|
2026-05-18 00:16:46 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Diagnostics.ConsoleLogs.RealTime;
|
|
|
|
|
|
2026-05-23 12:51:03 +00:00
|
|
|
[Authorize]
|
2026-05-18 00:16:46 +00:00
|
|
|
public class ConsoleLogsHub(ConsoleLogSubscriptionManager subscriptionManager) : Hub<IConsoleLogsClient>
|
|
|
|
|
{
|
2026-05-23 13:57:27 +00:00
|
|
|
private const string ReadAllPermission = "read:*";
|
|
|
|
|
private static readonly string[] ReadPermissions = [PermissionNames.All, ReadAllPermission, ConsoleLogsPermissions.Read];
|
|
|
|
|
|
2026-05-18 00:16:46 +00:00
|
|
|
public Task SubscribeAsync(ConsoleLogFilter? filter)
|
|
|
|
|
{
|
2026-05-23 13:57:27 +00:00
|
|
|
EnsureCanReadConsoleLogs();
|
2026-05-18 00:16:46 +00:00
|
|
|
return subscriptionManager.SubscribeAsync(Context.ConnectionId, ValidateFilter(filter), Context.ConnectionAborted);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 13:57:27 +00:00
|
|
|
public Task UpdateFilterAsync(ConsoleLogFilter? filter)
|
|
|
|
|
{
|
|
|
|
|
EnsureCanReadConsoleLogs();
|
|
|
|
|
return subscriptionManager.UpdateFilterAsync(Context.ConnectionId, ValidateFilter(filter), Context.ConnectionAborted);
|
|
|
|
|
}
|
2026-05-18 00:16:46 +00:00
|
|
|
|
|
|
|
|
public Task UnsubscribeAsync()
|
|
|
|
|
{
|
|
|
|
|
return subscriptionManager.UnsubscribeAsync(Context.ConnectionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
|
|
|
{
|
|
|
|
|
await UnsubscribeAsync();
|
|
|
|
|
await base.OnDisconnectedAsync(exception);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ConsoleLogFilter ValidateFilter(ConsoleLogFilter? filter)
|
|
|
|
|
{
|
|
|
|
|
filter ??= new();
|
|
|
|
|
|
|
|
|
|
if (filter.From is { } from && filter.To is { } to && from > to)
|
|
|
|
|
throw new HubException("The console log filter 'from' timestamp must be earlier than or equal to 'to'.");
|
|
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
|
}
|
2026-05-23 13:57:27 +00:00
|
|
|
|
|
|
|
|
private void EnsureCanReadConsoleLogs()
|
|
|
|
|
{
|
|
|
|
|
var user = Context.User;
|
|
|
|
|
|
|
|
|
|
if (user?.Identity?.IsAuthenticated != true || !ReadPermissions.Any(user.HasPermission))
|
|
|
|
|
throw new HubException("Access denied.");
|
|
|
|
|
}
|
2026-05-18 00:16:46 +00:00
|
|
|
}
|