Skip to content

Commit

Permalink
[SELC-4483] fix for change request
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Mar 27, 2024
1 parent 6992c57 commit b3cf565
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 36 deletions.
8 changes: 4 additions & 4 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@
"tags" : [ "institutions" ],
"summary" : "getInstitutions",
"description" : "Service to get all the institutions related to logged user",
"operationId" : "getInstitutionsUsingGET_1",
"operationId" : "v2RetrieveUserInstitutions",
"parameters" : [ {
"name" : "authenticated",
"in" : "query",
Expand Down Expand Up @@ -1393,7 +1393,7 @@
"tags" : [ "institutions" ],
"summary" : "createInstitutionProductUser",
"description" : "Service to Create a user related to a specific pair of institution-product",
"operationId" : "createInstitutionProductUserUsingPOST_1",
"operationId" : "v2PostCreateInstitutionProductUser",
"parameters" : [ {
"name" : "institutionId",
"in" : "path",
Expand Down Expand Up @@ -1474,7 +1474,7 @@
"tags" : [ "institutions" ],
"summary" : "addUserProductRoles",
"description" : "Service to add a new role/product to a specific user",
"operationId" : "addUserProductRolesUsingPUT_1",
"operationId" : "v2AddUserProductRole",
"parameters" : [ {
"name" : "institutionId",
"in" : "path",
Expand Down Expand Up @@ -1557,7 +1557,7 @@
"tags" : [ "institutions" ],
"summary" : "getInstitutionUser",
"description" : "Service to get the users with the given user id related to a specific institution",
"operationId" : "getInstitutionUserUsingGET_1",
"operationId" : "v2RetrieveInstitutionUser",
"parameters" : [ {
"name" : "authenticated",
"in" : "query",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public interface UserApiConnector {

List<UserInstitution> retrieveFilteredUser(String userId, String institutionId, String productId);

String createOrUpdateUserByFiscalCode(String institutionId, String productId, UserToCreate userDto, CreateUserDto.Role role);
String createOrUpdateUserByFiscalCode(String institutionId, String productId, UserToCreate userDto, List<CreateUserDto.Role> role);

void createOrUpdateUserByUserId(String institutionId, String productId, String userId, CreateUserDto.Role role);
void createOrUpdateUserByUserId(String institutionId, String productId, String userId, List<CreateUserDto.Role> role);

}
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,41 @@ private List<String> getValidUserStates() {
}

@Override
public String createOrUpdateUserByFiscalCode(String institutionId, String productId, UserToCreate userDto, it.pagopa.selfcare.dashboard.connector.model.user.CreateUserDto.Role role) {
public String createOrUpdateUserByFiscalCode(String institutionId, String productId, UserToCreate userDto, List<it.pagopa.selfcare.dashboard.connector.model.user.CreateUserDto.Role> roles) {
log.trace("createOrUpdateUserByFiscalCode start");
log.debug("createOrUpdateUserByFiscalCode userDto = {}", userDto);
CreateUserDto createUserDto = buildCreateUserDto(institutionId, productId, userDto, role);
String userId = userApiRestClient._usersPost(createUserDto).getBody();
List<it.pagopa.selfcare.dashboard.connector.model.user.CreateUserDto.Role> finalRole = new ArrayList<>(roles);
String userId = roles.stream()
.findFirst()
.map(role -> {
CreateUserDto createUserDto = buildCreateUserDto(institutionId, productId, userDto, role);
finalRole.remove(role);
return userApiRestClient._usersPost(createUserDto).getBody();
})
.orElseThrow(() -> new IllegalArgumentException("Role list cannot be empty"));


createOrUpdateUserByUserId(institutionId, productId, userId, finalRole);

log.trace("createOrUpdateUserByFiscalCode end");
return userId;
}

@Override
public void createOrUpdateUserByUserId(String institutionId, String productId, String userId, it.pagopa.selfcare.dashboard.connector.model.user.CreateUserDto.Role role) {
public void createOrUpdateUserByUserId(String institutionId, String productId, String userId, List<it.pagopa.selfcare.dashboard.connector.model.user.CreateUserDto.Role> roles) {
log.trace("createOrUpdateUserByUserId start");
log.debug("createOrUpdateUserByUserId userId = {}", userId);
AddUserRoleDto addUserRoleDto = AddUserRoleDto.builder()
.institutionId(institutionId)
.product(Product.builder()
.productRole(role.getProductRole())
.role(it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole.valueOf(role.getPartyRole().name()))
.productId(productId)
.build())
.build();

userApiRestClient._usersUserIdPost(userId, addUserRoleDto);
roles.forEach(role -> {
AddUserRoleDto addUserRoleDto = AddUserRoleDto.builder()
.institutionId(institutionId)
.product(Product.builder()
.productRole(role.getProductRole())
.role(it.pagopa.selfcare.user.generated.openapi.v1.dto.PartyRole.valueOf(role.getPartyRole().name()))
.productId(productId)
.build())
.build();
userApiRestClient._usersUserIdPost(userId, addUserRoleDto);
});
log.trace("createOrUpdateUserByUserId end");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,12 @@ void testCreateOrUpdateUserByFiscalCode() {
CreateUserDto.Role role = new CreateUserDto.Role();
role.setPartyRole(it.pagopa.selfcare.commons.base.security.PartyRole.MANAGER);
role.setProductRole("admin");

CreateUserDto.Role role2 = new CreateUserDto.Role();
role2.setPartyRole(it.pagopa.selfcare.commons.base.security.PartyRole.MANAGER);
role2.setProductRole("admin2");
// Act
userConnector.createOrUpdateUserByFiscalCode("institutionId", "productId", userDto, role);
userConnector.createOrUpdateUserByFiscalCode("institutionId", "productId", userDto, List.of(role, role2));

// Assert that nothing has changed
verify(userApiRestClient)._usersPost(Mockito.any());
Expand All @@ -316,7 +320,7 @@ void testCreateOrUpdateUserByUserId() {
role.setProductRole("admin");

// Act
userConnector.createOrUpdateUserByUserId("institutionId", "productId", "userId", role);
userConnector.createOrUpdateUserByUserId("institutionId", "productId", "userId", List.of(role));

// Assert that nothing has changed
verify(userApiRestClient)._usersUserIdPost(eq("userId"), any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.lang.reflect.Executable;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
Expand Down Expand Up @@ -119,7 +120,7 @@ public Collection<UserInfo> getUsersByInstitutionId(String institutionId, String
public String createUsers(String institutionId, String productId, UserToCreate userDto) {
log.trace("createOrUpdateUserByFiscalCode start");
log.debug("createOrUpdateUserByFiscalCode userDto = {}", userDto);
CreateUserDto.Role role = retrieveRole(productId, userDto.getProductRoles());
List<CreateUserDto.Role> role = retrieveRole(productId, userDto.getProductRoles());
String userId = userApiConnector.createOrUpdateUserByFiscalCode(institutionId, productId, userDto, role);
log.trace("createOrUpdateUserByFiscalCode end");
return userId;
Expand All @@ -129,22 +130,28 @@ public String createUsers(String institutionId, String productId, UserToCreate u
public void addUserProductRoles(String institutionId, String productId, String userId, Set<String> productRoles) {
log.trace("createOrUpdateUserByUserId start");
log.debug("createOrUpdateUserByUserId userId = {}", userId);
CreateUserDto.Role role = retrieveRole(productId, productRoles);
List<CreateUserDto.Role> role = retrieveRole(productId, productRoles);
userApiConnector.createOrUpdateUserByUserId(institutionId, productId, userId, role);
log.trace("createOrUpdateUserByUserId end");
}

private CreateUserDto.Role retrieveRole(String productId, Set<String> productRoles) {
/**
* This method is used to retrieve a list of roles for a given product.
* It maps each product role to a CreateUserDto.Role object, which includes the label and party role.
* To retrieve the party role, it uses the roleMappings of the product filtering by a white list of party roles (Only SUB_DELEGATE and OPERATOR are allowed
* as Role to be assigned to a user in add Users ProductRoles operation).
* If the party role is not valid, it throws an InvalidProductRoleException.
*/
private List<CreateUserDto.Role> retrieveRole(String productId, Set<String> productRoles) {
Product product = productsConnector.getProduct(productId);
return productRoles.stream().findFirst().map(productRole -> {
return productRoles.stream().map(productRole -> {
EnumMap<PartyRole, ProductRoleInfo> roleMappings = product.getRoleMappings();
CreateUserDto.Role role = new CreateUserDto.Role();
role.setLabel(Product.getLabel(productRole, roleMappings).orElse(null));
Optional<PartyRole> partyRole = Product.getPartyRole(productRole, roleMappings, PARTY_ROLE_WHITE_LIST);
role.setPartyRole(partyRole.orElseThrow(() ->
new InvalidProductRoleException(String.format("Product role '%s' is not valid", productRole))));
return role;
}).orElse(null);
}).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ void addUserProductRoles_ok() {
Set<String> productRoles = new HashSet<>(List.of("operator"));
Product product = getProduct();
when(productsConnector.getProduct(productId)).thenReturn(product);
doNothing().when(userApiConnector).createOrUpdateUserByUserId(eq(institutionId), eq(productId), eq(userId), any(CreateUserDto.Role.class));
doNothing().when(userApiConnector).createOrUpdateUserByUserId(eq(institutionId), eq(productId), eq(userId), anyList());

// when
userService.addUserProductRoles(institutionId, productId, userId, productRoles);

// then
verify(userApiConnector, times(1))
.createOrUpdateUserByUserId(eq(institutionId), eq(productId), eq(userId), any(CreateUserDto.Role.class));
.createOrUpdateUserByUserId(eq(institutionId), eq(productId), eq(userId), anyList());
verifyNoMoreInteractions(userApiConnector);
}

Expand Down Expand Up @@ -264,14 +264,14 @@ void createUsersByFiscalCode() {

when(productsConnector.getProduct(productId)).thenReturn(product);

when(userApiConnector.createOrUpdateUserByFiscalCode(eq(institutionId), eq(productId), eq(userToCreate), any(CreateUserDto.Role.class))).thenReturn("userId");
when(userApiConnector.createOrUpdateUserByFiscalCode(eq(institutionId), eq(productId), eq(userToCreate), anyList())).thenReturn("userId");
// when
String userId = userService.createUsers(institutionId, productId, userToCreate);

// then
assertNotNull(userId);
verify(userApiConnector, times(1))
.createOrUpdateUserByFiscalCode(eq(institutionId), eq(productId), eq(userToCreate), any(CreateUserDto.Role.class));
.createOrUpdateUserByFiscalCode(eq(institutionId), eq(productId), eq(userToCreate), anyList());
verifyNoMoreInteractions(userApiConnector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class InstitutionV2Controller {

@GetMapping(value = "/{institutionId}/users/{userId}")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.getInstitutionUser}")
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.getInstitutionUser}", nickname = "v2RetrieveInstitutionUser")
@PreAuthorize("hasPermission(#institutionId, 'InstitutionResource', 'ANY')")
public InstitutionUserDetailsResource getInstitutionUser(@ApiParam("${swagger.dashboard.institutions.model.id}")
@PathVariable("institutionId")
Expand All @@ -68,7 +68,7 @@ public InstitutionUserDetailsResource getInstitutionUser(@ApiParam("${swagger.da

@GetMapping
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.getInstitutions}")
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.getInstitutions}", nickname = "v2RetrieveUserInstitutions")
public List<InstitutionBaseResource> getInstitutions(Authentication authentication) {

log.trace("getInstitutions start");
Expand All @@ -86,7 +86,7 @@ public List<InstitutionBaseResource> getInstitutions(Authentication authenticati

@PostMapping(value = "/{institutionId}/products/{productId}/users")
@ResponseStatus(HttpStatus.CREATED)
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.createInstitutionProductUser}")
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.createInstitutionProductUser}", nickname = "v2PostCreateInstitutionProductUser")
@PreAuthorize("hasPermission(new it.pagopa.selfcare.dashboard.web.security.ProductAclDomain(#institutionId, #productId), 'ADMIN')")
public UserIdResource createInstitutionProductUser(@ApiParam("${swagger.dashboard.institutions.model.id}")
@PathVariable("institutionId")
Expand All @@ -111,7 +111,7 @@ public UserIdResource createInstitutionProductUser(@ApiParam("${swagger.dashboar

@PutMapping(value = "/{institutionId}/products/{productId}/users/{userId}")
@ResponseStatus(HttpStatus.CREATED)
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.addUserProductRoles}")
@ApiOperation(value = "", notes = "${swagger.dashboard.institutions.api.addUserProductRoles}", nickname = "v2AddUserProductRole")
@PreAuthorize("hasPermission(new it.pagopa.selfcare.dashboard.web.security.ProductAclDomain(#institutionId, #productId), 'ADMIN')")
public void addUserProductRoles(@ApiParam("${swagger.dashboard.institutions.model.id}")
@PathVariable("institutionId")
Expand Down

0 comments on commit b3cf565

Please sign in to comment.