Skip to content

Commit 88f9ce4

Browse files
committed
Menu Permission Update
1 parent 1649450 commit 88f9ce4

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<NavigationModel> GetNavigationModelAsync(IPrincipal user, bool
4949
}
5050
else if (user.IsInRole("Administrator"))
5151
{
52-
return this.AuthenticatedAdministrator(controllerName);
52+
return await this.AuthenticatedAdministrator(controllerName);
5353
}
5454
else if (user.IsInRole("ReadOnly"))
5555
{
@@ -100,7 +100,7 @@ public NavigationModel NotAuthenticated()
100100
/// </summary>
101101
/// <param name="controllerName">The controller name.</param>
102102
/// <returns>The <see cref="NavigationModel"/>.</returns>
103-
private NavigationModel AuthenticatedAdministrator(string controllerName)
103+
private async Task<NavigationModel> AuthenticatedAdministrator(string controllerName)
104104
{
105105
return new NavigationModel()
106106
{
@@ -118,7 +118,7 @@ private NavigationModel AuthenticatedAdministrator(string controllerName)
118118
ShowSignOut = true,
119119
ShowMyAccount = true,
120120
ShowBrowseCatalogues = true,
121-
ShowReports = true,
121+
ShowReports = await this.reportService.GetReporterPermission(),
122122
};
123123
}
124124

LearningHub.Nhs.WebUI/Services/ReportService.cs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
namespace LearningHub.Nhs.WebUI.Services
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Net.Http;
65
using System.Text;
76
using System.Threading.Tasks;
8-
using elfhHub.Nhs.Models.Common;
7+
using LearningHub.Nhs.Caching;
98
using LearningHub.Nhs.Models.Common;
109
using LearningHub.Nhs.Models.Databricks;
10+
using LearningHub.Nhs.Models.Extensions;
1111
using LearningHub.Nhs.Models.Paging;
12-
using LearningHub.Nhs.Models.Validation;
1312
using LearningHub.Nhs.WebUI.Interfaces;
13+
using Microsoft.AspNetCore.Http;
1414
using Microsoft.Extensions.Logging;
1515
using Newtonsoft.Json;
1616

@@ -19,15 +19,22 @@
1919
/// </summary>
2020
public class ReportService : BaseService<ReportService>, IReportService
2121
{
22+
private readonly ICacheService cacheService;
23+
private readonly IHttpContextAccessor contextAccessor;
24+
2225
/// <summary>
2326
/// Initializes a new instance of the <see cref="ReportService"/> class.
2427
/// </summary>
28+
/// <param name="cacheService">The cache service.</param>
29+
/// <param name="contextAccessor">The contextAccessor.</param>
2530
/// <param name="learningHubHttpClient">The Web Api Http Client.</param>
2631
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
2732
/// <param name="logger">logger.</param>
28-
public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<ReportService> logger)
33+
public ReportService(ICacheService cacheService, IHttpContextAccessor contextAccessor, ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<ReportService> logger)
2934
: base(learningHubHttpClient, openApiHttpClient, logger)
3035
{
36+
this.cacheService = cacheService;
37+
this.contextAccessor = contextAccessor;
3138
}
3239

3340
/// <summary>
@@ -36,26 +43,10 @@ public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpC
3643
/// <returns>The <see cref="T:Task{bool}"/>.</returns>
3744
public async Task<bool> GetReporterPermission()
3845
{
39-
bool viewmodel = false;
40-
41-
var client = await this.OpenApiHttpClient.GetClientAsync();
42-
43-
var request = $"Report/GetReporterPermission";
44-
var response = await client.GetAsync(request).ConfigureAwait(false);
45-
46-
if (response.IsSuccessStatusCode)
47-
{
48-
var result = response.Content.ReadAsStringAsync().Result;
49-
viewmodel = JsonConvert.DeserializeObject<bool>(result);
50-
}
51-
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
52-
||
53-
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
54-
{
55-
throw new Exception("AccessDenied");
56-
}
57-
58-
return viewmodel;
46+
bool response = false;
47+
var cacheKey = $"{this.contextAccessor.HttpContext.User.Identity.GetCurrentUserId()}:DatabricksReporter";
48+
response = await this.cacheService.GetOrFetchAsync(cacheKey, this.FetchReporterPermission);
49+
return response;
5950
}
6051

6152
/// <summary>
@@ -190,5 +181,28 @@ public async Task<ReportHistoryModel> DownloadReport(int reportHistoryId)
190181

191182
return apiResponse;
192183
}
184+
185+
private async Task<bool> FetchReporterPermission()
186+
{
187+
bool viewmodel = false;
188+
var client = await this.OpenApiHttpClient.GetClientAsync();
189+
190+
var request = $"Report/GetReporterPermission";
191+
var response = await client.GetAsync(request).ConfigureAwait(false);
192+
193+
if (response.IsSuccessStatusCode)
194+
{
195+
var result = response.Content.ReadAsStringAsync().Result;
196+
viewmodel = JsonConvert.DeserializeObject<bool>(result);
197+
}
198+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
199+
||
200+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
201+
{
202+
throw new Exception("AccessDenied");
203+
}
204+
205+
return viewmodel;
206+
}
193207
}
194208
}

0 commit comments

Comments
 (0)