| | | 1 | | using System.Collections; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Security.Claims; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using OzonDomains.Models.Authorization.Enums; |
| | | 7 | | using OzonRepositories.Context; |
| | | 8 | | using Services.Authorization.Services; |
| | | 9 | | |
| | | 10 | | namespace OzonOrdersWeb.ViewComponents; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Отображает таблицу только с теми столбцами, на которые у текущего пользователя есть CanRead. |
| | | 14 | | /// Источник метаданных столбцов — AppField (EntityName, FieldType=Column). |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class DynamicTableViewComponent : ViewComponent |
| | | 17 | | { |
| | | 18 | | private readonly IPermissionService _permissions; |
| | | 19 | | private readonly ApplicationDbContext _db; |
| | | 20 | | |
| | | 21 | | public DynamicTableViewComponent(IPermissionService permissions, ApplicationDbContext db) |
| | | 22 | | { |
| | | 23 | | _permissions = permissions; |
| | | 24 | | _db = db; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<IViewComponentResult> InvokeAsync(string entityName, IEnumerable data, string? tableCssClass = nul |
| | | 28 | | { |
| | | 29 | | var userId = ((ClaimsPrincipal)User).FindFirst(ClaimTypes.NameIdentifier)?.Value; |
| | | 30 | | var model = new DynamicTableModel |
| | | 31 | | { |
| | | 32 | | EntityName = entityName, |
| | | 33 | | Rows = data?.Cast<object?>().ToList() ?? new List<object?>(), |
| | | 34 | | TableCssClass = tableCssClass ?? "table table-sm table-bordered", |
| | | 35 | | EmptyMessage = emptyMessage ?? "Нет данных" |
| | | 36 | | }; |
| | | 37 | | |
| | | 38 | | // Получаем зарегистрированные столбцы из AppField |
| | | 39 | | var fields = await _db.AppFields.AsNoTracking() |
| | | 40 | | .Where(f => f.EntityName == entityName && f.IsActive && f.FieldType == FieldType.Column) |
| | | 41 | | .OrderBy(f => f.SortOrder).ThenBy(f => f.FieldName) |
| | | 42 | | .ToListAsync(); |
| | | 43 | | |
| | | 44 | | // Фильтруем по правам пользователя |
| | | 45 | | var allowed = new List<DynamicColumn>(); |
| | | 46 | | if (!string.IsNullOrWhiteSpace(userId)) |
| | | 47 | | { |
| | | 48 | | var perms = await _permissions.GetUserFieldPermissionsAsync(userId, entityName); |
| | | 49 | | var allowedNames = perms.Where(p => p.CanRead).Select(p => p.FieldName).ToHashSet(StringComparer.OrdinalIgno |
| | | 50 | | var bundle = await _permissions.GetUserPermissionsAsync(userId); |
| | | 51 | | foreach (var f in fields) |
| | | 52 | | { |
| | | 53 | | if (bundle.IsSuperAdmin || allowedNames.Contains(f.FieldName)) |
| | | 54 | | allowed.Add(new DynamicColumn(f.FieldName, f.DisplayName)); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | | 59 | | // Неаутентифицированный — без столбцов |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | model.Columns = allowed; |
| | | 63 | | |
| | | 64 | | // Заранее получаем PropertyInfo для ускорения рендера |
| | | 65 | | if (model.Rows.Count > 0 && model.Rows[0] is not null) |
| | | 66 | | { |
| | | 67 | | var type = model.Rows[0]!.GetType(); |
| | | 68 | | model.PropertyAccessors = allowed.ToDictionary( |
| | | 69 | | c => c.FieldName, |
| | | 70 | | c => type.GetProperty(c.FieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase |
| | | 71 | | } |
| | | 72 | | else |
| | | 73 | | { |
| | | 74 | | model.PropertyAccessors = new Dictionary<string, PropertyInfo?>(); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | return View(model); |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public sealed class DynamicTableModel |
| | | 82 | | { |
| | 0 | 83 | | public string EntityName { get; set; } = string.Empty; |
| | 0 | 84 | | public List<DynamicColumn> Columns { get; set; } = new(); |
| | 0 | 85 | | public List<object?> Rows { get; set; } = new(); |
| | 0 | 86 | | public string TableCssClass { get; set; } = "table"; |
| | 0 | 87 | | public string EmptyMessage { get; set; } = "Нет данных"; |
| | 0 | 88 | | public Dictionary<string, PropertyInfo?> PropertyAccessors { get; set; } = new(); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public sealed record DynamicColumn(string FieldName, string DisplayName); |