2424import invite .provision .ProvisioningService ;
2525import invite .provision .scim .OperationType ;
2626import invite .repository .ApplicationRepository ;
27+ import invite .repository .InvitationRepository ;
2728import invite .repository .RoleRepository ;
2829import invite .repository .UserRepository ;
2930import org .apache .commons .logging .Log ;
@@ -68,6 +69,7 @@ public class CRMController {
6869 private final Map <String , CrmConfigEntry > crmConfig ;
6970 private final UserRoleAuditService userRoleAuditService ;
7071 private final Provisionable provisionable = () -> "SURF CRM" ;
72+ private final InvitationRepository invitationRepository ;
7173
7274
7375 @ SuppressWarnings ("unchecked" )
@@ -79,7 +81,8 @@ public CRMController(@Value("${crm.collab-person-prefix}") String collabPersonPr
7981 ProvisioningService provisioningService ,
8082 ObjectMapper objectMapper ,
8183 MailBox mailBox , Manage manage ,
82- UserRoleAuditService userRoleAuditService ) throws IOException {
84+ UserRoleAuditService userRoleAuditService ,
85+ InvitationRepository invitationRepository ) throws IOException {
8386 this .userRepository = userRepository ;
8487 this .collabPersonPrefix = collabPersonPrefix ;
8588 this .roleRepository = roleRepository ;
@@ -100,7 +103,8 @@ public CRMController(@Value("${crm.collab-person-prefix}") String collabPersonPr
100103 crmConfigEntry -> crmConfigEntry .code (),
101104 crmConfigEntry -> crmConfigEntry
102105 ));
103-
106+ LOG .info (String .format ("Parsed %s entries from %s" , this .crmConfig .size (), crmConfigResource .getDescription ()));
107+ this .invitationRepository = invitationRepository ;
104108 }
105109
106110 @ PostMapping ("" )
@@ -112,7 +116,7 @@ public ResponseEntity<String> contact(@RequestBody CRMContact crmContact) {
112116 if (crmContact .isSuppressInvitation ()) {
113117 if (!StringUtils .hasText (crmContact .getSchacHomeOrganisation ()) || !StringUtils .hasText (crmContact .getUid ())) {
114118 throw new InvalidInputException (
115- "Missing schacHomeOrganisation or uid in crmContact with sendInvitation false : " + crmContact );
119+ "Missing schacHomeOrganisation or uid in crmContact with isSuppressInvitation true : " + crmContact );
116120 }
117121 created = provisionUser (crmContact );
118122 } else {
@@ -126,8 +130,16 @@ public ResponseEntity<String> contact(@RequestBody CRMContact crmContact) {
126130 public ResponseEntity <String > delete (@ RequestBody CRMContact crmContact ) {
127131 LOG .debug ("DELETE /api/external/v1/crm: " + crmContact );
128132
129- List <User > users = userRepository .findByCrmContactId (crmContact .getContactId ());
130- users .forEach (user -> {
133+ List <Invitation > invitations = invitationRepository .findByCrmContactIdAndCrmOrganisationId (
134+ crmContact .getContactId (), crmContact .getOrganisation ().getOrganisationId ());
135+ invitations .forEach (invitation -> {
136+ LOG .info ("Deleting CRM invitation: " + invitation .getEmail ());
137+ this .invitationRepository .delete (invitation );
138+ });
139+
140+ Optional <User > userOptional = userRepository .findByCrmContactIdAndCrmOrganisationId (
141+ crmContact .getContactId (), crmContact .getOrganisation ().getOrganisationId ());
142+ userOptional .ifPresent (user -> {
131143 LOG .info ("Deleting CRM user: " + user .getEmail ());
132144 this .provisioningService .deleteUserRequest (user );
133145 this .userRepository .delete (user );
@@ -140,6 +152,9 @@ private boolean provisionUser(CRMContact crmContact) {
140152 String sub = constructSub (crmContact );
141153 Optional <User > optionalUser = userRepository .findBySubIgnoreCase (sub );
142154 User user = optionalUser .orElseGet (() -> createUser (crmContact , sub ));
155+ user .setCrmContactId (crmContact .getContactId ());
156+ user .setCrmOrganisationId (crmContact .getOrganisation ().getOrganisationId ());
157+
143158 List <CRMRole > newCrmRoles = syncCrmRoles (crmContact , user );
144159
145160 List <Role > roles = convertCrmRolesToInviteRoles (crmContact , newCrmRoles );
@@ -166,7 +181,6 @@ private User createUser(CRMContact crmContact, String sub) {
166181 crmContact .getFirstname (),
167182 StringUtils .hasText (middleName ) ? String .format ("%s %s" , middleName , surName ) : surName ,
168183 crmContact .getEmail ());
169- unsavedUser .setCrmContactId (crmContact .getContactId ());
170184 User user = userRepository .save (unsavedUser );
171185 this .provisioningService .newUserRequest (user );
172186 return user ;
@@ -177,6 +191,7 @@ private String constructSub(CRMContact crmContact) {
177191 }
178192
179193 private List <CRMRole > syncCrmRoles (CRMContact crmContact , User user ) {
194+ LOG .debug (String .format ("Start syncing crmRoles %s for user %s" , crmContact .getRoles (), user .getEmail ()));
180195 // Removes roles no longer present in CRM
181196 user .getUserRoles ().removeIf (userRole -> {
182197 Role role = userRole .getRole ();
@@ -192,15 +207,18 @@ private List<CRMRole> syncCrmRoles(CRMContact crmContact, User user) {
192207 .map (userRole -> userRole .getRole ())
193208 .toList ();
194209 // Return all the new CRM roles
195- return crmContact .getRoles ().stream ()
210+ List < CRMRole > crmRoles = crmContact .getRoles ().stream ()
196211 .filter (crmRole -> currentRoles .stream ()
197212 .noneMatch (role -> crmRole .getRoleId ().equalsIgnoreCase (role .getCrmRoleId ())))
198213 .toList ();
214+ LOG .debug (String .format ("Finished syncing crmRoles %s for user %s" , crmContact .getRoles (), user .getEmail ()));
215+ return crmRoles ;
199216 }
200217
201218 private boolean sendInvitation (CRMContact crmContact ) {
202- String sub = constructSub (crmContact );
203- Optional <User > optionalUser = userRepository .findBySubIgnoreCase (sub );
219+ Optional <User > optionalUser =
220+ userRepository .findByCrmContactIdAndCrmOrganisationId (
221+ crmContact .getContactId (), crmContact .getOrganisation ().getOrganisationId ());
204222 List <CRMRole > newCrmRoles = syncCrmRoles (crmContact , optionalUser .orElse (new User ()));
205223 //Only save the user when the user already existed
206224 optionalUser .ifPresent (user -> userRepository .save (user ));
@@ -213,7 +231,7 @@ private boolean sendInvitation(CRMContact crmContact) {
213231 .map (role -> new InvitationRole (role ))
214232 .collect (Collectors .toSet ());
215233 Invitation invitation = createInvitation (crmContact , invitationRoles );
216- invitation . setOrganizationGUID ( crmContact . getOrganisation (). getOrganisationId ());
234+
217235 Optional <String > idpName = identityProviderName (manage , invitation );
218236 mailBox .sendInviteMail (this .provisionable , invitation , groupedProviders , Language .en , idpName );
219237 }
@@ -257,13 +275,15 @@ private Role createRole(CRMOrganisation crmOrganisation, CRMRole crmRole) {
257275 unsavedRole .setCrmRoleName (crmConfigEntry .name ());
258276 unsavedRole .setCrmOrganisationId (crmOrganisation .getOrganisationId ());
259277 unsavedRole .setCrmOrganisationCode (crmOrganisation .getAbbrev ());
278+ unsavedRole .setOrganizationGUID (crmOrganisation .getOrganisationId ());
279+
260280 Role role = roleRepository .save (unsavedRole );
261281 this .provisioningService .newGroupRequest (role );
262282 return role ;
263283 }
264284
265285 private Invitation createInvitation (CRMContact crmContact , Set <InvitationRole > invitationRoles ) {
266- return new Invitation (
286+ Invitation invitation = new Invitation (
267287 Authority .GUEST ,
268288 HashGenerator .generateRandomHash (),
269289 crmContact .getEmail (),
@@ -279,6 +299,12 @@ private Invitation createInvitation(CRMContact crmContact, Set<InvitationRole> i
279299 invitationRoles ,
280300 null
281301 );
302+ String crmOrganisationId = crmContact .getOrganisation ().getOrganisationId ();
303+ invitation .setOrganizationGUID (crmOrganisationId );
304+ invitation .setCrmOrganisationId (crmOrganisationId );
305+ invitation .setCrmContactId (crmContact .getContactId ());
306+ invitationRepository .save (invitation );
307+ return invitation ;
282308
283309 }
284310}
0 commit comments