diff --git a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java index b9ebdba..632527e 100644 --- a/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java +++ b/src/main/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapper.java @@ -3,13 +3,11 @@ import it.gov.pagopa.payhub.auth.connector.client.OrganizationSearchClient; import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO; import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO; -import it.gov.pagopa.payhub.auth.exception.custom.InvalidAccessTokenException; import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException; import it.gov.pagopa.payhub.auth.model.Operator; 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.service.TokenStoreService; import it.gov.pagopa.payhub.auth.utils.Constants; import it.gov.pagopa.payhub.dto.generated.UserInfo; import it.gov.pagopa.payhub.dto.generated.UserOrganizationRoles; @@ -18,10 +16,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; @Service public class IamUserInfoDTO2UserInfoMapper { @@ -31,18 +26,15 @@ public class IamUserInfoDTO2UserInfoMapper { private final OperatorsRepository operatorsRepository; private final OrganizationSearchClient organizationSearchClient; private final boolean organizationAccessMode; - private final TokenStoreService tokenStoreService; public IamUserInfoDTO2UserInfoMapper(@Value("${app.enable-access-organization-mode}") boolean organizationAccessMode, UsersRepository usersRepository, OperatorsRepository operatorsRepository, - OrganizationSearchClient organizationSearchClient, - TokenStoreService tokenStoreService) { + OrganizationSearchClient organizationSearchClient) { this.usersRepository = usersRepository; this.operatorsRepository = operatorsRepository; this.organizationSearchClient = organizationSearchClient; this.organizationAccessMode = organizationAccessMode; - this.tokenStoreService = tokenStoreService; } public UserInfo apply(IamUserInfoDTO iamUserInfoDTO, String accessToken) { @@ -66,7 +58,7 @@ private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO, String accessTo .roles(Collections.singletonList(Constants.ROLE_ADMIN)) .build())) .build(); - setBrokerInfo(userInfo, accessToken); + setBrokerInfo(userInfo, iamUserInfoDTO, accessToken); return userInfo; } @@ -98,7 +90,7 @@ private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO, String accessToke if (iamUserInfoDTO.getOrganizationAccess() != null) { userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode()); } - setBrokerInfo(userInfo, accessToken); + setBrokerInfo(userInfo, iamUserInfoDTO, accessToken); userInfo.setCanManageUsers(!organizationAccessMode); return userInfo; } @@ -117,13 +109,16 @@ private Broker getSessionBroker(IamUserInfoDTO iamUserInfoDTO, List us return null; } - private void setBrokerInfo(UserInfo userInfo, String accessToken) { - IamUserInfoDTO iamUserInfo = tokenStoreService.load(accessToken); - if (iamUserInfo == null) { - throw new InvalidAccessTokenException("AccessToken not found"); - } + private void setBrokerInfo(UserInfo userInfo, IamUserInfoDTO iamUserInfo, String accessToken) { + List userRoles = userInfo.getOrganizations().stream() + .map(org -> Operator.builder() + .operatorId(org.getOperatorId()) + .organizationIpaCode(org.getOrganizationIpaCode()) + .roles(new HashSet<>(org.getRoles())) + .email(org.getEmail()) + .build()) + .toList(); - List userRoles = operatorsRepository.findAllByUserId(iamUserInfo.getInnerUserId()); Broker brokerInfo = getSessionBroker(iamUserInfo, userRoles, accessToken); if (brokerInfo != null) { diff --git a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java index 3f2786d..ad8925b 100644 --- a/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java +++ b/src/test/java/it/gov/pagopa/payhub/auth/service/user/IamUserInfoDTO2UserInfoMapperTest.java @@ -8,7 +8,6 @@ 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.service.TokenStoreService; import it.gov.pagopa.payhub.auth.utils.Constants; import it.gov.pagopa.payhub.dto.generated.UserInfo; import it.gov.pagopa.payhub.dto.generated.UserOrganizationRoles; @@ -39,16 +38,13 @@ class IamUserInfoDTO2UserInfoMapperTest { @Mock private OrganizationSearchClient organizationSearchClientMock; - @Mock - private TokenStoreService tokenStoreService; - private IamUserInfoDTO2UserInfoMapper mapper; private final boolean organizationAccessMode = false; @BeforeEach void init() { - mapper = new IamUserInfoDTO2UserInfoMapper(organizationAccessMode, usersRepositoryMock, operatorsRepositoryMock, organizationSearchClientMock, tokenStoreService); + mapper = new IamUserInfoDTO2UserInfoMapper(organizationAccessMode, usersRepositoryMock, operatorsRepositoryMock, organizationSearchClientMock); } @AfterEach @@ -130,8 +126,6 @@ void givenCompleteDataWhenApplyThenOk() { Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString())) .thenReturn(mockBroker); - Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo); - UserInfo result = mapper.apply(iamUserInfo, accessToken); Assertions.assertEquals(expected, result); @@ -185,8 +179,6 @@ void givenNotOperatorsWhenApplyThenOk() { Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString())) .thenReturn(mockBroker); - Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo); - UserInfo result = mapper.apply(iamUserInfo, accessToken); Assertions.assertEquals(expected, result); @@ -247,8 +239,6 @@ void givenNoOrganizationAccessWhenApplyThenOk() { Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString())) .thenReturn(mockBroker); - Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo); - UserInfo result = mapper.apply(iamUserInfo, accessToken); Assertions.assertEquals(expected, result); @@ -296,9 +286,6 @@ void givenSystemUserWhenApplyThenOk() { Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString())) .thenReturn(mockBroker); - Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo); - Mockito.when(operatorsRepositoryMock.findAllByUserId(Mockito.anyString())).thenReturn(Collections.emptyList()); - UserInfo result = mapper.apply(iamUserInfo, accessToken); Assertions.assertEquals(expected, result);