diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/LogoutUserHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/LogoutUserHandler.cs new file mode 100644 index 0000000..2a02fbc --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/LogoutUserHandler.cs @@ -0,0 +1,63 @@ +using MediatR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.Wrapper.Services; +using NetworkResurrector.Api.Domain.Constants; +using NetworkResurrector.Api.Domain.Repositories; +using NetworkResurrector.Api.PublishedLanguage.Commands; +using NetworkResurrector.Api.PublishedLanguage.Events; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + public class LogoutUserHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly INetworkRepository _repository; + private readonly IResurrectorAgentService _resurrectorAgentService; + private readonly IConfiguration _configuration; + + public LogoutUserHandler(ILogger logger, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) + { + _logger=logger; + _repository=repository; + _resurrectorAgentService=resurrectorAgentService; + _configuration=configuration; + } + + public async Task Handle(LogoutUser command, CancellationToken cancellationToken) + { + _logger.LogDebug($"Start logging out the user connected on machine {command.MachineId}"); + var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.LOGOUT); + + //log activity + + var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName; + UserLoggedOut result; + + switch (powerConfiguration.Performer.PerformerCode) + { + case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: + if (machine.Agent == null) + throw new Exception($"Cannot use network resurrector agent as logout performer for machine '{ipAddressOrMachineName}' because it is not configured."); + + var owner = new ActionOwner(_configuration.GetValue("Service:Code")); + var LogoutResultByAgent = await _resurrectorAgentService.Logout(ipAddressOrMachineName, machine.Agent.AgentPort, owner); + var status = LogoutResultByAgent.Success ? $"The user from machine '{ipAddressOrMachineName}' was successfully logged out." : $"The user from machine '{ipAddressOrMachineName}' could not be logged out."; + result = new UserLoggedOut(LogoutResultByAgent.Success, status); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } + + _logger.LogDebug($"Logging out the user from machine {command.MachineId} finished. Success: {result.Success}; Status: {result.Status}"); + + return result; + } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LogoutUser.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LogoutUser.cs new file mode 100644 index 0000000..6977989 --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LogoutUser.cs @@ -0,0 +1,10 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Api.PublishedLanguage.Events; + +namespace NetworkResurrector.Api.PublishedLanguage.Commands +{ + public class LogoutUser : Command + { + public int MachineId { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/UserLoggedOut.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/UserLoggedOut.cs new file mode 100644 index 0000000..0eac156 --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/UserLoggedOut.cs @@ -0,0 +1,4 @@ +namespace NetworkResurrector.Api.PublishedLanguage.Events +{ + public record UserLoggedOut(bool Success, string Status); +}