1
1
package io .unityfoundation .auth ;
2
2
3
- import io .micronaut .core .annotation .Introspected ;
4
3
import io .micronaut .core .annotation .Nullable ;
5
4
import io .micronaut .http .HttpResponse ;
6
5
import io .micronaut .http .HttpStatus ;
11
10
import io .micronaut .security .rules .SecurityRule ;
12
11
import io .micronaut .serde .annotation .Serdeable ;
13
12
import io .unityfoundation .auth .entities .*;
14
- import io .unityfoundation .auth .entities .Permission .PermissionScope ;
15
13
import io .unityfoundation .auth .entities .Service .ServiceStatus ;
16
14
import jakarta .validation .constraints .NotNull ;
17
15
import java .util .List ;
18
16
import java .util .Optional ;
19
- import java .util .function .BiPredicate ;
20
17
21
18
@ Secured (SecurityRule .IS_AUTHENTICATED )
22
19
@ Controller ("/api" )
@@ -26,12 +23,14 @@ public class AuthController {
26
23
private final ServiceRepo serviceRepo ;
27
24
private final TenantRepo tenantRepo ;
28
25
private final RoleRepo roleRepo ;
26
+ private final PermissionsService permissionsService ;
29
27
30
- public AuthController (UserRepo userRepo , ServiceRepo serviceRepo , TenantRepo tenantRepo , RoleRepo roleRepo ) {
28
+ public AuthController (UserRepo userRepo , ServiceRepo serviceRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PermissionsService permissionsService ) {
31
29
this .userRepo = userRepo ;
32
30
this .serviceRepo = serviceRepo ;
33
31
this .tenantRepo = tenantRepo ;
34
32
this .roleRepo = roleRepo ;
33
+ this .permissionsService = permissionsService ;
35
34
}
36
35
37
36
@ Post ("/principal/permissions" )
@@ -68,7 +67,7 @@ public UserPermissionsResponse permissions(@Body UserPermissionsRequest requestD
68
67
"The Tenant and/or Service is not available for this user" );
69
68
}
70
69
71
- return new UserPermissionsResponse .Success (getPermissionsFor (user , tenant ));
70
+ return new UserPermissionsResponse .Success (permissionsService . getPermissionsFor (user , tenant ));
72
71
}
73
72
74
73
@ Post ("/hasPermission" )
@@ -96,7 +95,7 @@ public HttpResponse<HasPermissionResponse> hasPermission(@Body HasPermissionRequ
96
95
return createHasPermissionResponse (false , user .getEmail (), "The requested service is not enabled for the requested tenant!" , List .of ());
97
96
}
98
97
99
- List <String > commonPermissions = checkUserPermission (user , tenantOptional .get (), requestDTO .permissions ());
98
+ List <String > commonPermissions = permissionsService . checkUserPermission (user , tenantOptional .get (), requestDTO .permissions ());
100
99
if (commonPermissions .isEmpty ()) {
101
100
return createHasPermissionResponse (false , user .getEmail (), "The user does not have permission!" , commonPermissions );
102
101
}
@@ -159,28 +158,6 @@ private String checkServiceStatus(Optional<Service> service) {
159
158
return null ;
160
159
}
161
160
162
- private final BiPredicate <TenantPermission , Tenant > isTenantOrSystemOrSubtenantScopeAndBelongsToTenant = (tp , t ) ->
163
- PermissionScope .SYSTEM .equals (tp .permissionScope ()) || (
164
- (PermissionScope .TENANT .equals (tp .permissionScope ())
165
- || PermissionScope .SUBTENANT .equals (tp .permissionScope ()))
166
- && tp .tenantId == t .getId ());
167
-
168
-
169
- private List <String > checkUserPermission (User user , Tenant tenant , List <String > permissions ) {
170
- List <String > commonPermissions = getPermissionsFor (user , tenant ).stream ()
171
- .filter (permissions ::contains ).toList ();
172
-
173
- return commonPermissions ;
174
- }
175
-
176
- private List <String > getPermissionsFor (User user , Tenant tenant ) {
177
- return userRepo .getTenantPermissionsFor (user .getId ()).stream ()
178
- .filter (tenantPermission ->
179
- isTenantOrSystemOrSubtenantScopeAndBelongsToTenant .test (tenantPermission , tenant ))
180
- .map (TenantPermission ::permissionName )
181
- .toList ();
182
- }
183
-
184
161
private HttpResponse <HasPermissionResponse > createHasPermissionResponse (boolean hasPermission ,
185
162
String userEmail ,
186
163
String message ,
@@ -209,14 +186,6 @@ public record HasPermissionResponse(
209
186
List <String > permissions
210
187
) {}
211
188
212
- @ Introspected
213
- public record TenantPermission (
214
- long tenantId ,
215
- String permissionName ,
216
- PermissionScope permissionScope
217
- ) {}
218
-
219
-
220
189
public sealed interface UserPermissionsResponse {
221
190
@ Serdeable
222
191
record Success (List <String > permissions ) implements UserPermissionsResponse {}
0 commit comments