1919import invite .provision .eva .EvaClient ;
2020import invite .provision .graph .GraphClient ;
2121import invite .provision .graph .GraphResponse ;
22+ import invite .provision .scim .DisplayNameOperation ;
2223import invite .provision .scim .GroupPatchRequest ;
2324import invite .provision .scim .GroupRequest ;
2425import invite .provision .scim .GroupURN ;
2526import invite .provision .scim .Member ;
26- import invite .provision .scim .Operation ;
27+ import invite .provision .scim .MembersOperation ;
2728import invite .provision .scim .OperationType ;
2829import invite .provision .scim .UserRequest ;
2930import invite .repository .RemoteProvisionedGroupRepository ;
4445import org .springframework .http .MediaType ;
4546import org .springframework .http .RequestEntity ;
4647import org .springframework .http .ResponseEntity ;
47- import org .springframework .http .client .ClientHttpRequestInterceptor ;
4848import org .springframework .http .client .JdkClientHttpRequestFactory ;
49- import org .springframework .retry .support .RetryTemplate ;
5049import org .springframework .stereotype .Service ;
5150import org .springframework .util .StringUtils ;
5251import org .springframework .web .client .RestClientException ;
6463import java .util .Set ;
6564import java .util .concurrent .atomic .AtomicReference ;
6665import java .util .stream .Collectors ;
67- import java .util .stream .Stream ;
6866
6967@ Service
7068@ SuppressWarnings ("unchecked" )
@@ -228,7 +226,7 @@ public void deleteUserRoleRequest(UserRole userRole) {
228226 public void deleteUserRequest (User user ) {
229227 //First send update role requests
230228 user .getUserRoles ()
231- .forEach (userRole -> this .updateGroupRequest (userRole , OperationType .Remove ));
229+ .forEach (userRole -> this .updateGroupRequest (userRole , OperationType .remove ));
232230
233231 List <Provisioning > provisionings = getProvisionings (user );
234232 //Delete the user to all provisionings in Manage where the user is known
@@ -238,7 +236,7 @@ public void deleteUserRequest(User user) {
238236 @ Override
239237 public void deleteUserRequest (User user , UserRole userRole ) {
240238 //First send update role request
241- this .updateGroupRequest (userRole , OperationType .Remove );
239+ this .updateGroupRequest (userRole , OperationType .remove );
242240 /*
243241 * We first need a List all provisionings for the user#userRole, and then we need to remove the provisiongs
244242 * from that List that are in use by other user#userRoles, and those are the provisionings which we need to delete
@@ -343,9 +341,9 @@ public void updateGroupRequest(UserRole userRole, OperationType operationType) {
343341 .filter (userRoleDB -> userRoleDB .getAuthority ().equals (Authority .GUEST ) || userRoleDB .isGuestRoleIncluded ())
344342 .collect (Collectors .toCollection (ArrayList ::new ));
345343 boolean userRolePresent = userRoles .stream ().anyMatch (dbUserRole -> dbUserRole .getId ().equals (userRole .getId ()));
346- if (operationType .equals (OperationType .Add ) && !userRolePresent ) {
344+ if (operationType .equals (OperationType .add ) && !userRolePresent ) {
347345 userRoles .add (userRole );
348- } else if (operationType .equals (OperationType .Remove ) && userRolePresent ) {
346+ } else if (operationType .equals (OperationType .remove ) && userRolePresent ) {
349347 userRoles = userRoles .stream ()
350348 .filter (dbUserRole -> !dbUserRole .getId ().equals (userRole .getId ()))
351349 .collect (Collectors .toCollection (ArrayList ::new ));
@@ -381,31 +379,30 @@ private void sendGroupPutRequest(Provisioning provisioning,
381379 .map (Optional ::get )
382380 .map (RemoteProvisionedUser ::getRemoteIdentifier )
383381 .toList ();
384- if (!userScimIdentifiers .isEmpty ()) {
382+ if (!userScimIdentifiers .isEmpty () || operationType . equals ( OperationType . replace ) ) {
385383 if (provisioning .isScimUpdateRolePutMethod ()) {
386384 String groupRequest = constructGroupRequest (
387385 role ,
388386 provisionedGroup .getRemoteIdentifier (),
389387 userScimIdentifiers );
390388 this .updateRequest (provisioning , groupRequest , APIType .GROUP_API , provisionedGroup .getRemoteIdentifier (), HttpMethod .PUT );
391389 } else {
392- String groupRequest = patchGroupRequest (
393- role ,
394- userScimIdentifiers ,
395- provisionedGroup .getRemoteIdentifier (),
396- operationType );
390+ GroupPatchRequest request = operationType .equals (OperationType .replace ) ?
391+ new GroupPatchRequest (new DisplayNameOperation (role .getName ())):
392+ new GroupPatchRequest (new MembersOperation (operationType , userScimIdentifiers ));
393+ String groupRequest = prettyJson (request );
397394 this .updateRequest (provisioning , groupRequest , APIType .GROUP_API , provisionedGroup .getRemoteIdentifier (), HttpMethod .PATCH );
398395 }
399396 }
400-
401397 }
402398
403399 @ Override
404400 public void updateGroupRequest (List <String > previousManageIdentifiers , Role newRole , boolean nameChanged ) {
405401 //Immutable List cannot be sorted
406402 List <String > previousManageIdentifiersSorted = previousManageIdentifiers .stream ().sorted ().toList ();
407403 List <String > newManageIdentifiers = this .getManageIdentifiers (newRole );
408- if (!nameChanged && previousManageIdentifiers .equals (newManageIdentifiers )) {
404+ boolean noApplicationsChanged = previousManageIdentifiers .equals (newManageIdentifiers );
405+ if (!nameChanged && noApplicationsChanged ) {
409406 LOG .info (String .format ("Group %s update request with no difference in manage identifiers (%s). No action required" ,
410407 newRole .getName (),
411408 newManageIdentifiers ));
@@ -436,13 +433,14 @@ public void updateGroupRequest(List<String> previousManageIdentifiers, Role newR
436433 }
437434 provisionedGroupOptional .ifPresent (provisionedGroup -> {
438435 List <UserRole > userRoles = userRoleRepository .findByRole (newRole );
439- this .sendGroupPutRequest (provisioning , provisionedGroup , userRoles , newRole , OperationType .Add );
436+ this .sendGroupPutRequest (provisioning , provisionedGroup , userRoles , newRole , OperationType .replace );
440437 });
441438 });
442439
443440 LOG .info (String .format ("Deleting existing provisionings %s from group %s" , deletedManageIdentifiers , newRole .getName ()));
444441
445- List <Provisioning > provisionings = manage .provisioning (deletedManageIdentifiers ).stream ().map (Provisioning ::new ).toList ();
442+ List <Provisioning > provisionings = manage .provisioning (deletedManageIdentifiers ).stream ()
443+ .map (Provisioning ::new ).toList ();
446444 deleteGroupRequest (newRole , provisionings );
447445 }
448446
@@ -488,16 +486,6 @@ private String constructGroupRequest(Role role, String remoteGroupScimIdentifier
488486 return prettyJson (new GroupRequest (externalId , remoteGroupScimIdentifier , role .getName (), members ));
489487 }
490488
491- private String patchGroupRequest (Role role ,
492- List <String > remoteScimProvisionedUsers ,
493- String remoteScimProvisionedGroup ,
494- OperationType operationType ) {
495- String externalId = GroupURN .urnFromRole (groupUrnPrefix , role );
496- GroupPatchRequest request = new GroupPatchRequest (externalId , remoteScimProvisionedGroup ,
497- new Operation (operationType , remoteScimProvisionedUsers ));
498- return prettyJson (request );
499- }
500-
501489 private Optional <ProvisioningResponse > newRequest (Provisioning provisioning , String request , Provisionable provisionable ) {
502490 boolean isUser = provisionable instanceof User ;
503491 APIType apiType = isUser ? APIType .USER_API : APIType .GROUP_API ;
0 commit comments