@@ -26,12 +26,14 @@ public class UserController {
2626 private final TenantRepo tenantRepo ;
2727 private final RoleRepo roleRepo ;
2828 private final PasswordEncoder passwordEncoder ;
29+ private final PermissionsService permissionsService ;
2930
30- public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PasswordEncoder passwordEncoder ) {
31+ public UserController (UserRepo userRepo , TenantRepo tenantRepo , RoleRepo roleRepo , PasswordEncoder passwordEncoder , PermissionsService permissionsService ) {
3132 this .userRepo = userRepo ;
3233 this .tenantRepo = tenantRepo ;
3334 this .roleRepo = roleRepo ;
3435 this .passwordEncoder = passwordEncoder ;
36+ this .permissionsService = permissionsService ;
3537 }
3638
3739 @ Post
@@ -40,23 +42,31 @@ public HttpResponse<UserResponse> createUser(@Body AddUserRequest requestDTO,
4042
4143 Long requestTenantId = requestDTO .tenantId ();
4244
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. " );
4648 }
4749
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+ }
4954
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
5164 List <Long > rolesIntersection = getRolesIntersection (requestDTO .roles ());
5265
5366 // 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 ()))){
6070 // authenticated tenant admin user cannot grant unity admin role
6171 return HttpResponse .status (HttpStatus .FORBIDDEN ,
6272 "Authenticated user is not authorized to grant Unity Admin" );
@@ -97,30 +107,37 @@ public HttpResponse<UserResponse> createUser(@Body AddUserRequest requestDTO,
97107 public HttpResponse <UserResponse > updateUserRoles (@ PathVariable Long id , @ Body UpdateUserRolesRequest requestDTO ,
98108 Authentication authentication ) {
99109 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+ }
100114
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" );
104119 }
120+ User admin = adminOptional .get ();
105121
106122 Optional <User > userOptional = userRepo .findById (id );
107123 if (userOptional .isEmpty ()) {
108124 throw new HttpStatusException (HttpStatus .NOT_FOUND , "User not found" );
109125 }
110-
111126 User user = userOptional .get ();
112- Role unityAdministrator = roleRepo .findByName ("Unity Administrator" );
113127
114128 // ignore roles not defined by application
115129 List <Long > rolesIntersection = getRolesIntersection (requestDTO .roles ());
116130
117131 // 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 ()))){
124141 // authenticated tenant admin user cannot grant unity admin role
125142 return HttpResponse .status (HttpStatus .FORBIDDEN ,
126143 "Authenticated user is not authorized to grant Unity Admin" );
0 commit comments