diff --git a/LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs b/LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs index bae61e8e..5934c757 100644 --- a/LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs +++ b/LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs @@ -49,7 +49,7 @@ public async Task GetNavigationModelAsync(IPrincipal user, bool } else if (user.IsInRole("Administrator")) { - return this.AuthenticatedAdministrator(controllerName); + return await this.AuthenticatedAdministrator(controllerName); } else if (user.IsInRole("ReadOnly")) { @@ -100,7 +100,7 @@ public NavigationModel NotAuthenticated() /// /// The controller name. /// The . - private NavigationModel AuthenticatedAdministrator(string controllerName) + private async Task AuthenticatedAdministrator(string controllerName) { return new NavigationModel() { @@ -118,7 +118,7 @@ private NavigationModel AuthenticatedAdministrator(string controllerName) ShowSignOut = true, ShowMyAccount = true, ShowBrowseCatalogues = true, - ShowReports = true, + ShowReports = await this.reportService.GetReporterPermission(), }; } diff --git a/LearningHub.Nhs.WebUI/Services/ReportService.cs b/LearningHub.Nhs.WebUI/Services/ReportService.cs index de383e4d..788b56b6 100644 --- a/LearningHub.Nhs.WebUI/Services/ReportService.cs +++ b/LearningHub.Nhs.WebUI/Services/ReportService.cs @@ -1,16 +1,16 @@ namespace LearningHub.Nhs.WebUI.Services { using System; - using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; - using elfhHub.Nhs.Models.Common; + using LearningHub.Nhs.Caching; using LearningHub.Nhs.Models.Common; using LearningHub.Nhs.Models.Databricks; + using LearningHub.Nhs.Models.Extensions; using LearningHub.Nhs.Models.Paging; - using LearningHub.Nhs.Models.Validation; using LearningHub.Nhs.WebUI.Interfaces; + using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -19,15 +19,22 @@ /// public class ReportService : BaseService, IReportService { + private readonly ICacheService cacheService; + private readonly IHttpContextAccessor contextAccessor; + /// /// Initializes a new instance of the class. /// + /// The cache service. + /// The contextAccessor. /// The Web Api Http Client. /// The Open Api Http Client. /// logger. - public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger logger) + public ReportService(ICacheService cacheService, IHttpContextAccessor contextAccessor, ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger logger) : base(learningHubHttpClient, openApiHttpClient, logger) { + this.cacheService = cacheService; + this.contextAccessor = contextAccessor; } /// @@ -36,26 +43,10 @@ public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpC /// The . public async Task GetReporterPermission() { - bool viewmodel = false; - - var client = await this.OpenApiHttpClient.GetClientAsync(); - - var request = $"Report/GetReporterPermission"; - var response = await client.GetAsync(request).ConfigureAwait(false); - - if (response.IsSuccessStatusCode) - { - var result = response.Content.ReadAsStringAsync().Result; - viewmodel = JsonConvert.DeserializeObject(result); - } - else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized - || - response.StatusCode == System.Net.HttpStatusCode.Forbidden) - { - throw new Exception("AccessDenied"); - } - - return viewmodel; + bool response = false; + var cacheKey = $"{this.contextAccessor.HttpContext.User.Identity.GetCurrentUserId()}:DatabricksReporter"; + response = await this.cacheService.GetOrFetchAsync(cacheKey, this.FetchReporterPermission); + return response; } /// @@ -190,5 +181,28 @@ public async Task DownloadReport(int reportHistoryId) return apiResponse; } + + private async Task FetchReporterPermission() + { + bool viewmodel = false; + var client = await this.OpenApiHttpClient.GetClientAsync(); + + var request = $"Report/GetReporterPermission"; + var response = await client.GetAsync(request).ConfigureAwait(false); + + if (response.IsSuccessStatusCode) + { + var result = response.Content.ReadAsStringAsync().Result; + viewmodel = JsonConvert.DeserializeObject(result); + } + else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized + || + response.StatusCode == System.Net.HttpStatusCode.Forbidden) + { + throw new Exception("AccessDenied"); + } + + return viewmodel; + } } }