1+ using Honamic . Framework . Applications . Authorizes ;
2+ using Honamic . Framework . Applications . Exceptions ;
3+ using Honamic . Framework . Commands ;
4+ using System . Reflection ;
5+
6+ namespace Honamic . Framework . Applications . CommandHandlerDecorators ;
7+
8+ public class AuthorizeCommandHandlerDecorator < TCommand > : ICommandHandler < TCommand >
9+ where TCommand : ICommand
10+ {
11+ private readonly ICommandHandler < TCommand > _commandHandler ;
12+ private readonly IAuthorization _authorization ;
13+
14+ public AuthorizeCommandHandlerDecorator ( ICommandHandler < TCommand > commandHandler , IAuthorization authorization )
15+ {
16+ _commandHandler = commandHandler ;
17+ _authorization = authorization ;
18+ }
19+
20+ public async Task HandleAsync ( TCommand command , CancellationToken cancellationToken )
21+ {
22+ await _authorization . AuthorizeCommandAttributes ( typeof ( TCommand ) ) ;
23+
24+ await _commandHandler . HandleAsync ( command , cancellationToken ) ;
25+ }
26+ }
27+
28+ public class AuthorizeCommandHandlerDecorator < TCommand , TResponse > : ICommandHandler < TCommand , TResponse >
29+ where TCommand : ICommand < TResponse >
30+ {
31+ private readonly ICommandHandler < TCommand , TResponse > _commandHandler ;
32+ private readonly IAuthorization _authorization ;
33+
34+ public AuthorizeCommandHandlerDecorator ( ICommandHandler < TCommand , TResponse > commandHandler , IAuthorization authorization )
35+ {
36+ _commandHandler = commandHandler ;
37+ _authorization = authorization ;
38+ }
39+
40+ public async Task < TResponse > HandleAsync ( TCommand command , CancellationToken cancellationToken )
41+ {
42+ await _authorization . AuthorizeCommandAttributes ( typeof ( TCommand ) ) ;
43+
44+ return await _commandHandler . HandleAsync ( command , cancellationToken ) ;
45+ }
46+ }
47+
48+ internal static class AuthorizeCommandHandlerDecoratorHelper
49+ {
50+
51+ public static async Task AuthorizeCommandAttributes ( this IAuthorization authorization , Type type )
52+ {
53+ await authorization . AuthorizeWithAttributes ( type ) ;
54+
55+ await authorization . AuthorizeWithDynamicPermissions ( type ) ;
56+ }
57+
58+ public static async Task AuthorizeWithDynamicPermissions ( this IAuthorization authorization , Type type )
59+ {
60+ var dynamicAuthorizeAttribute = type . GetCustomAttribute < DynamicAuthorizeAttribute > ( ) ;
61+
62+ if ( dynamicAuthorizeAttribute is not null )
63+ {
64+ if ( ! authorization . IsAuthenticated ( ) )
65+ {
66+ throw new UnauthenticatedException ( ) ;
67+ }
68+
69+ string dynamicPermission = CalculatePermissionName ( type ) ;
70+
71+ if ( ! await authorization . HaveAccessAsync ( dynamicPermission ) )
72+ {
73+ throw new UnauthorizedException ( dynamicPermission ) ;
74+ }
75+ }
76+ }
77+
78+ public static async Task AuthorizeWithAttributes ( this IAuthorization authorization , Type type )
79+ {
80+ var authorizeAttribute = type . GetCustomAttribute < AuthorizeAttribute > ( ) ;
81+
82+ if ( authorizeAttribute is not null )
83+ {
84+ if ( ! authorization . IsAuthenticated ( ) )
85+ {
86+ throw new UnauthenticatedException ( ) ;
87+ }
88+
89+ if ( authorizeAttribute . Permissions ? . Length > 0 )
90+ {
91+ foreach ( var permission in authorizeAttribute . Permissions )
92+ {
93+ if ( ! await authorization . HaveAccessAsync ( permission ) )
94+ {
95+ throw new UnauthorizedException ( permission ) ;
96+ }
97+ }
98+ }
99+ }
100+ }
101+
102+ private static string CalculatePermissionName ( Type type )
103+ {
104+ return type . Name ;
105+ }
106+ }
0 commit comments