Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions LearningHub.Nhs.WebUI/Interfaces/IReportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LearningHub.Nhs.WebUI.Interfaces
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using elfhHub.Nhs.Models.Common;

/// <summary>
/// Defines the <see cref="IRegionService" />.
/// </summary>
public interface IReportService
{
/// <summary>
/// The GetReporterPermission.
/// </summary>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<bool> GetReporterPermission();
}
}
5 changes: 5 additions & 0 deletions LearningHub.Nhs.WebUI/Models/NavigationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public class NavigationModel
/// </summary>
public bool ShowBrowseCatalogues { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show reports.
/// </summary>
public bool ShowReports { get; set; }

/// <summary>
/// Gets or sets a value indicating whether ShowHome.
/// </summary>
Expand Down
13 changes: 12 additions & 1 deletion LearningHub.Nhs.WebUI/Services/NavigationPermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ public class NavigationPermissionService : INavigationPermissionService
{
private readonly IResourceService resourceService;
private readonly IUserGroupService userGroupService;
private readonly IReportService reportService;

/// <summary>
/// Initializes a new instance of the <see cref="NavigationPermissionService"/> class.
/// </summary>
/// <param name="resourceService">Resource service.</param>
/// <param name="userGroupService">UserGroup service.</param>
/// <param name="reportService">Report Service.</param>
public NavigationPermissionService(
IResourceService resourceService,
IUserGroupService userGroupService)
IUserGroupService userGroupService,
IReportService reportService)
{
this.resourceService = resourceService;
this.userGroupService = userGroupService;
this.reportService = reportService;
}

/// <summary>
Expand Down Expand Up @@ -87,6 +91,7 @@ public NavigationModel NotAuthenticated()
ShowSignOut = false,
ShowMyAccount = false,
ShowBrowseCatalogues = false,
ShowReports = false,
};
}

Expand All @@ -113,6 +118,7 @@ private NavigationModel AuthenticatedAdministrator(string controllerName)
ShowSignOut = true,
ShowMyAccount = true,
ShowBrowseCatalogues = true,
ShowReports = true,
};
}

Expand All @@ -139,6 +145,7 @@ private async Task<NavigationModel> AuthenticatedBlueUser(string controllerName)
ShowSignOut = true,
ShowMyAccount = true,
ShowBrowseCatalogues = true,
ShowReports = await this.reportService.GetReporterPermission(),
};
}

Expand All @@ -164,6 +171,7 @@ private NavigationModel AuthenticatedGuest()
ShowSignOut = true,
ShowMyAccount = false,
ShowBrowseCatalogues = false,
ShowReports = false,
};
}

Expand All @@ -190,6 +198,7 @@ private async Task<NavigationModel> AuthenticatedReadOnly(string controllerName)
ShowSignOut = true,
ShowMyAccount = false,
ShowBrowseCatalogues = true,
ShowReports = await this.reportService.GetReporterPermission(),
};
}

Expand All @@ -215,6 +224,7 @@ private async Task<NavigationModel> AuthenticatedBasicUserOnly()
ShowSignOut = true,
ShowMyAccount = true,
ShowBrowseCatalogues = true,
ShowReports = false,
};
}

Expand All @@ -240,6 +250,7 @@ private NavigationModel InLoginWizard()
ShowSignOut = true,
ShowMyAccount = false,
ShowBrowseCatalogues = false,
ShowReports = false,
};
}
}
Expand Down
55 changes: 55 additions & 0 deletions LearningHub.Nhs.WebUI/Services/ReportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace LearningHub.Nhs.WebUI.Services
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using elfhHub.Nhs.Models.Common;
using LearningHub.Nhs.WebUI.Interfaces;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

/// <summary>
/// Defines the <see cref="RegionService" />.
/// </summary>
public class ReportService : BaseService<ReportService>, IReportService
{
/// <summary>
/// Initializes a new instance of the <see cref="ReportService"/> class.
/// </summary>
/// <param name="learningHubHttpClient">The Web Api Http Client.</param>
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
/// <param name="logger">logger.</param>
public ReportService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient, ILogger<ReportService> logger)
: base(learningHubHttpClient, openApiHttpClient, logger)
{
}

/// <summary>
/// The GetAllAsync.
/// </summary>
/// <returns>The <see cref="T:Task{bool}"/>.</returns>
public async Task<bool> 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<bool>(result);
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}

return viewmodel;
}
}
}
1 change: 1 addition & 0 deletions LearningHub.Nhs.WebUI/Startup/ServiceMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
services.AddScoped<INotificationService, NotificationService>();
services.AddScoped<ISearchService, SearchService>();
services.AddScoped<IRegionService, RegionService>();
services.AddScoped<IReportService, ReportService>();
services.AddScoped<ICatalogueService, CatalogueService>();
services.AddScoped<IHierarchyService, HierarchyService>();
services.AddScoped<ICountryService, CountryService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace LearningHub.Nhs.OpenApi.Models.Configuration
{
/// <summary>
/// DatabricksConfig
/// </summary>
public class DatabricksConfig
{
/// <summary>
/// Gets or sets the ResourceId for the databricks instance.
/// </summary>
public string ResourceId { get; set; } = null!;

/// <summary>
/// Gets or sets the base url for the databricks instance.
/// </summary>
public string InstanceUrl { get; set; } = null!;

/// <summary>
/// Gets or sets the warehouse id for databricks.
/// </summary>
public string WarehouseId { get; set; } = null!;

/// <summary>
/// Gets or sets the tenant Id of the service pricncipl.
/// </summary>
public string TenantId { get; set; } = null!;

/// <summary>
/// Gets or sets the client Id of the service pricncipl.
/// </summary>
public string ClientId { get; set; } = null!;

/// <summary>
/// Gets or sets the client scret of the service pricncipl.
/// </summary>
public string clientSecret { get; set; } = null!;

/// <summary>
/// Gets or sets the endpoint to check user permission.
/// </summary>
public string UserPermissionEndpoint { get; set; } = "databricks_poc2_ws_uks.tel_unified_reporting_gold.sp_isreporter"; //null!;

/// <summary>
/// Gets or sets the token.
/// </summary>
public string Token { get; set; } = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public class LearningHubConfig
/// </summary>
public string BrowseCataloguesUrl { get; set; } = null!;

/// <summary>
/// Gets or sets <see cref="ReportUrl"/>.
/// </summary>
public string ReportUrl { get; set; } = null!;


/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public class NavigationModel
/// </summary>
public bool ShowBrowseCatalogues { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show reports.
/// </summary>
public bool ShowReports { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to show home.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LearningHub.Nhs.OpenApi.Services.Interface.HttpClients
{
using System;
using System.Net.Http;
using System.Threading.Tasks;

/// <summary>
/// The Bookmark Http Client interface.
/// </summary>
public interface IDatabricksApiHttpClient : IDisposable
{
/// <summary>
/// GETs data from Databricks API.
/// </summary>
/// <param name="requestUrl">The URL to make a get call to.</param>
/// <param name="authHeader">Optional authorization header.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<HttpResponseMessage> GetData(string requestUrl, string? authHeader);

/// <summary>
/// The Get Client method.
/// </summary>
/// <returns>The <see cref="HttpClient"/>.</returns>
HttpClient GetClient();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;

namespace LearningHub.Nhs.OpenApi.Services.Interface.Services
{
/// <summary>
/// IDatabricks service
/// </summary>
public interface IDatabricksService
{
/// <summary>
/// IsUserReporter.
/// </summary>
/// <param name="userId">The userId.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<bool> IsUserReporter(int userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace LearningHub.Nhs.OpenApi.Services.HttpClients
{
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using IdentityModel.Client;
using LearningHub.Nhs.OpenApi.Models.Configuration;
using LearningHub.Nhs.OpenApi.Services.Interface.HttpClients;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;

/// <summary>
/// Http client for Databricks.
/// </summary>
public class DatabricksApiHttpClient : IDatabricksApiHttpClient
{
private readonly HttpClient httpClient;
private readonly IOptions<DatabricksConfig> databricksConfig;

/// <summary>
/// Initializes a new instance of the <see cref="DatabricksApiHttpClient"/> class.
/// </summary>
/// <param name="databricksConfig">Configuration details for the databricks.</param>
public DatabricksApiHttpClient(IOptions<DatabricksConfig> databricksConfig)
{
this.databricksConfig = databricksConfig;
this.httpClient = new HttpClient { BaseAddress = new Uri(databricksConfig.Value.InstanceUrl) };
this.httpClient.DefaultRequestHeaders.Accept.Clear();
this.httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}

/// <inheritdoc />
public void Dispose()
{
this.httpClient.Dispose();
}

/// <summary>
/// The Get Client method.
/// </summary>
/// <returns>The <see cref="HttpClient"/>.</returns>
public HttpClient GetClient()
{
string accessToken = this.databricksConfig.Value.Token;
this.httpClient.SetBearerToken(accessToken);
return this.httpClient;
}

/// <inheritdoc/>
public async Task<HttpResponseMessage> GetData(string requestUrl, string? authHeader)
{
if (!string.IsNullOrEmpty(authHeader))
{
this.httpClient.SetBearerToken(authHeader);
}

var message = await this.httpClient.GetAsync(requestUrl).ConfigureAwait(false);
return message;
}
}
}
Loading
Loading