| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Filters; |
| | | 4 | | using Microsoft.AspNetCore.Mvc.ViewFeatures; |
| | | 5 | | using OzonOrdersWeb.Authorization; |
| | | 6 | | using Services.Authorization.Services; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Filters; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Права <see cref="IPermissionService"/> на пункты PIM-меню для <c>_Layout.cshtml</c> (коды страниц, без путей MVC). |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class PimNavViewDataResultFilter : IAsyncResultFilter |
| | | 14 | | { |
| | | 15 | | private readonly IPermissionService _permissions; |
| | | 16 | | |
| | 0 | 17 | | public PimNavViewDataResultFilter(IPermissionService permissions) => _permissions = permissions; |
| | | 18 | | |
| | | 19 | | public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) |
| | | 20 | | { |
| | 0 | 21 | | if (context.HttpContext.User.Identity?.IsAuthenticated == true |
| | 0 | 22 | | && context.Result is ViewResult { ViewData: { } viewData }) |
| | | 23 | | { |
| | 0 | 24 | | await EnrichViewData(viewData, context.HttpContext, context.HttpContext.RequestAborted); |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | await next(); |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | private async Task EnrichViewData(ViewDataDictionary viewData, HttpContext http, CancellationToken ct) |
| | | 31 | | { |
| | 0 | 32 | | var userId = http.User.FindFirstValue(ClaimTypes.NameIdentifier); |
| | 0 | 33 | | if (string.IsNullOrEmpty(userId)) |
| | | 34 | | { |
| | 0 | 35 | | viewData["UseAppPagePimNav"] = false; |
| | 0 | 36 | | viewData["PimNavAllowedCodes"] = Array.Empty<string>(); |
| | 0 | 37 | | viewData["IsAppSuperAdminPim"] = false; |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | if (http.User.IsInRole("Admin")) |
| | | 42 | | { |
| | 0 | 43 | | viewData["UseAppPagePimNav"] = false; |
| | 0 | 44 | | viewData["PimNavAllowedCodes"] = Array.Empty<string>(); |
| | 0 | 45 | | viewData["IsAppSuperAdminPim"] = false; |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | var bundle = await _permissions.GetUserPermissionsAsync(userId, ct); |
| | 0 | 50 | | viewData["IsAppSuperAdminPim"] = bundle.IsSuperAdmin; |
| | 0 | 51 | | if (bundle.IsSuperAdmin) |
| | | 52 | | { |
| | 0 | 53 | | viewData["UseAppPagePimNav"] = false; |
| | 0 | 54 | | viewData["PimNavAllowedCodes"] = Array.Empty<string>(); |
| | 0 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (bundle.Pages is null || bundle.Pages.Count == 0) |
| | | 59 | | { |
| | 0 | 60 | | viewData["UseAppPagePimNav"] = false; |
| | 0 | 61 | | viewData["PimNavAllowedCodes"] = Array.Empty<string>(); |
| | 0 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var byCode = bundle.Pages.ToDictionary(p => p.PageCode, StringComparer.OrdinalIgnoreCase); |
| | 0 | 66 | | var list = new List<string>(); |
| | 0 | 67 | | foreach (var code in Studio2PimNavMenuCatalog.AllPimNavPageCodes) |
| | | 68 | | { |
| | 0 | 69 | | if (byCode.TryGetValue(code, out var p) && (p.CanView || p.CanCreate)) |
| | | 70 | | { |
| | 0 | 71 | | list.Add(code); |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | viewData["UseAppPagePimNav"] = true; |
| | 0 | 76 | | viewData["PimNavAllowedCodes"] = list; |
| | 0 | 77 | | } |
| | | 78 | | } |