Skip to content

Commit

Permalink
feat: P4ADEV-1303-modify-GetUserInfo-to-handle-system-user (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia authored and umbcoppolabottazzi committed Nov 4, 2024
1 parent abf6def commit 250d4ec
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 100 deletions.
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
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";

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,15 @@ void givenSystemUserWhenMatcherThenAssertionOk() {
// Given
String clientId = "piattaforma-unitaria_IPA_TEST";
String clientSecret = "SECRET";
String[] splitted = clientId.split("_");

String piattaformaUnitaria = "piattaforma-unitaria_";
// 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).length()))
.clientSecret(clientSecret)
.build(), actualClientDTO);
}
Expand Down
Loading

0 comments on commit 250d4ec

Please sign in to comment.