• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 8911801772

01 May 2024 04:09PM UTC coverage: 57.379% (-1.8%) from 59.163%
8911801772

push

github

pwelter34
tweak build

342 of 700 branches covered (48.86%)

Branch coverage included in aggregate %.

1011 of 1658 relevant lines covered (60.98%)

18.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.5
/src/MediatR.CommandQuery/Behaviors/TenantFilterBehaviorBase.cs
1
using System.Security.Principal;
2

3
using MediatR.CommandQuery.Definitions;
4
using MediatR.CommandQuery.Queries;
5

6
using Microsoft.Extensions.Logging;
7

8
namespace MediatR.CommandQuery.Behaviors;
9

10
public abstract class TenantFilterBehaviorBase<TKey, TEntityModel, TRequest, TResponse>
11
    : PipelineBehaviorBase<TRequest, TResponse>
12
    where TEntityModel : class
13
    where TRequest : class, IRequest<TResponse>
14
{
15
    // ReSharper disable once StaticMemberInGenericType
16
    private static readonly Lazy<bool> _supportsTenant = new(SupportsTenant);
8✔
17

18

19
    protected TenantFilterBehaviorBase(ILoggerFactory loggerFactory, ITenantResolver<TKey> tenantResolver) : base(loggerFactory)
20✔
20
    {
21
        TenantResolver = tenantResolver ?? throw new ArgumentNullException(nameof(tenantResolver));
20!
22
    }
20✔
23

24
    protected ITenantResolver<TKey> TenantResolver { get; }
20✔
25

26

27
    protected virtual async Task<EntityFilter?> RewriteFilter(EntityFilter? originalFilter, IPrincipal? principal)
28
    {
29
        if (!_supportsTenant.Value)
20!
30
            return originalFilter;
×
31

32
        var tenantId = await TenantResolver.GetTenantId(principal).ConfigureAwait(false);
20✔
33
        if (Equals(tenantId, default(TKey)))
20!
34
            throw new DomainException(500, "Could not find tenant for the query request.");
×
35

36
        var tenantFilter = new EntityFilter
20✔
37
        {
20✔
38
            Name = nameof(IHaveTenant<TKey>.TenantId),
20✔
39
            Value = tenantId,
20✔
40
            Operator = EntityFilterOperators.Equal
20✔
41
        };
20✔
42

43
        if (originalFilter == null)
20!
44
            return tenantFilter;
×
45

46
        var boolFilter = new EntityFilter
20✔
47
        {
20✔
48
            Logic = EntityFilterLogic.And,
20✔
49
            Filters = new List<EntityFilter>
20✔
50
                {
20✔
51
                    tenantFilter,
20✔
52
                    originalFilter
20✔
53
                }
20✔
54
        };
20✔
55

56
        return boolFilter;
20✔
57
    }
20✔
58

59
    private static bool SupportsTenant()
60
    {
61
        var interfaceType = typeof(IHaveTenant<TKey>);
8✔
62
        var entityType = typeof(TEntityModel);
8✔
63

64
        return interfaceType.IsAssignableFrom(entityType);
8✔
65
    }
66

67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc