Skip to content

Commit bcb74c9

Browse files
committed
LH Menu refactor
1 parent 8f48caa commit bcb74c9

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

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

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using Microsoft.AspNetCore.Authorization;
1515
using Microsoft.AspNetCore.Mvc;
1616
using Microsoft.Extensions.Options;
17+
using System.Security.Claims;
18+
using System.Security.Principal;
1719

1820
/// <summary>
1921
/// The log controller.
@@ -261,26 +263,64 @@ public async Task<IActionResult> ValidateEmailChangeTokenAsync(string token, str
261263
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
262264
[HttpGet]
263265
[AllowAnonymous]
264-
[Route("GetLHUserNavigation")]
265-
public async Task<List<Dictionary<string, object>>> GetLHUserNavigation()
266+
[Route("GetLHUserNavigation/{userId}")]
267+
public async Task<List<Dictionary<string, object>>> GetLHUserNavigation(string userId = "")
266268
{
267269
NavigationModel model;
268270

269-
if (!this.User.Identity.IsAuthenticated)
271+
if (!int.TryParse(userId, out int validUserId) || validUserId <= 0)
270272
{
271273
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();
272284
}
273285
else
274286
{
275-
var userId = this.User.Identity.GetCurrentUserId();
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+
}
276300

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

279-
model = await this.permissionService.GetNavigationModelAsync(this.User, !cacheExists, string.Empty);
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+
}
280314

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

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+
284324
return this.MenuItems(model);
285325
}
286326

@@ -329,9 +369,9 @@ private List<Dictionary<string, object>> MenuItems(NavigationModel model)
329369
},
330370
new Dictionary<string, object>
331371
{
332-
{ "title", "Admin" },
333-
{ "url", this.learningHubConfig.AdminUrl },
334-
{ "visible", model.ShowAdmin },
372+
{ "title", "Sign Out" },
373+
{ "url", this.learningHubConfig.SignOutUrl },
374+
{ "visible", model.ShowSignOut },
335375
},
336376
};
337377
return menu;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6084,11 +6084,21 @@
60846084
}
60856085
}
60866086
},
6087-
"/User/GetLHUserNavigation": {
6087+
"/User/GetLHUserNavigation/{userId}": {
60886088
"get": {
60896089
"tags": [
60906090
"User"
60916091
],
6092+
"parameters": [
6093+
{
6094+
"name": "userId",
6095+
"in": "query",
6096+
"schema": {
6097+
"type": "string",
6098+
"default": ""
6099+
}
6100+
}
6101+
],
60926102
"responses": {
60936103
"200": {
60946104
"description": "OK",

0 commit comments

Comments
 (0)