1
1
using System . Text . Json ;
2
2
using AdminHubApi . Dtos . ApiResponse ;
3
+ using AdminHubApi . Security . Permissions ;
3
4
using Microsoft . AspNetCore . Authorization ;
4
5
using Microsoft . AspNetCore . Authorization . Policy ;
5
6
6
- namespace AdminHubApi . Security
7
+ namespace AdminHubApi . Security ;
8
+
9
+ public class CustomAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
7
10
{
8
- public class CustomAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
11
+ private readonly AuthorizationMiddlewareResultHandler _defaultHandler =
12
+ new AuthorizationMiddlewareResultHandler ( ) ;
13
+
14
+ private readonly IPermissionMessageService _permissionMessageService ;
15
+
16
+ public CustomAuthorizationMiddlewareResultHandler ( IPermissionMessageService permissionMessageService )
9
17
{
10
- private readonly AuthorizationMiddlewareResultHandler _defaultHandler =
11
- new AuthorizationMiddlewareResultHandler ( ) ;
12
-
13
- public async Task HandleAsync (
14
- RequestDelegate next ,
15
- HttpContext context ,
16
- AuthorizationPolicy policy ,
17
- PolicyAuthorizationResult authorizeResult )
18
+ _permissionMessageService = permissionMessageService ;
19
+ }
20
+
21
+ public async Task HandleAsync (
22
+ RequestDelegate next ,
23
+ HttpContext context ,
24
+ AuthorizationPolicy policy ,
25
+ PolicyAuthorizationResult authorizeResult )
26
+ {
27
+ // If the authorization was successful, continue down the pipeline
28
+ if ( authorizeResult . Succeeded )
18
29
{
19
- // If the authorization was successful, continue down the pipeline
20
- if ( authorizeResult . Succeeded )
21
- {
22
- await _defaultHandler . HandleAsync ( next , context , policy , authorizeResult ) ;
30
+ await _defaultHandler . HandleAsync ( next , context , policy , authorizeResult ) ;
23
31
24
- return ;
25
- }
32
+ return ;
33
+ }
26
34
27
- // Check if unauthorized or forbidden
28
- if ( context . User . Identity ? . IsAuthenticated != true )
29
- {
30
- // Return a 401 Unauthorized with a descriptive message
31
- context . Response . StatusCode = StatusCodes . Status401Unauthorized ;
35
+ // Check if unauthorized or forbidden
36
+ if ( context . User . Identity ? . IsAuthenticated != true )
37
+ {
38
+ // Return a 401 Unauthorized with a descriptive message
39
+ context . Response . StatusCode = StatusCodes . Status401Unauthorized ;
32
40
33
- var response = new ApiResponse < object >
34
- {
35
- Succeeded = false ,
36
- Message = "Authentication required" ,
37
- Errors = new List < string > ( )
38
- {
39
- "You need to authenticate to access this resource."
40
- }
41
- } ;
42
-
43
- await WriteJsonResponseAsync ( context , response ) ;
44
- }
45
- else
41
+ var response = new ApiResponse < object >
46
42
{
47
- // Return a 403 Forbidden with a descriptive message
48
- context . Response . StatusCode = StatusCodes . Status403Forbidden ;
49
-
50
- // Check if it's related to project permissions
51
- if ( context . Request . Path . StartsWithSegments ( "/api/projects" ) )
43
+ Succeeded = false ,
44
+ Message = "Authentication required" ,
45
+ Errors = new List < string > ( )
52
46
{
53
- // Customize message based on the HTTP method
54
- string errorMessage = context . Request . Method switch
55
- {
56
- "POST" => "You do not have sufficient permissions to create a project." ,
57
- "PUT" => "You do not have sufficient permissions to edit a project." ,
58
- "DELETE" => "You do not have sufficient permissions to delete a project." ,
59
- _ => "You do not have sufficient permissions to perform this action on projects."
60
- } ;
61
-
62
- var response = new ApiResponse < object >
63
- {
64
- Succeeded = false ,
65
- Message = "Permission denied" ,
66
- Errors = new List < string > ( )
67
- {
68
- errorMessage
69
- }
70
- } ;
71
-
72
- await WriteJsonResponseAsync ( context , response ) ;
47
+ "You need to authenticate to access this resource."
73
48
}
74
- else
75
- {
76
- // Generic permission error
77
- var response = new ApiResponse < object >
78
- {
79
- Succeeded = false ,
80
- Message = "Permission denied" ,
81
- Errors = new List < string > ( )
82
- {
83
- "You do not have sufficient permissions to perform this action."
84
- }
85
- } ;
86
-
87
- await WriteJsonResponseAsync ( context , response ) ;
88
- }
89
- }
90
- }
49
+ } ;
91
50
92
- private static async Task WriteJsonResponseAsync < T > ( HttpContext context , ApiResponse < T > response )
51
+ await WriteJsonResponseAsync ( context , response ) ;
52
+ }
53
+ else
93
54
{
94
- context . Response . ContentType = "application/json" ;
55
+ // Return a 403 Forbidden with a descriptive message
56
+ context . Response . StatusCode = StatusCodes . Status403Forbidden ;
57
+
58
+ // Get the path and HTTP method
59
+ string path = context . Request . Path . Value ?? "" ;
60
+ string method = context . Request . Method ;
61
+
62
+ // Get a specific error message for the resource and action
63
+ string errorMessage = _permissionMessageService . GetPermissionMessage ( path , method ) ;
95
64
96
- var options = new JsonSerializerOptions
65
+ var response = new ApiResponse < object >
97
66
{
98
- PropertyNamingPolicy = JsonNamingPolicy . CamelCase
67
+ Succeeded = false ,
68
+ Message = "Permission denied" ,
69
+ Errors = new List < string > ( ) { errorMessage }
99
70
} ;
100
71
101
- await JsonSerializer . SerializeAsync ( context . Response . Body , response , options ) ;
72
+ await WriteJsonResponseAsync ( context , response ) ;
102
73
}
103
74
}
75
+
76
+ private static async Task WriteJsonResponseAsync < T > ( HttpContext context , ApiResponse < T > response )
77
+ {
78
+ context . Response . ContentType = "application/json" ;
79
+
80
+ var options = new JsonSerializerOptions
81
+ {
82
+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase
83
+ } ;
84
+
85
+ await JsonSerializer . SerializeAsync ( context . Response . Body , response , options ) ;
86
+ }
104
87
}
0 commit comments