@@ -26,12 +26,14 @@ public class UserController {
26
26
private final TenantRepo tenantRepo ;
27
27
private final RoleRepo roleRepo ;
28
28
private final PasswordEncoder passwordEncoder ;
29
+ private final PermissionsService permissionsService ;
29
30
30
- public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PasswordEncoder passwordEncoder ) {
31
+ public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PasswordEncoder passwordEncoder , PermissionsService permissionsService ) {
31
32
this .userRepo = userRepo ;
32
33
this .tenantRepo = tenantRepo ;
33
34
this .roleRepo = roleRepo ;
34
35
this .passwordEncoder = passwordEncoder ;
36
+ this .permissionsService = permissionsService ;
35
37
}
36
38
37
39
@ Post
@@ -40,23 +42,31 @@ public HttpResponse<UserResponse> createUser(@Body AddUserRequest requestDTO,
40
42
41
43
Long requestTenantId = requestDTO .tenantId ();
42
44
43
- // reject if the declared tenant does not exist
44
- if (! tenantRepo . existsById ( requestTenantId )) {
45
- throw new HttpStatusException (HttpStatus .NOT_FOUND , "Tenant not found" );
45
+ Optional < Tenant > tenantOptional = tenantRepo . findById ( requestTenantId );
46
+ if (tenantOptional . isEmpty ( )) {
47
+ throw new HttpStatusException (HttpStatus .NOT_FOUND , "Tenant not found. " );
46
48
}
47
49
48
- Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
50
+ Optional <User > adminOptional = userRepo .findByEmail (authentication .getName ());
51
+ if (adminOptional .isEmpty ()) {
52
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The user is disabled." );
53
+ }
49
54
50
- // ignore roles not defined by application
55
+ User admin = adminOptional .get ();
56
+
57
+ List <String > commonPermissions = permissionsService .checkUserPermission (admin , tenantOptional .get (),
58
+ List .of ("AUTH_SERVICE_EDIT-SYSTEM" , "AUTH_SERVICE_EDIT-TENANT" ));
59
+ if (commonPermissions .isEmpty ()) {
60
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The user does not have permission!" );
61
+ }
62
+
63
+ // ignore roles not defined by system
51
64
List <Long > rolesIntersection = getRolesIntersection (requestDTO .roles ());
52
65
53
66
// reject if caller is not a unity nor tenant admin of the declared tenant
54
- String authUserEmail = authentication .getName ();
55
- if (!userRepo .existsByEmailAndRoleEqualsUnityAdmin (authUserEmail )) {
56
- if (!userRepo .existsByEmailAndTenantEqualsAndIsTenantAdmin (authUserEmail , requestTenantId )) {
57
- return HttpResponse .status (HttpStatus .FORBIDDEN ,
58
- "Authenticated user is not authorized to make changes under declared tenant." );
59
- } else if (rolesIntersection .stream ().anyMatch (roleId -> roleId .equals (unityAdministrator .getId ()))){
67
+ if (!commonPermissions .contains ("AUTH_SERVICE_EDIT-SYSTEM" )) {
68
+ Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
69
+ if (rolesIntersection .stream ().anyMatch (roleId -> roleId .equals (unityAdministrator .getId ()))){
60
70
// authenticated tenant admin user cannot grant unity admin role
61
71
return HttpResponse .status (HttpStatus .FORBIDDEN ,
62
72
"Authenticated user is not authorized to grant Unity Admin" );
@@ -97,30 +107,37 @@ public HttpResponse<UserResponse> createUser(@Body AddUserRequest requestDTO,
97
107
public HttpResponse <UserResponse > updateUserRoles (@ PathVariable Long id , @ Body UpdateUserRolesRequest requestDTO ,
98
108
Authentication authentication ) {
99
109
Long requestTenantId = requestDTO .tenantId ();
110
+ Optional <Tenant > tenantOptional = tenantRepo .findById (requestTenantId );
111
+ if (tenantOptional .isEmpty ()) {
112
+ throw new HttpStatusException (HttpStatus .NOT_FOUND , "Tenant not found." );
113
+ }
100
114
101
- // reject if the declared tenant does not exist
102
- if (!tenantRepo .existsById (requestTenantId )) {
103
- throw new HttpStatusException (HttpStatus .NOT_FOUND , "Tenant not found" );
115
+ String authUserEmail = authentication .getName ();
116
+ Optional <User > adminOptional = userRepo .findByEmail (authUserEmail );
117
+ if (adminOptional .isEmpty ()) {
118
+ throw new HttpStatusException (HttpStatus .NOT_FOUND , "Authenticated user does not exist" );
104
119
}
120
+ User admin = adminOptional .get ();
105
121
106
122
Optional <User > userOptional = userRepo .findById (id );
107
123
if (userOptional .isEmpty ()) {
108
124
throw new HttpStatusException (HttpStatus .NOT_FOUND , "User not found" );
109
125
}
110
-
111
126
User user = userOptional .get ();
112
- Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
113
127
114
128
// ignore roles not defined by application
115
129
List <Long > rolesIntersection = getRolesIntersection (requestDTO .roles ());
116
130
117
131
// if unity admin, proceed; otherwise, reject if roles exceed authenticated user's under same tenant.
118
- String authUserEmail = authentication .getName ();
119
- if (!userRepo .existsByEmailAndRoleEqualsUnityAdmin (authUserEmail )) {
120
- if (!userRepo .existsByEmailAndTenantEqualsAndIsTenantAdmin (authUserEmail , requestTenantId )) {
121
- return HttpResponse .status (HttpStatus .FORBIDDEN ,
122
- "Authenticated user is not authorized to make changes under declared tenant." );
123
- } else if (rolesIntersection .stream ().anyMatch (roleId -> roleId .equals (unityAdministrator .getId ()))){
132
+ List <String > commonPermissions = permissionsService .checkUserPermission (admin , tenantOptional .get (),
133
+ List .of ("AUTH_SERVICE_EDIT-SYSTEM" , "AUTH_SERVICE_EDIT-TENANT" ));
134
+ if (commonPermissions .isEmpty ()) {
135
+ throw new HttpStatusException (HttpStatus .FORBIDDEN , "The user does not have permission!" );
136
+ }
137
+
138
+ if (!commonPermissions .contains ("AUTH_SERVICE_VIEW-SYSTEM" )) {
139
+ Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
140
+ if (rolesIntersection .stream ().anyMatch (roleId -> roleId .equals (unityAdministrator .getId ()))){
124
141
// authenticated tenant admin user cannot grant unity admin role
125
142
return HttpResponse .status (HttpStatus .FORBIDDEN ,
126
143
"Authenticated user is not authorized to grant Unity Admin" );
0 commit comments