3
3
import io .micronaut .core .annotation .Introspected ;
4
4
import io .micronaut .core .annotation .Nullable ;
5
5
import io .micronaut .http .HttpResponse ;
6
+ import io .micronaut .http .HttpStatus ;
6
7
import io .micronaut .http .annotation .Body ;
7
8
import io .micronaut .http .annotation .Controller ;
9
+ import io .micronaut .http .annotation .Get ;
8
10
import io .micronaut .http .annotation .Post ;
11
+ import io .micronaut .http .exceptions .HttpStatusException ;
9
12
import io .micronaut .security .annotation .Secured ;
10
13
import io .micronaut .security .authentication .Authentication ;
11
14
import io .micronaut .security .rules .SecurityRule ;
15
18
import io .unityfoundation .auth .entities .Service .ServiceStatus ;
16
19
import io .unityfoundation .auth .entities .ServiceRepo ;
17
20
import io .unityfoundation .auth .entities .Tenant ;
21
+ import io .unityfoundation .auth .entities .Tenant .TenantStatus ;
18
22
import io .unityfoundation .auth .entities .TenantRepo ;
19
23
import io .unityfoundation .auth .entities .User ;
20
24
import io .unityfoundation .auth .entities .UserRepo ;
25
+ import jakarta .validation .constraints .NotNull ;
21
26
import java .util .List ;
22
27
import java .util .Optional ;
28
+ import java .util .function .BiPredicate ;
23
29
24
30
@ Secured (SecurityRule .IS_AUTHENTICATED )
25
31
@ Controller ("/api" )
@@ -35,6 +41,34 @@ public AuthController(UserRepo userRepo, ServiceRepo serviceRepo, TenantRepo ten
35
41
this .tenantRepo = tenantRepo ;
36
42
}
37
43
44
+ @ Get ("/permissions" )
45
+ public HttpResponse <UserPermissionsResponse > permissions (@ Body UserPermissionsRequest requestDTO ,
46
+ Authentication authentication ) {
47
+ Tenant tenant = tenantRepo .findById (requestDTO .tenantId ())
48
+ .orElseThrow (() -> new HttpStatusException (HttpStatus .NOT_FOUND , "No tenant found." ));
49
+
50
+ if (!tenant .getStatus ().equals (TenantStatus .ENABLED )){
51
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The tenant is not enabled." );
52
+ }
53
+
54
+ User user = userRepo .findByEmail (authentication .getName ()).orElse (null );
55
+ if (checkUserStatus (user )) {
56
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The user's account has been disabled." );
57
+ }
58
+
59
+ Service service = serviceRepo .findById (requestDTO .serviceId ())
60
+ .orElseThrow (() -> new HttpStatusException (HttpStatus .NOT_FOUND , "Service not found." ));
61
+
62
+ if (service .getStatus () == ServiceStatus .DISABLED ) {
63
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The service is disabled." );
64
+ } else if (service .getStatus () == ServiceStatus .DOWN_FOR_MAINTENANCE ) {
65
+ throw new HttpStatusException (HttpStatus .SERVICE_UNAVAILABLE ,
66
+ "The service is down for maintenance." );
67
+ }
68
+
69
+ return HttpResponse .ok (new UserPermissionsResponse (getPermissionsFor (user , tenant )));
70
+ }
71
+
38
72
@ Post ("/hasPermission" )
39
73
public HttpResponse <HasPermissionResponse > hasPermission (@ Body HasPermissionRequest requestDTO ,
40
74
Authentication authentication ) {
@@ -87,20 +121,26 @@ private String checkServiceStatus(Optional<Service> service) {
87
121
return null ;
88
122
}
89
123
124
+ private final BiPredicate <TenantPermission , Tenant > isTenantOrSystemOrSubtenantScopeAndBelongsToTenant = (tp , t ) ->
125
+ PermissionScope .SYSTEM .equals (tp .permissionScope ()) || (
126
+ (PermissionScope .TENANT .equals (tp .permissionScope ())
127
+ || PermissionScope .SUBTENANT .equals (tp .permissionScope ()))
128
+ && tp .tenantId == t .getId ());
129
+
130
+
90
131
private List <String > checkUserPermission (User user , Tenant tenant , List <String > permissions ) {
91
- List <TenantPermission > userPermissions = userRepo .getTenantPermissionsFor (user .getId ()).stream ()
92
- .filter (tenantPermission ->
93
- PermissionScope .SYSTEM .equals (tenantPermission .permissionScope ()) ||
94
- ((PermissionScope .TENANT .equals (tenantPermission .permissionScope ()) || PermissionScope .SUBTENANT .equals (tenantPermission .permissionScope ()))
95
- && tenantPermission .tenantId == tenant .getId ()))
96
- .toList ();
132
+ List <String > commonPermissions = getPermissionsFor (user , tenant ).stream ()
133
+ .filter (permissions ::contains ).toList ();
97
134
98
- List <String > commonPermissions = userPermissions .stream ()
135
+ return commonPermissions ;
136
+ }
137
+
138
+ private List <String > getPermissionsFor (User user , Tenant tenant ) {
139
+ return userRepo .getTenantPermissionsFor (user .getId ()).stream ()
140
+ .filter (tenantPermission ->
141
+ isTenantOrSystemOrSubtenantScopeAndBelongsToTenant .test (tenantPermission , tenant ))
99
142
.map (TenantPermission ::permissionName )
100
- .filter (permissions ::contains )
101
143
.toList ();
102
-
103
- return commonPermissions ;
104
144
}
105
145
106
146
private HttpResponse <HasPermissionResponse > createHasPermissionResponse (boolean hasPermission ,
@@ -129,4 +169,13 @@ public record TenantPermission(
129
169
130
170
}
131
171
172
+ @ Serdeable
173
+ public record UserPermissionsRequest (@ NotNull Long tenantId ,
174
+ @ NotNull Long serviceId ) {
175
+
176
+ }
177
+
178
+ @ Serdeable
179
+ public record UserPermissionsResponse (List <String > permissions ){}
180
+
132
181
}
0 commit comments