win logout
parent
8e665d2d73
commit
bfc1e863ad
|
@ -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 LogoutHandler : IRequestHandler<Logout, LogoutResult>
|
||||
{
|
||||
private readonly ILogger<LogoutHandler> _logger;
|
||||
private readonly IPowerService _powerService;
|
||||
private readonly IValidationService _validationService;
|
||||
|
||||
public LogoutHandler(ILogger<LogoutHandler> logger, IPowerService powerService, IValidationService validationService)
|
||||
{
|
||||
_logger=logger;
|
||||
_powerService=powerService;
|
||||
_validationService=validationService;
|
||||
}
|
||||
|
||||
public async Task<LogoutResult> Handle(Logout command, CancellationToken cancellationToken)
|
||||
{
|
||||
return await Task.Run(() => Handle(command));
|
||||
}
|
||||
|
||||
private LogoutResult Handle(Logout command)
|
||||
{
|
||||
_validationService.ValidateRestrictions(command.Owner);
|
||||
|
||||
_logger.LogDebug($"Start logout the user.");
|
||||
|
||||
var stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
|
||||
_powerService.Logout();
|
||||
|
||||
stopWatch.Stop();
|
||||
_logger.LogDebug($"User logout finished - {stopWatch.ElapsedMilliseconds:N0} ms");
|
||||
|
||||
return new LogoutResult() { Success = true };
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
|
||||
<PackageReference Include="System.Security.Principal.Windows" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -3,6 +3,7 @@ using NetworkResurrector.Agent.Application.Services.Abstractions;
|
|||
using NetworkResurrector.Agent.Domain.Models;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||
{
|
||||
|
@ -42,6 +43,27 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
|||
return arguments;
|
||||
}
|
||||
|
||||
private bool IsSystemUser()
|
||||
{
|
||||
bool isSystem;
|
||||
#pragma warning disable CA1416 // Validate platform compatibility
|
||||
using (var identity = WindowsIdentity.GetCurrent())
|
||||
{
|
||||
isSystem = identity.IsSystem;
|
||||
}
|
||||
#pragma warning restore CA1416 // Validate platform compatibility
|
||||
|
||||
return isSystem;
|
||||
}
|
||||
|
||||
private void ValidateLocalOrDomainUser()
|
||||
{
|
||||
var isSystem = IsSystemUser();
|
||||
if (isSystem)
|
||||
throw new Exception("This action is not available for system users.");
|
||||
}
|
||||
|
||||
|
||||
public void Shutdown(PowerOptions options)
|
||||
{
|
||||
var arguments = "-s";
|
||||
|
@ -69,11 +91,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
|||
|
||||
public void Logout()
|
||||
{
|
||||
ValidateLocalOrDomainUser();
|
||||
StartShutdownProcess("-l");
|
||||
}
|
||||
|
||||
public void Lock()
|
||||
{
|
||||
ValidateLocalOrDomainUser();
|
||||
var command = "rundll32.exe user32.dll,LockWorkStation";
|
||||
var (success, message) = _cliService.Execute(command);
|
||||
var msg = $"Command: {command} | ExecutionLog: {message}";
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
{
|
||||
public class ConfigurationRecords
|
||||
{
|
||||
public record Restrictions(bool EnforceActionOwner);
|
||||
public record Restrictions
|
||||
{
|
||||
public bool EnforceActionOwner { get; init; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
using NDB.Application.DataContracts;
|
||||
using NetworkResurrector.Agent.PublishedLanguage.Dto;
|
||||
using NetworkResurrector.Agent.PublishedLanguage.Events;
|
||||
|
||||
namespace NetworkResurrector.Agent.PublishedLanguage.Commands
|
||||
{
|
||||
public class Logout : Command<LogoutResult>
|
||||
{
|
||||
public ActionOwner Owner { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using NetworkResurrector.Agent.PublishedLanguage.Dto;
|
||||
|
||||
namespace NetworkResurrector.Agent.PublishedLanguage.Events
|
||||
{
|
||||
public class LogoutResult
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public HostInfo Host { get; set; }
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
|||
namespace NetworkResurrector.Agent.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
// [Authorize]
|
||||
[Route("host")]
|
||||
public class HostController : ControllerBase
|
||||
{
|
||||
|
@ -38,5 +38,12 @@ namespace NetworkResurrector.Agent.Controllers
|
|||
var result = await _mediator.Send(sleep);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("logout")]
|
||||
public async Task<IActionResult> Logout([FromBody] Logout logout)
|
||||
{
|
||||
var result = await _mediator.Send(logout);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue