diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/LockHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/LockHandler.cs new file mode 100644 index 0000000..af90e01 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/LockHandler.cs @@ -0,0 +1,47 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.Application.Services.Abstractions; +using NetworkResurrector.Agent.PublishedLanguage.Commands; +using NetworkResurrector.Agent.PublishedLanguage.Events; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Agent.Application.CommandHandlers +{ + public class LockHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IPowerService _powerService; + private readonly IValidationService _validationService; + + public LockHandler(ILogger logger, IPowerService powerService, IValidationService validationService) + { + _logger=logger; + _powerService=powerService; + _validationService=validationService; + } + + public async Task Handle(Lock command, CancellationToken cancellationToken) + { + return await Task.Run(() => Handle(command)); + } + + private LockResult Handle(Lock command) + { + _validationService.ValidateRestrictions(command.Owner); + + _logger.LogDebug($"Start locking the user."); + + var stopWatch = new Stopwatch(); + stopWatch.Start(); + + _powerService.Lock(); + + stopWatch.Stop(); + _logger.LogDebug($"User lock finished - {stopWatch.ElapsedMilliseconds:N0} ms"); + + return new LockResult() { Success = true }; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs index ab55980..9a50a46 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs @@ -55,7 +55,7 @@ namespace NetworkResurrector.Agent.Application.Services.Windows msg.Append($"Errors:\n {commandError}\n"); msg.Append($"Process ended at: {DateTime.Now}"); - return (commandError.Length > 0, msg.ToString()); + return (commandError.Length == 0, msg.ToString()); } } } diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Lock.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Lock.cs new file mode 100644 index 0000000..de4d5d3 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Lock.cs @@ -0,0 +1,12 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.PublishedLanguage.Events; + +namespace NetworkResurrector.Agent.PublishedLanguage.Commands +{ + public class Lock : Command + { + public ActionOwner Owner { get; set; } + public ActionOptions Options { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/LockResult.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/LockResult.cs new file mode 100644 index 0000000..96e94b0 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/LockResult.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Agent.PublishedLanguage.Dto; + +namespace NetworkResurrector.Agent.PublishedLanguage.Events +{ + public class LockResult + { + public bool Success { get; set; } + public HostInfo Host { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs index 387a31a..6a3f291 100644 --- a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs +++ b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs @@ -45,5 +45,12 @@ namespace NetworkResurrector.Agent.Controllers var result = await _mediator.Send(logout); return Ok(result); } + + [HttpPost("lock")] + public async Task Lock([FromBody] Lock _lock) + { + var result = await _mediator.Send(_lock); + return Ok(result); + } } }