From e08972292cae7ef3550bd6244a2c0c4756a2ce1a Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 7 May 2023 00:59:52 +0300 Subject: [PATCH] 1.2.7 - cache reset support --- Directory.Build.props | 2 +- ReleaseNotes.xml | 10 +++++++ .../System/ResetCacheHandler.cs | 28 +++++++++++++++++++ .../Queries/GetSystemVersion.cs | 23 +++++++++++++-- .../NetworkResurrector.Api.Domain.Data.csproj | 3 ++ ...1.Add system administration permission.sql | 5 ++++ .../Constants/PermissionCodes.cs | 3 +- .../Commands/SystemCommands.cs | 7 +++++ .../Dto/GenericRecords.cs | 4 +++ .../Authorization/Constants/Policies.cs | 3 +- .../DependencyInjectionExtensions.cs | 4 +++ .../Requirements/SystemRequirements.cs | 10 +++++++ .../Controllers/SystemController.cs | 10 +++++++ 13 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Application/CommandHandlers/System/ResetCacheHandler.cs create mode 100644 src/api/NetworkResurrector.Api.Domain.Data/Scripts/1.2.6/01.Add system administration permission.sql create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SystemCommands.cs create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Dto/GenericRecords.cs create mode 100644 src/api/NetworkResurrector.Api/Authorization/Requirements/SystemRequirements.cs diff --git a/Directory.Build.props b/Directory.Build.props index 32df56e..7fdac9c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.2.5 + 1.2.7 Tudor Stanciu STA NetworkResurrector diff --git a/ReleaseNotes.xml b/ReleaseNotes.xml index c8c39f7..06b761d 100644 --- a/ReleaseNotes.xml +++ b/ReleaseNotes.xml @@ -188,6 +188,16 @@ General improvements • Added environment variables support for frontend. + • From now on, there is no hardcoded URL in the source code of the application. + + + + 1.2.7 + 2023-05-07 00:52 + + Cache reset support + • A new permission called "System administration" has been added. + • The cache can now be reset from UI by a user who has "System administration" permission. \ No newline at end of file diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/System/ResetCacheHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/System/ResetCacheHandler.cs new file mode 100644 index 0000000..688807a --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/System/ResetCacheHandler.cs @@ -0,0 +1,28 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using Netmash.Extensions.Caching.Services; +using NetworkResurrector.Api.PublishedLanguage.Commands; +using NetworkResurrector.Api.PublishedLanguage.Dto; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + internal class ResetCacheHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly ICacheService _cache; + + public ResetCacheHandler(ILogger logger, ICacheService cache) + { + _logger=logger; + _cache=cache; + } + + public async Task Handle(ResetCache request, CancellationToken cancellationToken) + { + _cache.Reset(); + return await Task.FromResult(new CommandResult()); + } + } +} diff --git a/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs b/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs index f60fe0c..1bf9ed9 100644 --- a/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs +++ b/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs @@ -1,4 +1,5 @@ using MediatR; +using Microsoft.Extensions.Logging; using NetworkResurrector.Server.Wrapper.Services; using System; using System.IO; @@ -23,21 +24,23 @@ namespace NetworkResurrector.Api.Application.Queries public class QueryHandler : IRequestHandler { private readonly IResurrectorService _resurrectorService; + private readonly ILogger _logger; - public QueryHandler(IResurrectorService resurrectorService) + public QueryHandler(IResurrectorService resurrectorService, ILogger logger) { _resurrectorService=resurrectorService; + _logger=logger; } public async Task Handle(Query request, CancellationToken cancellationToken) { - var serverVersion = await _resurrectorService.GetServiceVersion(); var apiVersion = GetApiVersion(); + var serverVersion = await GetServerVersion(); var result = new Model { Api = apiVersion, - Server = new ServiceVersion(serverVersion.Version, serverVersion.LastReleaseDate) + Server = serverVersion }; return result; @@ -60,6 +63,20 @@ namespace NetworkResurrector.Api.Application.Queries var result = new ServiceVersion(version, lastReleaseDate); return result; } + + private async Task GetServerVersion() + { + try + { + var serverVersion = await _resurrectorService.GetServiceVersion(); + return new ServiceVersion(serverVersion.Version, serverVersion.LastReleaseDate); + } + catch (Exception ex) + { + _logger.LogError(ex, "Server version could not be obtained."); + } + return new ServiceVersion("0.0.0", DateTime.MinValue); + } } } } diff --git a/src/api/NetworkResurrector.Api.Domain.Data/NetworkResurrector.Api.Domain.Data.csproj b/src/api/NetworkResurrector.Api.Domain.Data/NetworkResurrector.Api.Domain.Data.csproj index 86dec0b..36278ec 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/NetworkResurrector.Api.Domain.Data.csproj +++ b/src/api/NetworkResurrector.Api.Domain.Data/NetworkResurrector.Api.Domain.Data.csproj @@ -28,6 +28,9 @@ PreserveNewest + + PreserveNewest + diff --git a/src/api/NetworkResurrector.Api.Domain.Data/Scripts/1.2.6/01.Add system administration permission.sql b/src/api/NetworkResurrector.Api.Domain.Data/Scripts/1.2.6/01.Add system administration permission.sql new file mode 100644 index 0000000..e5be6a7 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain.Data/Scripts/1.2.6/01.Add system administration permission.sql @@ -0,0 +1,5 @@ +if not exists (select top 1 1 from Permission where PermissionCode = 'SYSTEM_ADMINISTRATION') +begin + insert into Permission(PermissionCode, PermissionName, PermissionDescription) + values ('SYSTEM_ADMINISTRATION', 'System administration', 'The user with this permission can perform system administration operations such as resetting the cache.') +end \ No newline at end of file diff --git a/src/api/NetworkResurrector.Api.Domain/Constants/PermissionCodes.cs b/src/api/NetworkResurrector.Api.Domain/Constants/PermissionCodes.cs index c1a46b6..c91a532 100644 --- a/src/api/NetworkResurrector.Api.Domain/Constants/PermissionCodes.cs +++ b/src/api/NetworkResurrector.Api.Domain/Constants/PermissionCodes.cs @@ -9,6 +9,7 @@ VIEW_MACHINES = "VIEW_MACHINES", MANAGE_MACHINES = "MANAGE_MACHINES", OPERATE_MACHINES = "OPERATE_MACHINES", - GUEST_ACCESS = "GUEST_ACCESS"; + GUEST_ACCESS = "GUEST_ACCESS", + SYSTEM_ADMINISTRATION = "SYSTEM_ADMINISTRATION"; } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SystemCommands.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SystemCommands.cs new file mode 100644 index 0000000..58a271d --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/SystemCommands.cs @@ -0,0 +1,7 @@ +using MediatR; +using NetworkResurrector.Api.PublishedLanguage.Dto; + +namespace NetworkResurrector.Api.PublishedLanguage.Commands +{ + public record ResetCache : IRequest { } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Dto/GenericRecords.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Dto/GenericRecords.cs new file mode 100644 index 0000000..d87ec53 --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Dto/GenericRecords.cs @@ -0,0 +1,4 @@ +namespace NetworkResurrector.Api.PublishedLanguage.Dto +{ + public record CommandResult(); +} diff --git a/src/api/NetworkResurrector.Api/Authorization/Constants/Policies.cs b/src/api/NetworkResurrector.Api/Authorization/Constants/Policies.cs index d571638..6ee5561 100644 --- a/src/api/NetworkResurrector.Api/Authorization/Constants/Policies.cs +++ b/src/api/NetworkResurrector.Api/Authorization/Constants/Policies.cs @@ -4,6 +4,7 @@ { public const string OperateMachines = "OPERATE_MACHINES", - ViewMachines = "VIEW_MACHINES"; + ViewMachines = "VIEW_MACHINES", + SystemAdministration = "SYSTEM_ADMINISTRATION"; } } diff --git a/src/api/NetworkResurrector.Api/Authorization/DependencyInjectionExtensions.cs b/src/api/NetworkResurrector.Api/Authorization/DependencyInjectionExtensions.cs index e907de9..1cf7d96 100644 --- a/src/api/NetworkResurrector.Api/Authorization/DependencyInjectionExtensions.cs +++ b/src/api/NetworkResurrector.Api/Authorization/DependencyInjectionExtensions.cs @@ -20,6 +20,10 @@ namespace NetworkResurrector.Api.Authorization { policy.Requirements.Add(new ViewMachinesRequirement()); }); + options.AddPolicy(Policies.SystemAdministration, policy => + { + policy.Requirements.Add(new SystemAdministrationRequirement()); + }); }); services.AddScoped(); diff --git a/src/api/NetworkResurrector.Api/Authorization/Requirements/SystemRequirements.cs b/src/api/NetworkResurrector.Api/Authorization/Requirements/SystemRequirements.cs new file mode 100644 index 0000000..f8b4c5f --- /dev/null +++ b/src/api/NetworkResurrector.Api/Authorization/Requirements/SystemRequirements.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Api.Domain.Constants; + +namespace NetworkResurrector.Api.Authorization.Requirements +{ + public class SystemAdministrationRequirement : IPermissionsBasedAuthorizationRequirement + { + public string[] AllRequired => new string[] { PermissionCodes.SYSTEM_ADMINISTRATION }; + public string[] OneOf => null; + } +} diff --git a/src/api/NetworkResurrector.Api/Controllers/SystemController.cs b/src/api/NetworkResurrector.Api/Controllers/SystemController.cs index 32734ee..2964d54 100644 --- a/src/api/NetworkResurrector.Api/Controllers/SystemController.cs +++ b/src/api/NetworkResurrector.Api/Controllers/SystemController.cs @@ -2,6 +2,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using NetworkResurrector.Api.Application.Queries; +using NetworkResurrector.Api.Authorization.Constants; +using NetworkResurrector.Api.PublishedLanguage.Commands; using System; using System.Threading.Tasks; @@ -39,5 +41,13 @@ namespace NetworkResurrector.Api.Controllers var result = await _mediator.Send(query); return Ok(result); } + + [HttpPost("reset-cache")] + [Authorize(Policy = Policies.SystemAdministration)] + public async Task WakeMachine([FromBody] ResetCache resetCache) + { + var result = await _mediator.Send(resetCache); + return Ok(result); + } } }