< Summary

Information
Class: Services.AccessControl.DataScopeEvaluator
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/AccessControl/DataScopeEvaluator.cs
Line coverage
71%
Covered lines: 67
Uncovered lines: 27
Coverable lines: 94
Total lines: 165
Line coverage: 71.2%
Branch coverage
68%
Covered branches: 40
Total branches: 58
Branch coverage: 68.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Evaluate(...)75%1212100%
ApplyToOrders(...)0%156120%
ApplyToOrdersList(...)91.17%3434100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/AccessControl/DataScopeEvaluator.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonDomains.Models.AccessControl;
 4using OzonRepositories.Context;
 5
 6namespace Services.AccessControl;
 7
 8public class DataScopeEvaluator : IDataScopeEvaluator
 9{
 10    private readonly ApplicationDbContext _context;
 11
 1712    public DataScopeEvaluator(ApplicationDbContext context)
 13    {
 1714        _context = context;
 1715    }
 16
 17    /// <inheritdoc />
 18    public UserDataScope Evaluate(AccessProfile? accessProfile)
 19    {
 1020        if (accessProfile == null || !accessProfile.ScopeItems.Any())
 621            return UserDataScope.Unrestricted;
 22
 423        var items = accessProfile.ScopeItems.ToList();
 24
 425        var appStatusIds = items
 926            .Where(x => x.ScopeType == DataScopeType.AppStatus)
 227            .Select(x => x.ReferenceId)
 428            .ToHashSet();
 29
 430        var supplierIds = items
 931            .Where(x => x.ScopeType == DataScopeType.Supplier)
 432            .Select(x => x.ReferenceId)
 433            .ToHashSet();
 34
 435        var ozonClientIds = items
 936            .Where(x => x.ScopeType == DataScopeType.OzonClient)
 137            .Select(x => x.ReferenceId)
 438            .ToHashSet();
 39
 440        var warehouseIds = items
 941            .Where(x => x.ScopeType == DataScopeType.Warehouse)
 142            .Select(x => x.ReferenceId)
 443            .ToHashSet();
 44
 445        var deliveryProviderIds = items
 946            .Where(x => x.ScopeType == DataScopeType.DeliveryProvider)
 147            .Select(x => x.ReferenceId)
 448            .ToHashSet();
 49
 450        bool isUnrestricted = appStatusIds.Count == 0
 451                              && supplierIds.Count == 0
 452                              && ozonClientIds.Count == 0
 453                              && warehouseIds.Count == 0
 454                              && deliveryProviderIds.Count == 0;
 55
 456        return new UserDataScope(
 457            AppStatusIds: appStatusIds,
 458            SupplierIds: supplierIds,
 459            OzonClientIds: ozonClientIds,
 460            WarehouseIds: warehouseIds,
 461            DeliveryProviderIds: deliveryProviderIds,
 462            IsUnrestricted: isUnrestricted
 463        );
 64    }
 65
 66    /// <inheritdoc />
 67    public IQueryable<Order> ApplyToOrders(IQueryable<Order> query, UserDataScope scope)
 68    {
 069        if (scope.IsUnrestricted)
 070            return query;
 71
 072        if (scope.AppStatusIds.Count > 0)
 73        {
 074            var ids = scope.AppStatusIds;
 075            query = query.Where(o => o.AppStatus != null && ids.Contains(o.AppStatus.Id));
 76        }
 77
 078        if (scope.SupplierIds.Count > 0)
 79        {
 080            var ids = scope.SupplierIds;
 081            query = query.Where(o => o.Supplier != null && ids.Contains(o.Supplier.Id));
 82        }
 83
 084        if (scope.OzonClientIds.Count > 0)
 85        {
 086            var ids = scope.OzonClientIds;
 087            query = query.Where(o => o.OzonClient != null && ids.Contains(o.OzonClient.Id));
 88        }
 89
 090        if (scope.WarehouseIds.Count > 0)
 91        {
 092            var ids = scope.WarehouseIds;
 093            var warehouseNames = _context.Warehouses
 094                .Where(w => ids.Contains(w.Id))
 095                .Select(w => w.Name)
 096                .ToList();
 97
 098            query = query.Where(o =>
 099                (o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id)) ||
 0100                (o.OrderExtensions != null && o.OrderExtensions.Any(e =>
 0101                    e.Key == "Засыл" && warehouseNames.Contains(e.Value))));
 102        }
 103
 0104        if (scope.DeliveryProviderIds.Count > 0)
 105        {
 0106            var ids = scope.DeliveryProviderIds;
 0107            query = query.Where(o => o.Delivery != null &&
 0108                                     o.Delivery.Provider != null &&
 0109                                     ids.Contains(o.Delivery.Provider.Id));
 110        }
 111
 0112        return query;
 113    }
 114
 115    /// <inheritdoc />
 116    public IEnumerable<Order> ApplyToOrdersList(IEnumerable<Order> orders, UserDataScope scope)
 117    {
 7118        if (scope.IsUnrestricted)
 1119            return orders;
 120
 6121        if (scope.AppStatusIds.Count > 0)
 122        {
 1123            var ids = scope.AppStatusIds;
 4124            orders = orders.Where(o => o.AppStatus != null && ids.Contains(o.AppStatus.Id));
 125        }
 126
 6127        if (scope.SupplierIds.Count > 0)
 128        {
 1129            var ids = scope.SupplierIds;
 4130            orders = orders.Where(o => o.Supplier != null && ids.Contains(o.Supplier.Id));
 131        }
 132
 6133        if (scope.OzonClientIds.Count > 0)
 134        {
 1135            var ids = scope.OzonClientIds;
 3136            orders = orders.Where(o => o.OzonClient != null && ids.Contains(o.OzonClient.Id));
 137        }
 138
 6139        if (scope.WarehouseIds.Count > 0)
 140        {
 2141            var ids = scope.WarehouseIds;
 142            // For in-memory filtering, derive warehouse names from loaded orders
 2143            var warehouseNames = orders
 4144                .Where(o => o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id))
 2145                .Select(o => o.ShipmentWarehouse!.Name)
 2146                .Distinct()
 2147                .ToHashSet();
 148
 2149            orders = orders.Where(o =>
 6150                (o.ShipmentWarehouse != null && ids.Contains(o.ShipmentWarehouse.Id)) ||
 6151                (o.OrderExtensions != null && o.OrderExtensions.Any(e =>
 7152                    e.Key == "Засыл" && warehouseNames.Contains(e.Value))));
 153        }
 154
 6155        if (scope.DeliveryProviderIds.Count > 0)
 156        {
 1157            var ids = scope.DeliveryProviderIds;
 4158            orders = orders.Where(o => o.Delivery != null &&
 4159                                       o.Delivery.Provider != null &&
 4160                                       ids.Contains(o.Delivery.Provider.Id));
 161        }
 162
 6163        return orders;
 164    }
 165}