Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: P4ADEV-1303-modify-GetUserInfo-to-handle-system-user #105

Merged
merged 10 commits into from
Oct 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ private ClientDTO authorizeSilCredentials(String clientId, String clientSecret)
private ClientDTO authorizePiattaformaUnitariaCredentials(String clientId, String clientSecret) {
if (!clientSecret.equals(piattaformaUnitariaClientSecret))
throw new ClientUnauthorizedException("Unauthorized client for piattaforma-unitaria client-credentials");
String[] splittedClientId = clientId.split("_");
return ClientDTO.builder()
.clientId(clientId)
.clientName(splittedClientId[0])
.organizationIpaCode(splittedClientId[1])
.clientName(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX)
.organizationIpaCode(clientId.substring(PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX.length()))
.clientSecret(clientSecret)
.build();
}
Expand Down
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

@Service
public class IamUserInfoDTO2UserInfoMapper implements Function<IamUserInfoDTO, UserInfo> {

private static final String WS_USER_SUFFIX = "-WS_USER";
macacia marked this conversation as resolved.
Show resolved Hide resolved
private final UsersRepository usersRepository;
private final OperatorsRepository operatorsRepository;

Expand All @@ -27,26 +29,48 @@ public IamUserInfoDTO2UserInfoMapper(UsersRepository usersRepository, OperatorsR

@Override
public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) {
if (iamUserInfoDTO.isSystemUser()) {
return systemUserMapper(iamUserInfoDTO);
}
return userInfoMapper(iamUserInfoDTO);
}

private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) {
return UserInfo.builder()
.userId(iamUserInfoDTO.getUserId())
.mappedExternalUserId(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode() + WS_USER_SUFFIX)
macacia marked this conversation as resolved.
Show resolved Hide resolved
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(Collections.singletonList(UserOrganizationRoles.builder()
.organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode())
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build()))
.build();
}

private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO) {
User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId()));
List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId());
UserInfo userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();

if(iamUserInfoDTO.getOrganizationAccess()!=null){
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();

if(iamUserInfoDTO.getOrganizationAccess() != null){
userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode());
}
return userInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ void givenSystemUserWhenMatcherThenAssertionOk() {
// Given
String clientId = "piattaforma-unitaria_IPA_TEST";
String clientSecret = "SECRET";
String[] splitted = clientId.split("_");

String piattaformaUnitaria = "piattaforma-unitaria";
String separator = "_";
// When
ClientDTO actualClientDTO = service.authorizeCredentials(clientId, clientSecret);
// Then
Assertions.assertEquals(
ClientDTO.builder()
.clientId(clientId)
.clientName(splitted[0])
.organizationIpaCode(splitted[1])
.clientName(piattaformaUnitaria)
.organizationIpaCode(clientId.substring((piattaformaUnitaria + separator).length()))
.clientSecret(clientSecret)
.build(), actualClientDTO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -82,7 +83,7 @@ void givenCompleteDataWhenApplyThenOk(){
.roles(Set.of("ROLE"))
.build());

testApplyOk(iamUserInfo, user, organizationRoles);
testApplyOk(iamUserInfo, user, organizationRoles, null);
}

@Test
Expand All @@ -106,7 +107,7 @@ void givenNotOperatorsWhenApplyThenOk(){
.mappedExternalUserId("MAPPEDEXTERNALUSERID")
.build();

testApplyOk(iamUserInfo, user, Collections.emptyList());
testApplyOk(iamUserInfo, user, Collections.emptyList(), null);
}

@Test
Expand All @@ -133,36 +134,72 @@ void givenNoOrganizationAccessWhenApplyThenOk(){
.email("EMAIL")
.build());

testApplyOk(iamUserInfo, user, organizationRoles);
testApplyOk(iamUserInfo, user, organizationRoles, null);
}

private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List<Operator> organizationRoles) {
Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);
@Test
void givenSystemUserWhenApplyThenOk(){
// Given
IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.systemUser(Boolean.TRUE)
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("ISSUER")
.organizationAccess(IamUserOrganizationRolesDTO.builder()
.organizationIpaCode("IPA_CODE")
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build())
.build();

UserInfo expected = UserInfo.builder()
.userId("EXTERNALUSERID")
.mappedExternalUserId("IPA_CODE-WS_USER")
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("IPA_CODE")
.organizations(Collections.singletonList(UserOrganizationRoles.builder()
.organizationIpaCode("IPA_CODE")
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build()))
.build();

testApplyOk(iamUserInfo, null, null, expected);
}

private void testApplyOk(IamUserInfoDTO iamUserInfo, User user, List<Operator> organizationRoles, UserInfo expected) {

UserInfo userInfo;
if (iamUserInfo.isSystemUser()) {
userInfo = expected;
} else {
Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);
userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfo.getFiscalCode())
.familyName(iamUserInfo.getFamilyName())
.name(iamUserInfo.getName())
.issuer(iamUserInfo.getIssuer())
.organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null)
.organizations(organizationRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();
macacia marked this conversation as resolved.
Show resolved Hide resolved
}
// When
UserInfo result = mapper.apply(iamUserInfo);

// Then
Assertions.assertEquals(
UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfo.getFiscalCode())
.familyName(iamUserInfo.getFamilyName())
.name(iamUserInfo.getName())
.issuer(iamUserInfo.getIssuer())
.organizationAccess(iamUserInfo.getOrganizationAccess()!=null? iamUserInfo.getOrganizationAccess().getOrganizationIpaCode(): null)
.organizations(organizationRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build(),
result
);
Assertions.assertEquals(userInfo, result);
}
}