UserService
parent
7be1fd6a69
commit
68f6cc5332
|
@ -1,5 +1,6 @@
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using NetworkResurrector.Api.Domain.Abstractions;
|
||||||
using NetworkResurrector.Api.Domain.Constants;
|
using NetworkResurrector.Api.Domain.Constants;
|
||||||
using NetworkResurrector.Api.Domain.Repositories;
|
using NetworkResurrector.Api.Domain.Repositories;
|
||||||
using NetworkResurrector.Api.PublishedLanguage.Commands;
|
using NetworkResurrector.Api.PublishedLanguage.Commands;
|
||||||
|
@ -16,16 +17,20 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
|
||||||
private readonly ILogger<PingMachineHandler> _logger;
|
private readonly ILogger<PingMachineHandler> _logger;
|
||||||
private readonly IResurrectorService _resurrectorService;
|
private readonly IResurrectorService _resurrectorService;
|
||||||
private readonly INetworkRepository _repository;
|
private readonly INetworkRepository _repository;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
|
||||||
public PingMachineHandler(ILogger<PingMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository)
|
public PingMachineHandler(ILogger<PingMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository, IUserService userService)
|
||||||
{
|
{
|
||||||
_logger=logger;
|
_logger=logger;
|
||||||
_resurrectorService=resurrectorService;
|
_resurrectorService=resurrectorService;
|
||||||
_repository=repository;
|
_repository=repository;
|
||||||
|
_userService=userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MachinePinged> Handle(PingMachine command, CancellationToken cancellationToken)
|
public async Task<MachinePinged> Handle(PingMachine command, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
var userId = _userService.GetUserId();
|
||||||
|
|
||||||
_logger.LogDebug($"Start pinging machine {command.MachineId}");
|
_logger.LogDebug($"Start pinging machine {command.MachineId}");
|
||||||
var machine = await _repository.GetMachine(command.MachineId);
|
var machine = await _repository.GetMachine(command.MachineId);
|
||||||
var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.PING);
|
var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.PING);
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace NetworkResurrector.Api.Domain.Abstractions
|
||||||
|
{
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
bool UserIsLoggedIn { get; }
|
||||||
|
string GetUserId();
|
||||||
|
string GetUserName();
|
||||||
|
bool UserIsGuest();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using NetworkResurrector.Api.Domain.Abstractions;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace NetworkResurrector.Api.Services
|
||||||
|
{
|
||||||
|
public class UserService : IUserService
|
||||||
|
{
|
||||||
|
private readonly IHttpContextAccessor _httpAccessor;
|
||||||
|
|
||||||
|
public UserService(IHttpContextAccessor httpAccessor)
|
||||||
|
{
|
||||||
|
_httpAccessor = httpAccessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool UserIsLoggedIn => _httpAccessor.HttpContext.User != null;
|
||||||
|
|
||||||
|
public string GetUserId()
|
||||||
|
{
|
||||||
|
var userId = _httpAccessor.HttpContext.User?.Claims.FirstOrDefault(z => z.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(userId))
|
||||||
|
throw new Exception("User id could not be retrieved from claims.");
|
||||||
|
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetUserName()
|
||||||
|
{
|
||||||
|
var userName = _httpAccessor.HttpContext.User?.Claims.FirstOrDefault(z => z.Type == ClaimTypes.Name)?.Value;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(userName))
|
||||||
|
throw new Exception("User name could not be retrieved from claims.");
|
||||||
|
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool UserIsGuest()
|
||||||
|
{
|
||||||
|
var userIsGuest = _httpAccessor.HttpContext.User?.Claims.FirstOrDefault(z => z.Type == NDB.Security.Authentication.Identity.Constants.ClaimTypes.IsGuestUser)?.Value;
|
||||||
|
return !string.IsNullOrEmpty(userIsGuest) && bool.TrueString == userIsGuest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,9 @@ using NDB.Extensions.Swagger.Constants;
|
||||||
using NDB.Security.Authentication.Identity;
|
using NDB.Security.Authentication.Identity;
|
||||||
using NetworkResurrector.Agent.Wrapper;
|
using NetworkResurrector.Agent.Wrapper;
|
||||||
using NetworkResurrector.Api.Application;
|
using NetworkResurrector.Api.Application;
|
||||||
|
using NetworkResurrector.Api.Domain.Abstractions;
|
||||||
using NetworkResurrector.Api.Domain.Data;
|
using NetworkResurrector.Api.Domain.Data;
|
||||||
|
using NetworkResurrector.Api.Services;
|
||||||
using NetworkResurrector.Server.Wrapper;
|
using NetworkResurrector.Server.Wrapper;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
@ -36,6 +38,9 @@ namespace NetworkResurrector.Api
|
||||||
// Add basic authentication
|
// Add basic authentication
|
||||||
services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]);
|
services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]);
|
||||||
|
|
||||||
|
services.AddHttpContextAccessor();
|
||||||
|
services.AddScoped<IUserService, UserService>();
|
||||||
|
|
||||||
// MediatR
|
// MediatR
|
||||||
services.AddMediatR(GetMediatRAssemblies());
|
services.AddMediatR(GetMediatRAssemblies());
|
||||||
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
|
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
|
||||||
|
|
Loading…
Reference in New Issue