6
6
import io .micronaut .http .HttpStatus ;
7
7
import io .micronaut .http .annotation .Body ;
8
8
import io .micronaut .http .annotation .Controller ;
9
- import io .micronaut .http .annotation .Get ;
10
9
import io .micronaut .http .annotation .Post ;
11
10
import io .micronaut .http .exceptions .HttpStatusException ;
12
11
import io .micronaut .security .annotation .Secured ;
@@ -41,19 +40,22 @@ public AuthController(UserRepo userRepo, ServiceRepo serviceRepo, TenantRepo ten
41
40
this .tenantRepo = tenantRepo ;
42
41
}
43
42
44
- @ Get ( " /permissions" )
45
- public HttpResponse < UserPermissionsResponse > permissions (@ Body UserPermissionsRequest requestDTO ,
43
+ @ Post ( "/principal /permissions" )
44
+ public UserPermissionsResponse permissions (@ Body UserPermissionsRequest requestDTO ,
46
45
Authentication authentication ) {
47
- Tenant tenant = tenantRepo .findById (requestDTO .tenantId ())
48
- .orElseThrow (() -> new HttpStatusException (HttpStatus .NOT_FOUND , "No tenant found." ));
46
+ Optional <Tenant > maybeTenant = tenantRepo .findById (requestDTO .tenantId ());
47
+ if (maybeTenant .isEmpty ()){
48
+ return new UserPermissionsResponse .Failure ("No tenant found." );
49
+ }
50
+ Tenant tenant = maybeTenant .get ();
49
51
50
52
if (!tenant .getStatus ().equals (TenantStatus .ENABLED )){
51
- throw new HttpStatusException ( HttpStatus . FORBIDDEN , "The tenant is not enabled." );
53
+ return new UserPermissionsResponse . Failure ( "The tenant is not enabled." );
52
54
}
53
55
54
56
User user = userRepo .findByEmail (authentication .getName ()).orElse (null );
55
57
if (checkUserStatus (user )) {
56
- throw new HttpStatusException ( HttpStatus . FORBIDDEN , "The user's account has been disabled." );
58
+ return new UserPermissionsResponse . Failure ( "The users account has been disabled." );
57
59
}
58
60
59
61
Service service = serviceRepo .findById (requestDTO .serviceId ())
@@ -62,11 +64,17 @@ public HttpResponse<UserPermissionsResponse> permissions(@Body UserPermissionsRe
62
64
if (service .getStatus () == ServiceStatus .DISABLED ) {
63
65
throw new HttpStatusException (HttpStatus .FORBIDDEN , "The service is disabled." );
64
66
} else if (service .getStatus () == ServiceStatus .DOWN_FOR_MAINTENANCE ) {
67
+
65
68
throw new HttpStatusException (HttpStatus .SERVICE_UNAVAILABLE ,
66
69
"The service is down for maintenance." );
67
70
}
68
71
69
- return HttpResponse .ok (new UserPermissionsResponse (getPermissionsFor (user , tenant )));
72
+ if (!userRepo .isServiceAvailable (user .getId (), service .getId ())) {
73
+ return new UserPermissionsResponse .Failure (
74
+ "The Tenant and/or Service is not available for this user" );
75
+ }
76
+
77
+ return new UserPermissionsResponse .Success (getPermissionsFor (user , tenant ));
70
78
}
71
79
72
80
@ Post ("/hasPermission" )
@@ -169,13 +177,18 @@ public record TenantPermission(
169
177
170
178
}
171
179
180
+
181
+ public sealed interface UserPermissionsResponse {
182
+ @ Serdeable
183
+ record Success (List <String > permissions ) implements UserPermissionsResponse {}
184
+ @ Serdeable
185
+ record Failure (String errorMessage ) implements UserPermissionsResponse {}
186
+ }
187
+
172
188
@ Serdeable
173
189
public record UserPermissionsRequest (@ NotNull Long tenantId ,
174
190
@ NotNull Long serviceId ) {
175
191
176
192
}
177
193
178
- @ Serdeable
179
- public record UserPermissionsResponse (List <String > permissions ){}
180
-
181
194
}
0 commit comments