| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonDomains.Models.AccessControl; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace Services.AccessControl; |
| | | 7 | | |
| | | 8 | | public class DataScopeEvaluator : IDataScopeEvaluator |
| | | 9 | | { |
| | | 10 | | private readonly ApplicationDbContext _context; |
| | | 11 | | |
| | 17 | 12 | | public DataScopeEvaluator(ApplicationDbContext context) |
| | | 13 | | { |
| | 17 | 14 | | _context = context; |
| | 17 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public UserDataScope Evaluate(AccessProfile? accessProfile) |
| | | 19 | | { |
| | 10 | 20 | | if (accessProfile == null || !accessProfile.ScopeItems.Any()) |
| | 6 | 21 | | return UserDataScope.Unrestricted; |
| | | 22 | | |
| | 4 | 23 | | var items = accessProfile.ScopeItems.ToList(); |
| | | 24 | | |
| | 4 | 25 | | var appStatusIds = items |
| | 9 | 26 | | .Where(x => x.ScopeType == DataScopeType.AppStatus) |
| | 2 | 27 | | .Select(x => x.ReferenceId) |
| | 4 | 28 | | .ToHashSet(); |
| | | 29 | | |
| | 4 | 30 | | var supplierIds = items |
| | 9 | 31 | | .Where(x => x.ScopeType == DataScopeType.Supplier) |
| | 4 | 32 | | .Select(x => x.ReferenceId) |
| | 4 | 33 | | .ToHashSet(); |
| | | 34 | | |
| | 4 | 35 | | var ozonClientIds = items |
| | 9 | 36 | | .Where(x => x.ScopeType == DataScopeType.OzonClient) |
| | 1 | 37 | | .Select(x => x.ReferenceId) |
| | 4 | 38 | | .ToHashSet(); |
| | | 39 | | |
| | 4 | 40 | | var warehouseIds = items |
| | 9 | 41 | | .Where(x => x.ScopeType == DataScopeType.Warehouse) |
| | 1 | 42 | | .Select(x => x.ReferenceId) |
| | 4 | 43 | | .ToHashSet(); |
| | | 44 | | |
| | 4 | 45 | | var deliveryProviderIds = items |
| | 9 | 46 | | .Where(x => x.ScopeType == DataScopeType.DeliveryProvider) |
| | 1 | 47 | | .Select(x => x.ReferenceId) |
| | 4 | 48 | | .ToHashSet(); |
| | | 49 | | |
| | 4 | 50 | | bool isUnrestricted = appStatusIds.Count == 0 |
| | 4 | 51 | | && supplierIds.Count == 0 |
| | 4 | 52 | | && ozonClientIds.Count == 0 |
| | 4 | 53 | | && warehouseIds.Count == 0 |
| | 4 | 54 | | && deliveryProviderIds.Count == 0; |
| | | 55 | | |
| | 4 | 56 | | return new UserDataScope( |
| | 4 | 57 | | AppStatusIds: appStatusIds, |
| | 4 | 58 | | SupplierIds: supplierIds, |
| | 4 | 59 | | OzonClientIds: ozonClientIds, |
| | 4 | 60 | | WarehouseIds: warehouseIds, |
| | 4 | 61 | | DeliveryProviderIds: deliveryProviderIds, |
| | 4 | 62 | | IsUnrestricted: isUnrestricted |
| | 4 | 63 | | ); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public IQueryable<Order> ApplyToOrders(IQueryable<Order> query, UserDataScope scope) |
| | | 68 | | { |
| | 0 | 69 | | if (scope.IsUnrestricted) |
| | 0 | 70 | | return query; |
| | | 71 | | |
| | 0 | 72 | | if (scope.AppStatusIds.Count > 0) |
| | | 73 | | { |
| | 0 | 74 | | var ids = scope.AppStatusIds; |
| | 0 | 75 | | query = query.Where(o => o.AppStatus != null && ids.Contains(o.AppStatus.Id)); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | if (scope.SupplierIds.Count > 0) |
| | | 79 | | { |
| | 0 | 80 | | var ids = scope.SupplierIds; |
| | 0 | 81 | | query = query.Where(o => o.Supplier != null && ids.Contains(o.Supplier.Id)); |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | if (scope.OzonClientIds.Count > 0) |
| | | 85 | | { |
| | 0 | 86 | | var ids = scope.OzonClientIds; |
| | 0 | 87 | | query = query.Where(o => o.OzonClient != null && ids.Contains(o.OzonClient.Id)); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if (scope.WarehouseIds.Count > 0) |
| | | 91 | | { |
| | 0 | 92 | | var ids = scope.WarehouseIds; |
| | 0 | 93 | | var warehouseNames = _context.Warehouses |
| | 0 | 94 | | .Where(w => ids.Contains(w.Id)) |
| | 0 | 95 | | .Select(w => w.Name) |
| | 0 | 96 | | .ToList(); |
| | | 97 | | |
| | 0 | 98 | | query = query.Where(o => |
| | 0 | 99 | | (o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id)) || |
| | 0 | 100 | | (o.OrderExtensions != null && o.OrderExtensions.Any(e => |
| | 0 | 101 | | e.Key == "Засыл" && warehouseNames.Contains(e.Value)))); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | if (scope.DeliveryProviderIds.Count > 0) |
| | | 105 | | { |
| | 0 | 106 | | var ids = scope.DeliveryProviderIds; |
| | 0 | 107 | | query = query.Where(o => o.Delivery != null && |
| | 0 | 108 | | o.Delivery.Provider != null && |
| | 0 | 109 | | ids.Contains(o.Delivery.Provider.Id)); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return query; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | public IEnumerable<Order> ApplyToOrdersList(IEnumerable<Order> orders, UserDataScope scope) |
| | | 117 | | { |
| | 7 | 118 | | if (scope.IsUnrestricted) |
| | 1 | 119 | | return orders; |
| | | 120 | | |
| | 6 | 121 | | if (scope.AppStatusIds.Count > 0) |
| | | 122 | | { |
| | 1 | 123 | | var ids = scope.AppStatusIds; |
| | 4 | 124 | | orders = orders.Where(o => o.AppStatus != null && ids.Contains(o.AppStatus.Id)); |
| | | 125 | | } |
| | | 126 | | |
| | 6 | 127 | | if (scope.SupplierIds.Count > 0) |
| | | 128 | | { |
| | 1 | 129 | | var ids = scope.SupplierIds; |
| | 4 | 130 | | orders = orders.Where(o => o.Supplier != null && ids.Contains(o.Supplier.Id)); |
| | | 131 | | } |
| | | 132 | | |
| | 6 | 133 | | if (scope.OzonClientIds.Count > 0) |
| | | 134 | | { |
| | 1 | 135 | | var ids = scope.OzonClientIds; |
| | 3 | 136 | | orders = orders.Where(o => o.OzonClient != null && ids.Contains(o.OzonClient.Id)); |
| | | 137 | | } |
| | | 138 | | |
| | 6 | 139 | | if (scope.WarehouseIds.Count > 0) |
| | | 140 | | { |
| | 2 | 141 | | var ids = scope.WarehouseIds; |
| | | 142 | | // For in-memory filtering, derive warehouse names from loaded orders |
| | 2 | 143 | | var warehouseNames = orders |
| | 4 | 144 | | .Where(o => o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id)) |
| | 2 | 145 | | .Select(o => o.ShipmentWarehouse!.Name) |
| | 2 | 146 | | .Distinct() |
| | 2 | 147 | | .ToHashSet(); |
| | | 148 | | |
| | 2 | 149 | | orders = orders.Where(o => |
| | 6 | 150 | | (o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id)) || |
| | 6 | 151 | | (o.OrderExtensions != null && o.OrderExtensions.Any(e => |
| | 7 | 152 | | e.Key == "Засыл" && warehouseNames.Contains(e.Value)))); |
| | | 153 | | } |
| | | 154 | | |
| | 6 | 155 | | if (scope.DeliveryProviderIds.Count > 0) |
| | | 156 | | { |
| | 1 | 157 | | var ids = scope.DeliveryProviderIds; |
| | 4 | 158 | | orders = orders.Where(o => o.Delivery != null && |
| | 4 | 159 | | o.Delivery.Provider != null && |
| | 4 | 160 | | ids.Contains(o.Delivery.Provider.Id)); |
| | | 161 | | } |
| | | 162 | | |
| | 6 | 163 | | return orders; |
| | | 164 | | } |
| | | 165 | | } |