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 @@ -10,7 +10,9 @@
@Service
@Slf4j
public class AuthorizeClientCredentialsRequestService {

private static final String PIATTAFORMA_UNITARIA_CLIENT_ID_PREFIX = "piattaforma-unitaria_";

private final ClientService clientService;
private final ClientMapper clientMapper;
private final String piattaformaUnitariaClientSecret;
Expand Down Expand Up @@ -41,11 +43,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,21 @@
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 +31,49 @@ 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) {
String organizationIpaCode = iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode();
return UserInfo.builder()
.userId(iamUserInfoDTO.getUserId())
.mappedExternalUserId(organizationIpaCode + WS_USER_SUFFIX)
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(Collections.singletonList(UserOrganizationRoles.builder()
.organizationIpaCode(organizationIpaCode)
.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,15 @@ void givenCompleteDataWhenApplyThenOk(){
.roles(Set.of("ROLE"))
.build());

testApplyOk(iamUserInfo, user, organizationRoles);
UserInfo expected = UserInfo.builder()
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("ISSUER")
.organizationAccess("ORG")
.build();

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

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

testApplyOk(iamUserInfo, user, Collections.emptyList());
UserInfo expected = UserInfo.builder()
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("ISSUER")
.organizationAccess("ORG")
.build();

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

@Test
Expand All @@ -132,37 +149,70 @@ void givenNoOrganizationAccessWhenApplyThenOk(){
.roles(Set.of("ROLE"))
.email("EMAIL")
.build());

testApplyOk(iamUserInfo, user, organizationRoles);

UserInfo expected = UserInfo.builder()
.fiscalCode("FISCALCODE")
.familyName("FAMILYNAME")
.name("NAME")
.issuer("ISSUER")
.build();

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

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("IPA_CODE")
.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) {

if (!iamUserInfo.isSystemUser()) {
Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);
expected.setUserId(user.getUserId());
expected.setMappedExternalUserId(user.getMappedExternalUserId());
expected.setOrganizations(organizationRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList());
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(expected, result);
}
}
Loading