Skip to content

Commit 5a09493

Browse files
committed
WIP for #699
1 parent cc8f0f8 commit 5a09493

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

server/src/main/java/invite/audit/UserRoleAuditService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public UserRoleAuditService(UserRoleAuditRepository userRoleAuditRepository) {
1616
this.userRoleAuditRepository = userRoleAuditRepository;
1717
}
1818

19-
2019
public void logAction(UserRole userRole, UserRoleAudit.ActionType action) {
2120
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
2221
String authority = authentication != null ? authentication.getName() : "system";

server/src/main/java/invite/crm/CRMController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.util.NoSuchElementException;
6565
import java.util.Optional;
6666
import java.util.Set;
67+
import java.util.function.Predicate;
6768
import java.util.stream.Collectors;
6869

6970
import static invite.SwaggerOpenIdConfig.API_HEADER_SCHEME_NAME;
@@ -342,6 +343,34 @@ public ResponseEntity<ResendInvitationResponse> resendInvitation(@RequestBody Re
342343
return ResponseEntity.ok(new ResendInvitationResponse(timestamp, 200, "ok", "Resend invitation"));
343344
}
344345

346+
@PostMapping(value = "/crm/api/v1/invite/remove", produces = MediaType.APPLICATION_JSON_VALUE)
347+
@Operation(summary = "Remove all CRM roles memberships from a user",
348+
description = "Remove all CRM roles memberships from a user")
349+
@SecurityRequirement(name = API_HEADER_SCHEME_NAME)
350+
@PreAuthorize("hasRole('CRM')")
351+
public ResponseEntity<String> remove(@RequestBody RemoveRoles removeRoles) {
352+
LOG.debug("POST /crm/api/v1/invite/remove: " + removeRoles);
353+
354+
Optional<Organisation> optionalOrganisation = organisationRepository.findByCrmOrganisationId(removeRoles.crmOrganisationId());
355+
optionalOrganisation
356+
.flatMap(organisation -> userRepository
357+
.findByCrmContactIdAndOrganisation(removeRoles.crmContatcId(), organisation))
358+
.ifPresent(user -> {
359+
LOG.debug("Removing roles from CRM user: " + user.getEmail());
360+
361+
Predicate<UserRole> predicate = userRole -> StringUtils.hasText(userRole.getRole().getCrmRoleId());
362+
user.getUserRoles().stream()
363+
.filter(predicate)
364+
.forEach(userRole -> {
365+
this.provisioningService.deleteUserRoleRequest(userRole);
366+
this.userRoleAuditService.logAction(userRole, UserRoleAudit.ActionType.DELETE);
367+
});
368+
user.getUserRoles().removeIf(predicate);
369+
this.userRepository.save(user);
370+
});
371+
return ResponseEntity.ok().body("removed");
372+
}
373+
345374
@GetMapping(value = "/crm/api/v1/organisations", produces = MediaType.APPLICATION_JSON_VALUE)
346375
@Operation(summary = "Get all CRM organisations")
347376
@SecurityRequirement(name = API_HEADER_SCHEME_NAME)

server/src/main/java/invite/provision/ProvisioningServiceDefault.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private enum APIType {
8686

8787
private final ParameterizedTypeReference<String> stringParameterizedTypeReference = new ParameterizedTypeReference<>() {
8888
};
89+
8990
private final RestTemplate restTemplate = new RestTemplate();
9091

9192
private final UserRoleRepository userRoleRepository;
@@ -606,7 +607,7 @@ private <T, S> T doExchange(RequestEntity<S> requestEntity,
606607
provisioning.getEntityId()));
607608

608609
return body;
609-
} catch (RestClientException e) {
610+
} catch (RuntimeException e) {
610611
String errorMessage = String.format("Error %s SCIM request (entityID %s) to %s with %s httpMethod and body %s",
611612
api,
612613
provisioning.getEntityId(),

server/src/test/java/invite/crm/CRMControllerTest.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void contactProvisioningNewUser() throws JsonProcessingException {
8686
}
8787

8888
@Test
89-
void contactProvisioningWrongApiHeader() throws JsonProcessingException {
89+
void contactProvisioningWrongApiHeader() {
9090
given()
9191
.when()
9292
.accept(ContentType.JSON)
@@ -759,7 +759,7 @@ void profileWithOrgGUID() {
759759
assertEquals(1, profileResponse.profiles().size());
760760
}
761761

762-
@Test
762+
@Test
763763
void profileWithGuidRole() {
764764
this.seedCRMData();
765765
ProfileResponse profileResponse = given()
@@ -877,6 +877,66 @@ void deleteOrganisation() {
877877
assertTrue(optionalOrganisation.isEmpty());
878878
}
879879

880+
@Test
881+
void removeCRMRoles() throws JsonProcessingException {
882+
this.seedCRMData();
883+
884+
Organisation organisation = organisationRepository.findByCrmOrganisationId(CRM_ORGANIZATION_ID).get();
885+
User user = userRepository.findByCrmContactIdAndOrganisation(CRM_CONTACT_ID, organisation).get();
886+
assertEquals(2, user.getUserRoles().size());
887+
888+
stubForManageProvisioning(List.of());
889+
890+
String response = given()
891+
.when()
892+
.accept(ContentType.JSON)
893+
.header(API_KEY_HEADER, "secret")
894+
.contentType(ContentType.JSON)
895+
.body(new RemoveRoles(CRM_ORGANIZATION_ID, CRM_CONTACT_ID))
896+
.post("/crm/api/v1/invite/remove")
897+
.then()
898+
.extract()
899+
.asString();
900+
assertEquals("removed", response);
901+
902+
user = userRepository.findByCrmContactIdAndOrganisation(CRM_CONTACT_ID, organisation).get();
903+
assertEquals(1, user.getUserRoles().size());
904+
}
905+
906+
@Test
907+
void removeCRMRolesNoUserFound() {
908+
this.seedCRMData();
909+
910+
String response = given()
911+
.when()
912+
.accept(ContentType.JSON)
913+
.header(API_KEY_HEADER, "secret")
914+
.contentType(ContentType.JSON)
915+
.body(new RemoveRoles(CRM_ORGANIZATION_ID, "nope"))
916+
.post("/crm/api/v1/invite/remove")
917+
.then()
918+
.extract()
919+
.asString();
920+
assertEquals("removed", response);
921+
}
922+
923+
@Test
924+
void removeCRMRolesNoOrganisationFound() {
925+
this.seedCRMData();
926+
927+
String response = given()
928+
.when()
929+
.accept(ContentType.JSON)
930+
.header(API_KEY_HEADER, "secret")
931+
.contentType(ContentType.JSON)
932+
.body(new RemoveRoles("nope", "nope"))
933+
.post("/crm/api/v1/invite/remove")
934+
.then()
935+
.extract()
936+
.asString();
937+
assertEquals("removed", response);
938+
}
939+
880940
private void seedCRMData() {
881941
Organisation organisation = organisationRepository.findByCrmOrganisationId(CRM_ORGANIZATION_ID).get();
882942
Role role = new Role();

0 commit comments

Comments
 (0)