Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisKina-dev committed Jan 10, 2025
1 parent 656a728 commit 70cc8d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -117,13 +109,16 @@ private Broker getSessionBroker(IamUserInfoDTO iamUserInfoDTO, List<Operator> 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<Operator> 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<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfo.getInnerUserId());
Broker brokerInfo = getSessionBroker(iamUserInfo, userRoles, accessToken);

if (brokerInfo != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 70cc8d2

Please sign in to comment.