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

loresoft / MediatR.CommandQuery / 11418431610

19 Oct 2024 02:43PM UTC coverage: 58.337% (+0.9%) from 57.424%
11418431610

push

github

pwelter34
add HybridCache, misc refactors

331 of 675 branches covered (49.04%)

Branch coverage included in aggregate %.

28 of 176 new or added lines in 24 files covered. (15.91%)

156 existing lines in 33 files now uncovered.

1163 of 1886 relevant lines covered (61.66%)

19.49 hits per line

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

79.31
/src/MediatR.CommandQuery/Behaviors/TenantAuthenticateCommandBehavior.cs
1
using System.Net;
2

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

6
using Microsoft.Extensions.Logging;
7

8
namespace MediatR.CommandQuery.Behaviors;
9

10
public class TenantAuthenticateCommandBehavior<TKey, TEntityModel, TResponse>
11
    : PipelineBehaviorBase<EntityModelCommand<TEntityModel, TResponse>, TResponse>
12
    where TEntityModel : class
13
{
14

15
    private readonly ITenantResolver<TKey> _tenantResolver;
16

17
    public TenantAuthenticateCommandBehavior(ILoggerFactory loggerFactory, ITenantResolver<TKey> tenantResolver)
18
        : base(loggerFactory)
24✔
19
    {
20
        _tenantResolver = tenantResolver ?? throw new ArgumentNullException(nameof(tenantResolver));
24!
21
    }
24✔
22

23
    protected override async Task<TResponse> Process(
24
        EntityModelCommand<TEntityModel, TResponse> request,
25
        RequestHandlerDelegate<TResponse> next,
26
        CancellationToken cancellationToken)
27
    {
28
        ArgumentNullException.ThrowIfNull(request);
24✔
29
        ArgumentNullException.ThrowIfNull(next);
24✔
30

31
        await Authorize(request).ConfigureAwait(false);
24✔
32

33
        // continue pipeline
34
        return await next().ConfigureAwait(false);
20✔
35
    }
20✔
36

37
    private async Task Authorize(EntityModelCommand<TEntityModel, TResponse> request)
38
    {
39
        var principal = request.Principal;
24✔
40
        if (principal == null)
24!
UNCOV
41
            return;
×
42

43
        // check principal tenant is same as model tenant
44
        if (request.Model is not IHaveTenant<TKey> tenantModel)
24!
UNCOV
45
            return;
×
46

47
        var tenantId = await _tenantResolver.GetTenantId(principal);
24✔
48
        if (Equals(tenantId, tenantModel.TenantId))
24✔
49
            return;
20✔
50

51
        Logger.LogError("User {UserName} does not have access to specified tenant: {TenantId}", principal.Identity?.Name, tenantId);
4!
52

53
        throw new DomainException(HttpStatusCode.Forbidden, "User does not have access to specified tenant.");
4✔
54
    }
20✔
55
}
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

© 2026 Coveralls, Inc