2using System.Collections.Generic;
5using System.Threading.Tasks;
7using Microsoft.AspNetCore.SignalR;
8using Microsoft.EntityFrameworkCore;
9using Microsoft.Extensions.Hosting;
10using Microsoft.Extensions.Logging;
43 readonly ILogger<JobsHubGroupMapper>
logger;
56 ILogger<JobsHubGroupMapper>
logger)
58 this.hub =
hub ??
throw new ArgumentNullException(nameof(
hub));
61 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
69 ArgumentNullException.ThrowIfNull(instancePermissionSet);
72 logger.LogTrace(
"InstancePermissionSetCreated");
81 ArgumentNullException.ThrowIfNull(user);
82 if (!user.
Id.HasValue)
83 throw new InvalidOperationException(
"user.Id was null!");
85 logger.LogTrace(
"UserDisabled");
86 hub.AbortUnauthedConnections(user);
87 return ValueTask.CompletedTask;
93 ArgumentNullException.ThrowIfNull(permissionSet);
94 logger.LogTrace(
"InstancePermissionSetDeleted");
96 permissionSet.
Id ??
throw new InvalidOperationException(
"permissionSet?.Id was null!"),
101 public Task
StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
104 public Task
StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
115 Func<IEnumerable<string>, Task> mappingFunc,
116 CancellationToken cancellationToken)
118 ArgumentNullException.ThrowIfNull(authenticationContext);
120 var pid = authenticationContext.
PermissionSet.Require(x => x.Id);
122 "MapConnectionGroups UID: {uid} PID: {pid}",
123 authenticationContext.
User.Require(x => x.Id),
126 List<long>? permedInstanceIds =
null;
128 async databaseContext =>
129 permedInstanceIds = await databaseContext
130 .InstancePermissionSets
132 .Where(ips => ips.PermissionSetId == pid)
133 .Select(ips => ips.InstanceId)
134 .ToListAsync(cancellationToken));
137 permedInstanceIds!.Select(
151 async databaseContext =>
153 logger.LogTrace(
"RefreshHubGroups");
154 var permissionSetUsers = await databaseContext
156 .Where(x => x.PermissionSet!.Id == permissionSetId)
157 .ToListAsync(cancellationToken);
158 var allInstanceIds = await databaseContext
161 instance => instance.Id!.Value)
162 .ToListAsync(cancellationToken);
163 var permissionSetAccessibleInstanceIds = await databaseContext
164 .InstancePermissionSets
166 .Where(ips => ips.PermissionSetId == permissionSetId)
167 .Select(ips => ips.InstanceId)
168 .ToListAsync(cancellationToken);
170 var groupsToRemove = allInstanceIds
171 .Except(permissionSetAccessibleInstanceIds)
174 var groupsToAdd = permissionSetAccessibleInstanceIds
177 var connectionIds = permissionSetUsers
178 .SelectMany(user =>
hub.UserConnectionIds(user))
182 "Updating groups for the {connectionCount} hub connections of permission set {permissionSetId}. They may access {allowed}/{total} instances.",
185 permissionSetAccessibleInstanceIds.Count,
186 allInstanceIds.Count);
188 var removeTasks = connectionIds
189 .SelectMany(connectionId => groupsToRemove
190 .Select(groupName =>
hub
192 .RemoveFromGroupAsync(connectionId, groupName, cancellationToken)));
194 var addTasks = connectionIds
195 .SelectMany(connectionId => groupsToAdd
196 .Select(groupName =>
hub
198 .AddToGroupAsync(connectionId, groupName, cancellationToken)));
202 await Task.WhenAll(removeTasks.Concat(addTasks));
virtual ? long Id
The ID of the entity.
long PermissionSetId
The EntityId.Id of the PermissionSet the InstancePermissionSet belongs to.
Handles mapping groups for the JobsHub.
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the JobService.
JobsHubGroupMapper(IConnectionMappedHubContext< JobsHub, IJobsHub > hub, IDatabaseContextFactory databaseContextFactory, IJobsHubUpdater jobsHubUpdater, ILogger< JobsHubGroupMapper > logger)
Initializes a new instance of the JobsHubGroupMapper class.
ValueTask UserDisabled(User user, CancellationToken cancellationToken)
Called when a given User is successfully disabled.A ValueTask representing the running operation.
readonly ILogger< JobsHubGroupMapper > logger
The ILogger for the JobService.
ValueTask InstancePermissionSetCreated(InstancePermissionSet instancePermissionSet, CancellationToken cancellationToken)
Called when a given instancePermissionSet is successfully created.A ValueTask representing the runni...
ValueTask InstancePermissionSetDeleted(PermissionSet permissionSet, CancellationToken cancellationToken)
Called when an InstancePermissionSet is successfully deleted.A ValueTask representing the running ope...
async ValueTask MapConnectionGroups(IAuthenticationContext authenticationContext, Func< IEnumerable< string >, Task > mappingFunc, CancellationToken cancellationToken)
Implementation of IConnectionMappedHubContext<THub, THubMethods>.OnConnectionMapGroups.
Task StopAsync(CancellationToken cancellationToken)
readonly IJobsHubUpdater jobsHubUpdater
The IJobsHubUpdater for the JobService.
readonly IConnectionMappedHubContext< JobsHub, IJobsHub > hub
The IHubContext for the JobsHub.
ValueTask RefreshHubGroups(long permissionSetId, CancellationToken cancellationToken)
Refresh the hub Hub.Groups for clients associated with a given permissionSetId .
Task StartAsync(CancellationToken cancellationToken)
A SignalR Hub for pushing job updates.
static string HubGroupName(long instanceId)
Get the group name for a given instanceId .
Factory for scoping usage of IDatabaseContexts. Meant for use by Components.
ValueTask UseContext(Func< IDatabaseContext, ValueTask > operation)
Run an operation in the scope of an IDatabaseContext.
Allows manually triggering jobs hub updates.
void QueueActiveJobUpdates()
Queue a message to be sent to all clients with the current state of active jobs.
For creating and accessing authentication contexts.
PermissionSet PermissionSet
The User's effective PermissionSet.
User User
The authenticated user.
Receives notifications about permissions updates.
A IHubContext<THub> that maps Users to their connection IDs.