-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandDispatcherAuthorizer.cs
43 lines (34 loc) · 1.68 KB
/
CommandDispatcherAuthorizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using TNArch.Microservices.Core.Common.Command;
using TNArch.Microservices.Core.Common.DependencyInjection;
namespace TNArch.Microservices.Infrastructure.Common.Identity
{
[DecorateDependency(typeof(ICommandDispatcher), typeof(CommandDispatcher))]
public class CommandDispatcherAuthorizer : ICommandDispatcher
{
private readonly ICommandDispatcher _inner;
private readonly IIdentityService _identityService;
public CommandDispatcherAuthorizer(ICommandDispatcher inner, IIdentityService identityService)
{
_inner = inner;
_identityService = identityService;
}
public async Task<CommandResponse> DispatchCommand<TCommand>(TCommand command) where TCommand : ICommand
{
if (!await _identityService.HasPermission(command.Permission))
return new CommandResponse { IsAuthorized = false };
return await _inner.DispatchCommand(command);
}
public async Task<CommandResponse> DispatchCommand<TCommand, TResponse>(TCommand command) where TCommand : ICommand
{
if (!await _identityService.HasPermission(command.Permission))
return new CommandResponse { IsAuthorized = false };
return await _inner.DispatchCommand<TCommand, TResponse>(command);
}
public async Task<QueryResult<TResponse>> DispatchQuery<TQuery, TResponse>(TQuery query) where TQuery : IQuery
{
if (!await _identityService.HasPermission(query.Permission))
return new QueryResult<TResponse> { IsAuthorized = false };
return await _inner.DispatchQuery<TQuery, TResponse>(query);
}
}
}