72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using _0_Framework.Application;
|
|
using GozareshgirProgramManager.Application._Common.Constants;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using GozareshgirProgramManager.Domain.ProjectAgg.Repositories;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System;
|
|
|
|
namespace ServiceHost.Hubs.ProgramManager;
|
|
|
|
public class ProjectBoardHub : Hub
|
|
{
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly ITaskSectionRepository _taskSectionRepository;
|
|
|
|
public ProjectBoardHub(IAuthHelper authHelper, ITaskSectionRepository taskSectionRepository)
|
|
{
|
|
_authHelper = authHelper;
|
|
_taskSectionRepository = taskSectionRepository;
|
|
}
|
|
|
|
public override async Task OnConnectedAsync()
|
|
{
|
|
// Rule 4: Determine all group memberships server-side.
|
|
if (!_authHelper.IsAuthenticated())
|
|
{
|
|
await base.OnConnectedAsync();
|
|
return;
|
|
}
|
|
|
|
var connectionId = Context.ConnectionId;
|
|
|
|
// Rule 2: Add to all permission-based groups based on user's claims.
|
|
var permissionGroups = _authHelper.GetPermissions() ?? new List<int>();
|
|
|
|
// Rule 3: Add to task-specific groups for all tasks assigned to the user (by accountId claim).
|
|
var userId =Convert.ToInt32(Context.User?.FindFirst("pm.userId")?.Value);
|
|
|
|
var taskGroups = new List<string>();
|
|
if (userId > 0)
|
|
{
|
|
var assignedTasks = await _taskSectionRepository.GetAssignedToUserAsync(userId);
|
|
if (assignedTasks is { Count: > 0 })
|
|
{
|
|
taskGroups = assignedTasks
|
|
.Select(t => $"pm.task:{t.Id}")
|
|
.ToList();
|
|
}
|
|
}
|
|
|
|
// Build the full, de-duplicated set of groups to join.
|
|
var groupsToJoin = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
// Permission-based groups
|
|
foreach (var perm in permissionGroups)
|
|
groupsToJoin.Add($"pm.perm:{perm}");
|
|
|
|
// Task-based groups
|
|
foreach (var tg in taskGroups)
|
|
groupsToJoin.Add(tg);
|
|
|
|
// Rule 5: Avoid duplicate joins; join all needed groups concurrently.
|
|
if (groupsToJoin.Count > 0)
|
|
{
|
|
var joinTasks = groupsToJoin
|
|
.Select(group => Groups.AddToGroupAsync(connectionId, group));
|
|
await Task.WhenAll(joinTasks);
|
|
}
|
|
|
|
await base.OnConnectedAsync();
|
|
}
|
|
} |