Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
refactor(back-end): Remove unnecessary handover from user role
Browse files Browse the repository at this point in the history
The user's role is already registered in the token that is generated for it, therefore it is not necessary to search for the user and his role when he connects
  • Loading branch information
CarlosPavajeau committed Mar 12, 2021
1 parent c11c92e commit 195efe7
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 40 deletions.
3 changes: 0 additions & 3 deletions Kaizen/Hubs/ActivityHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ namespace Kaizen.Hubs
{
public class ActivityHub : BaseHub
{
public ActivityHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}
}
}
3 changes: 0 additions & 3 deletions Kaizen/Hubs/ApplicationUserHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ namespace Kaizen.Hubs
[Authorize]
public class ApplicationUserHub : BaseHub
{
public ApplicationUserHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}
}
}
26 changes: 5 additions & 21 deletions Kaizen/Hubs/BaseHub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

Expand All @@ -9,33 +9,17 @@ namespace Kaizen.Hubs
[Authorize]
public abstract class BaseHub : Hub
{
private readonly IApplicationUserRepository _applicationUserRepository;

protected BaseHub(IApplicationUserRepository applicationUserRepository)
{
_applicationUserRepository = applicationUserRepository;
}

public override async Task OnConnectedAsync()
{
if (Context.User?.Identity != null)
{
string userName = Context.User.Identity.Name;

if (string.IsNullOrEmpty(userName))
{
return;
}

ApplicationUser user = await _applicationUserRepository.FindByNameAsync(userName);

if (user is null)
string userRole = Context.User.FindFirst(ClaimTypes.Role)?.Value;
if (string.IsNullOrEmpty(userRole))
{
return;
}

string role = await _applicationUserRepository.GetUserRoleAsync(user);
await Groups.AddToGroupAsync(Context.ConnectionId, role);
await Groups.AddToGroupAsync(Context.ConnectionId, userRole);
}

await base.OnConnectedAsync();
Expand Down
3 changes: 0 additions & 3 deletions Kaizen/Hubs/ClientHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ namespace Kaizen.Hubs
{
public class ClientHub : BaseHub
{
public ClientHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}
}
}
4 changes: 0 additions & 4 deletions Kaizen/Hubs/EmployeeLocationHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ namespace Kaizen.Hubs
{
public class EmployeeLocationHub : BaseHub
{
public EmployeeLocationHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}

public async Task UpdateEmployeeLocation(EmployeeLocation employeeLocation)
{
await Clients.Group("Administrator").SendAsync("OnUpdateEmployeeLocation", employeeLocation);
Expand Down
3 changes: 0 additions & 3 deletions Kaizen/Hubs/InvoiceHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ namespace Kaizen.Hubs
{
public class InvoiceHub : BaseHub
{
public InvoiceHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}
}
}
3 changes: 0 additions & 3 deletions Kaizen/Hubs/ServiceRequestHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ namespace Kaizen.Hubs
{
public class ServiceRequestHub : BaseHub
{
public ServiceRequestHub(IApplicationUserRepository applicationUserRepository) : base(applicationUserRepository)
{
}
}
}

0 comments on commit 195efe7

Please sign in to comment.