| | | 1 | | using System.Security.Claims; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | using Services.Authorization.Services; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.WebServices.Orders; |
| | | 8 | | |
| | | 9 | | public sealed class OrderDataScopeQueryApplier : IOrderDataScopeQueryApplier |
| | | 10 | | { |
| | | 11 | | private readonly IDataScopeService _dataScopeService; |
| | | 12 | | private readonly IScopeFilterValuesService _scopeFilterValuesService; |
| | | 13 | | |
| | 0 | 14 | | public OrderDataScopeQueryApplier( |
| | 0 | 15 | | IDataScopeService dataScopeService, |
| | 0 | 16 | | IScopeFilterValuesService scopeFilterValuesService) |
| | | 17 | | { |
| | 0 | 18 | | _dataScopeService = dataScopeService; |
| | 0 | 19 | | _scopeFilterValuesService = scopeFilterValuesService; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<IQueryable<Order>> ApplyAsync( |
| | | 23 | | IQueryable<Order> ordersQuery, |
| | | 24 | | string? userId, |
| | | 25 | | CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 0 | 27 | | if (string.IsNullOrEmpty(userId)) |
| | | 28 | | { |
| | 0 | 29 | | return ordersQuery; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | var scopedQuery = await _dataScopeService.ApplyDataScopeAsync( |
| | 0 | 33 | | ordersQuery, |
| | 0 | 34 | | userId, |
| | 0 | 35 | | entityName: "Order", |
| | 0 | 36 | | extraParameters: null, |
| | 0 | 37 | | ct: cancellationToken); |
| | | 38 | | |
| | 0 | 39 | | var allowed = await _scopeFilterValuesService.GetAllowedValuesAsync(userId, "Order", cancellationToken); |
| | 0 | 40 | | if (allowed.TryGetValue("ShipmentWarehouseId", out var allowedWarehouseIdsRaw) && |
| | 0 | 41 | | allowedWarehouseIdsRaw.Count > 0) |
| | | 42 | | { |
| | 0 | 43 | | var allowedWarehouseIds = allowedWarehouseIdsRaw |
| | 0 | 44 | | .Select(x => int.TryParse(x, out var id) ? id : (int?)null) |
| | 0 | 45 | | .Where(x => x.HasValue) |
| | 0 | 46 | | .Select(x => x!.Value) |
| | 0 | 47 | | .Distinct() |
| | 0 | 48 | | .ToArray(); |
| | | 49 | | |
| | 0 | 50 | | if (allowedWarehouseIds.Length > 0) |
| | | 51 | | { |
| | 0 | 52 | | scopedQuery = scopedQuery.Where(o => |
| | 0 | 53 | | o.ShipmentWarehouseId.HasValue && allowedWarehouseIds.Contains(o.ShipmentWarehouseId.Value)); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return scopedQuery; |
| | 0 | 58 | | } |
| | | 59 | | } |