diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs index 4607308..2c8b7b4 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs @@ -1,5 +1,6 @@ using MediatR; using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Abstractions; using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; @@ -16,16 +17,20 @@ namespace NetworkResurrector.Api.Application.CommandHandlers private readonly ILogger _logger; private readonly IResurrectorService _resurrectorService; private readonly INetworkRepository _repository; + private readonly IUserService _userService; - public PingMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository) + public PingMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository, IUserService userService) { _logger=logger; _resurrectorService=resurrectorService; _repository=repository; + _userService=userService; } public async Task Handle(PingMachine command, CancellationToken cancellationToken) { + var userId = _userService.GetUserId(); + _logger.LogDebug($"Start pinging machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.PING); diff --git a/src/api/NetworkResurrector.Api.Domain/Abstractions/IUserService.cs b/src/api/NetworkResurrector.Api.Domain/Abstractions/IUserService.cs new file mode 100644 index 0000000..76e1518 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Abstractions/IUserService.cs @@ -0,0 +1,10 @@ +namespace NetworkResurrector.Api.Domain.Abstractions +{ + public interface IUserService + { + bool UserIsLoggedIn { get; } + string GetUserId(); + string GetUserName(); + bool UserIsGuest(); + } +} diff --git a/src/api/NetworkResurrector.Api/Services/UserService.cs b/src/api/NetworkResurrector.Api/Services/UserService.cs new file mode 100644 index 0000000..ae7ed1c --- /dev/null +++ b/src/api/NetworkResurrector.Api/Services/UserService.cs @@ -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; + } + } +} diff --git a/src/api/NetworkResurrector.Api/Startup.cs b/src/api/NetworkResurrector.Api/Startup.cs index 73e991b..c0b867f 100644 --- a/src/api/NetworkResurrector.Api/Startup.cs +++ b/src/api/NetworkResurrector.Api/Startup.cs @@ -11,7 +11,9 @@ using NDB.Extensions.Swagger.Constants; using NDB.Security.Authentication.Identity; using NetworkResurrector.Agent.Wrapper; using NetworkResurrector.Api.Application; +using NetworkResurrector.Api.Domain.Abstractions; using NetworkResurrector.Api.Domain.Data; +using NetworkResurrector.Api.Services; using NetworkResurrector.Server.Wrapper; using Newtonsoft.Json; using System.Reflection; @@ -36,6 +38,9 @@ namespace NetworkResurrector.Api // Add basic authentication services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]); + services.AddHttpContextAccessor(); + services.AddScoped(); + // MediatR services.AddMediatR(GetMediatRAssemblies()); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));