|
14 | 14 | using Microsoft.AspNetCore.Authorization; |
15 | 15 | using Microsoft.AspNetCore.Mvc; |
16 | 16 | using Microsoft.Extensions.Options; |
| 17 | + using System.Security.Claims; |
| 18 | + using System.Security.Principal; |
17 | 19 |
|
18 | 20 | /// <summary> |
19 | 21 | /// The log controller. |
@@ -261,26 +263,64 @@ public async Task<IActionResult> ValidateEmailChangeTokenAsync(string token, str |
261 | 263 | /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> |
262 | 264 | [HttpGet] |
263 | 265 | [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 = "") |
266 | 268 | { |
267 | 269 | NavigationModel model; |
268 | 270 |
|
269 | | - if (!this.User.Identity.IsAuthenticated) |
| 271 | + if (!int.TryParse(userId, out int validUserId) || validUserId <= 0) |
270 | 272 | { |
271 | 273 | 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(); |
272 | 284 | } |
273 | 285 | else |
274 | 286 | { |
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 | + } |
276 | 300 |
|
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 | + }; |
278 | 306 |
|
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 | + } |
280 | 314 |
|
281 | | - model.NotificationCount = await this.userNotificationService.GetUserUnreadNotificationCountAsync(userId); |
| 315 | + var identity = new ClaimsIdentity(claims, "Impersonated"); |
| 316 | + userPrincipal = new ClaimsPrincipal(identity); |
282 | 317 | } |
283 | 318 |
|
| 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 | + |
284 | 324 | return this.MenuItems(model); |
285 | 325 | } |
286 | 326 |
|
@@ -329,9 +369,9 @@ private List<Dictionary<string, object>> MenuItems(NavigationModel model) |
329 | 369 | }, |
330 | 370 | new Dictionary<string, object> |
331 | 371 | { |
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 }, |
335 | 375 | }, |
336 | 376 | }; |
337 | 377 | return menu; |
|
0 commit comments