Skip to content

Commit 0233e88

Browse files
committed
Removed API calls from LH navigation
1 parent 65bffcc commit 0233e88

File tree

3 files changed

+15
-65
lines changed

3 files changed

+15
-65
lines changed

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/UserController.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -263,67 +263,30 @@ public async Task<IActionResult> ValidateEmailChangeTokenAsync(string token, str
263263
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
264264
[HttpGet]
265265
[AllowAnonymous]
266-
[Route("GetLHUserNavigation/{userId}")]
267-
public async Task<List<Dictionary<string, object>>> GetLHUserNavigation(string userId = "")
266+
[Route("GetLHUserNavigation")]
267+
public async Task<List<Dictionary<string, object>>> GetLHUserNavigation()
268268
{
269269
NavigationModel model;
270270

271-
if (!int.TryParse(userId, out int validUserId) || validUserId <= 0)
271+
if (!this.User.Identity.IsAuthenticated)
272272
{
273273
model = this.permissionService.NotAuthenticated();
274-
return this.MenuItems(model);
275-
}
276-
277-
IPrincipal userPrincipal;
278-
279-
// Use current user if already authenticated
280-
if (this.User?.Identity?.IsAuthenticated == true)
281-
{
282-
userPrincipal = this.User;
283-
validUserId = this.User.Identity.GetCurrentUserId();
284274
}
285275
else
286276
{
287-
var basicDetails = await this.userService.GetByIdAsync(validUserId);
288-
if (basicDetails == null || string.IsNullOrWhiteSpace(basicDetails.UserName))
289-
{
290-
model = this.permissionService.NotAuthenticated();
291-
return this.MenuItems(model);
292-
}
293-
294-
var user = await this.userService.GetByUsernameAsync(basicDetails.UserName);
295-
if (user == null || user.AssignedRoles == null)
296-
{
297-
model = this.permissionService.NotAuthenticated();
298-
return this.MenuItems(model);
299-
}
277+
var userId = this.User.Identity.GetCurrentUserId();
300278

301-
var claims = new List<Claim>
302-
{
303-
new Claim(ClaimTypes.NameIdentifier, validUserId.ToString()),
304-
new Claim(ClaimTypes.Name, user.UserName),
305-
};
279+
var (cacheExists, _) = await this.cacheService.TryGetAsync<string>($"{userId}:LoginWizard");
306280

307-
foreach (var role in user.AssignedRoles)
308-
{
309-
if (!string.IsNullOrWhiteSpace(role?.Name))
310-
{
311-
claims.Add(new Claim(ClaimTypes.Role, role.Name));
312-
}
313-
}
281+
model = await this.permissionService.GetNavigationModelAsync(this.User, !cacheExists, string.Empty);
314282

315-
var identity = new ClaimsIdentity(claims, "Impersonated");
316-
userPrincipal = new ClaimsPrincipal(identity);
283+
model.NotificationCount = await this.userNotificationService.GetUserUnreadNotificationCountAsync(userId);
317284
}
318285

319-
var (cacheExists, _) = await this.cacheService.TryGetAsync<string>($"{validUserId}:LoginWizard");
320-
321-
model = await this.permissionService.GetNavigationModelAsync(userPrincipal, !cacheExists, string.Empty);
322-
model.NotificationCount = await this.userNotificationService.GetUserUnreadNotificationCountAsync(validUserId);
323-
324286
return this.MenuItems(model);
325287
}
326288

289+
327290
private List<Dictionary<string, object>> MenuItems(NavigationModel model)
328291
{
329292
var menu = new List<Dictionary<string, object>>

OpenAPI/LearningHub.Nhs.OpenApi/Startup.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
namespace LearningHub.NHS.OpenAPI
66
{
7+
using System;
78
using System.Collections.Generic;
89
using System.IO;
910
using AspNetCore.Authentication.ApiKey;
11+
using LearningHub.Nhs.Caching;
12+
using LearningHub.Nhs.Models.Enums;
13+
using LearningHub.Nhs.Models.Extensions;
1014
using LearningHub.NHS.OpenAPI.Auth;
1115
using LearningHub.NHS.OpenAPI.Configuration;
1216
using LearningHub.NHS.OpenAPI.Middleware;
@@ -25,11 +29,6 @@ namespace LearningHub.NHS.OpenAPI
2529
using Microsoft.Extensions.Hosting;
2630
using Microsoft.IdentityModel.Tokens;
2731
using Microsoft.OpenApi.Models;
28-
using LearningHub.Nhs.Caching;
29-
using LearningHub.Nhs.Models.Enums;
30-
using System.Configuration;
31-
using System;
32-
using LearningHub.Nhs.Models.Extensions;
3332

3433
/// <summary>
3534
/// The Startup class.
@@ -67,7 +66,7 @@ public void ConfigureServices(IServiceCollection services)
6766
options.TokenValidationParameters = new TokenValidationParameters()
6867
{
6968
NameClaimType = "given_name",
70-
RoleClaimType = "role",
69+
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
7170
ValidateAudience = true,
7271
ValidAudiences = new List<string> { "learninghubopenapi", "learninghubapi" },
7372
};
@@ -84,8 +83,6 @@ public void ConfigureServices(IServiceCollection services)
8483
services.AddApplicationInsightsTelemetry();
8584
services.AddControllers(options => options.Filters.Add(new HttpResponseExceptionFilter()));
8685
services.AddControllers(opt => { opt.Filters.Add(new AuthorizeFilter()); })
87-
88-
// .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
8986
.AddJsonOptions(options =>
9087
{
9188
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
@@ -133,6 +130,7 @@ public void ConfigureServices(IServiceCollection services)
133130
Scopes = new Dictionary<string, string>
134131
{
135132
{ "learninghubapi", string.Empty },
133+
136134
},
137135
},
138136
},

OpenAPI/LearningHub.Nhs.OpenApi/SwaggerDefinitions/v1.3.0.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6084,22 +6084,11 @@
60846084
}
60856085
}
60866086
},
6087-
"/User/GetLHUserNavigation/{userId}": {
6087+
"/User/GetLHUserNavigation": {
60886088
"get": {
60896089
"tags": [
60906090
"User"
60916091
],
6092-
"parameters": [
6093-
{
6094-
"name": "userId",
6095-
"in": "path",
6096-
"required": true,
6097-
"schema": {
6098-
"type": "string",
6099-
"default": ""
6100-
}
6101-
}
6102-
],
61036092
"responses": {
61046093
"200": {
61056094
"description": "OK",

0 commit comments

Comments
 (0)