Skip to content

Commit

Permalink
Feature/Vas-11619:Fix rebase conflict and adapt services with develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaissa BENARBIA committed Apr 2, 2024
1 parent ac17f49 commit 8c837c0
Show file tree
Hide file tree
Showing 19 changed files with 6,240 additions and 33,769 deletions.
7 changes: 6 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,14 @@ pipeline {
sh 'npmrc default'

sh '''
$MVN_COMMAND clean verify -U -Pvitam -pl '!cots/vitamui-nginx,!cots/vitamui-mongod,!cots/vitamui-logstash,!cots/vitamui-mongo-express' -DskipTests -DskipAllFrontend
$MVN_COMMAND clean verify -U -Pvitam -pl '!cots/vitamui-nginx,!cots/vitamui-mongod,!cots/vitamui-logstash,!cots/vitamui-mongo-express'
'''
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}

stage('Generate front ressources') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void updateNbFailedAttempsPlusLastConnectionAndStatus(final User user, fi
}

public User findUserByEmailAndCustomerId(final String email, String customerId) {
final User user = userRepository.findByEmailAndCustomerId(email, customerId);
final User user = userRepository.findByEmailIgnoreCaseAndCustomerId(email, customerId);
if (user == null) {
throw new NotFoundException(USER_NOT_FOUND_MESSAGE + email);
} else if (UserTypeEnum.NOMINATIVE != user.getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@

/**
* MongoDB repository for the users.
*
*
*/
public interface UserRepository extends VitamUIRepository<User, String> {

Optional<User> findByIdAndCustomerId(String id, String customerId);

User findByEmailIgnoreCaseAndCustomerId(String email, String customerId);

List<User> findAllByEmailIgnoreCase(String email);

boolean existsByEmailAndCustomerId(String email, String customerId);
boolean existsByEmailIgnoreCaseAndCustomerId(String email, String customerId);

long countByGroupId(String profileGroupId);

long countByGroupIdIn(List<String> groupIds);

Page<User> findByCustomerIdAndSubrogeableAndTypeAndStatus(String customerId, boolean subrogeable, UserTypeEnum type, UserStatusEnum status,
Pageable pageable);
Page<User> findByCustomerIdAndSubrogeableAndTypeAndStatus(String customerId, boolean subrogeable, UserTypeEnum type,
UserStatusEnum status,
Pageable pageable);

List<User> findByCustomerId(String customerId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,16 @@ public void sendCreationEmail(final UserDto userDto) {
if (userDto != null && userDto.getStatus() == UserStatusEnum.ENABLED &&
userDto.getType() == UserTypeEnum.NOMINATIVE) {
final List<IdentityProviderDto> providers =
internalIdentityProviderService.getAll(Optional.empty(), Optional.empty());
if (identityProviderHelper.identifierMatchProviderPattern(providers, userDto.getEmail(),
userDto.getCustomerId())) {
internalIdentityProviderService.getAll(Optional.empty(), Optional.empty());
if (identityProviderHelper.identifierMatchProviderPattern(providers, userDto.getEmail(),
userDto.getCustomerId())) {
LOGGER.debug("Sending mail after creating user: {}", userDto.getEmail());
final UserInfoDto userInfoDto = userInfoInternalService.getOne(userDto.getUserInfoId());
restClientFactory.getRestTemplate()
.getForEntity(restClientFactory.getBaseUrl() + casResetPasswordUrl, Boolean.class,
userDto.getEmail(), userDto.getFirstname(), userDto.getLastname(),
LanguageDto.valueOf(userInfoDto.getLanguage()).getLanguage(), userDto.getCustomerId());
restClientFactory.getRestTemplate()
.getForEntity(restClientFactory.getBaseUrl() + casResetPasswordUrl, Boolean.class,
userDto.getEmail(), userDto.getFirstname(), userDto.getLastname(),
LanguageDto.valueOf(userInfoDto.getLanguage()).getLanguage(), userDto.getCustomerId());
}
} catch (final Exception e) {
LOGGER.error("User creation: failed to send mail after creation. \n{}", e);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
*/
package fr.gouv.vitamui.iam.internal.server.user.service;

import static fr.gouv.vitamui.commons.api.CommonConstants.*;
import static fr.gouv.vitamui.commons.logbook.common.EventType.*;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -51,11 +48,13 @@
import fr.gouv.vitamui.commons.api.converter.Converter;
import fr.gouv.vitamui.commons.api.domain.ApplicationDto;
import fr.gouv.vitamui.commons.api.domain.GroupDto;
import fr.gouv.vitamui.commons.api.domain.IdDto;
import fr.gouv.vitamui.commons.api.domain.ProfileDto;
import fr.gouv.vitamui.commons.api.domain.ServicesData;
import fr.gouv.vitamui.commons.api.domain.TenantDto;
import fr.gouv.vitamui.commons.api.domain.TenantInformationDto;
import fr.gouv.vitamui.commons.api.domain.UserDto;
import fr.gouv.vitamui.commons.api.domain.UserInfoDto;
import fr.gouv.vitamui.commons.api.enums.UserStatusEnum;
import fr.gouv.vitamui.commons.api.enums.UserTypeEnum;
import fr.gouv.vitamui.commons.api.exception.ApplicationServerException;
Expand Down Expand Up @@ -101,14 +100,6 @@
import fr.gouv.vitamui.iam.internal.server.user.domain.AlertAnalytics;
import fr.gouv.vitamui.iam.internal.server.user.domain.User;
import fr.gouv.vitamui.iam.security.service.InternalSecurityService;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.MapUtils;
Expand All @@ -128,6 +119,8 @@
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.Assert;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -142,10 +135,19 @@
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static fr.gouv.vitamui.commons.api.CommonConstants.APPLICATION_ID;
import static fr.gouv.vitamui.commons.api.CommonConstants.GPDR_DEFAULT_VALUE;
import static fr.gouv.vitamui.commons.api.CommonConstants.USER_ID_ATTRIBUTE;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_BLOCK_USER;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_CREATE_USER;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_CREATE_USER_INFO;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_PASSWORD_CHANGE;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_PASSWORD_INIT;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_PASSWORD_REVOCATION;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_UPDATE_USER;
import static fr.gouv.vitamui.commons.logbook.common.EventType.EXT_VITAMUI_UPDATE_USER_INFO;

/**
* The service to read, create, update and delete the users.
Expand All @@ -164,7 +166,7 @@ public class UserInternalService extends VitamUICrudService<UserDto, User> {
EXT_VITAMUI_PASSWORD_CHANGE,
EXT_VITAMUI_CREATE_USER_INFO,
EXT_VITAMUI_UPDATE_USER_INFO
);
);

private UserRepository userRepository;

Expand Down Expand Up @@ -224,7 +226,8 @@ public UserInternalService(final SequenceGeneratorService sequenceGeneratorServi
final MongoTransactionManager mongoTransactionManager, final LogbookService logbookService,
final AddressService addressService,
final ApplicationInternalService applicationInternalService, final PasswordConfiguration passwordConfiguration,
final UserExportService userExportService, final UserInfoInternalService userInfoInternalService, final ConnectionHistoryService connectionHistoryService) {
final UserExportService userExportService, final UserInfoInternalService userInfoInternalService,
final ConnectionHistoryService connectionHistoryService) {
super(sequenceGeneratorService);
this.userRepository = userRepository;
this.groupInternalService = groupInternalService;
Expand Down Expand Up @@ -266,7 +269,7 @@ public UserDto findUserByEmailAndCustomerId(final String email, final String cus
}

public List<UserDto> findUsersByEmail(final String email) {
List<User> users = getRepository().findAllByEmail(email);
List<User> users = getRepository().findAllByEmailIgnoreCase(email);
return users.stream()
.map(this::convertFromEntityToDto)
.collect(Collectors.toList());
Expand Down Expand Up @@ -318,7 +321,8 @@ public Resource exportUsers(final Optional<String> criteria) {
final List<LogbookEventDto> userEvents = mapToEvents(userOperations, userInfoOperations);
final List<LogbookEventDto> filteredUserEvents = filterUserEvents(userEvents, userIds);

userExportService.createXlsxFile(usersDto, filteredUserEvents, buildUsersInfoLangMap(userInfoIds), buildUsersGroupNamesMap(userGroupIds), xlsOutputStream);
userExportService.createXlsxFile(usersDto, filteredUserEvents, buildUsersInfoLangMap(userInfoIds),
buildUsersGroupNamesMap(userGroupIds), xlsOutputStream);

return new ByteArrayResource(xlsOutputStream.toByteArray());
} catch (final IOException exception) {
Expand All @@ -328,7 +332,8 @@ public Resource exportUsers(final Optional<String> criteria) {

private List<LogbookEventDto> filterUserEvents(final List<LogbookEventDto> userEvents, final List<String> userIds) {
return userEvents.stream()
.filter(logbookEventDto -> USER_OPERATIONS_EVENT_TYPES.contains(EventType.valueOf(logbookEventDto.getEvType())))
.filter(
logbookEventDto -> USER_OPERATIONS_EVENT_TYPES.contains(EventType.valueOf(logbookEventDto.getEvType())))
.filter(logbookEventDto -> userIds.contains(logbookEventDto.getObId()))
.collect(Collectors.toList());
}
Expand All @@ -337,36 +342,45 @@ private List<String> getIdentifiers(List<UserDto> usersDto) {
return usersDto.stream().map(UserDto::getIdentifier).collect(Collectors.toList());
}

private List<LogbookEventDto> mapToEvents(LogbookOperationsResponseDto userOperations, LogbookOperationsResponseDto userInfoOperations) {
final Stream<LogbookOperationDto> mergedOperations = Stream.concat(userOperations.getResults().stream(), userInfoOperations.getResults().stream());
private List<LogbookEventDto> mapToEvents(LogbookOperationsResponseDto userOperations,
LogbookOperationsResponseDto userInfoOperations) {
final Stream<LogbookOperationDto> mergedOperations =
Stream.concat(userOperations.getResults().stream(), userInfoOperations.getResults().stream());

return mergedOperations
.map(operation -> {
operation.getEvents().forEach(logbookEventDto -> logbookEventDto.setEvIdAppSession(operation.getEvIdAppSession()));
operation.getEvents()
.forEach(logbookEventDto -> logbookEventDto.setEvIdAppSession(operation.getEvIdAppSession()));
return operation.getEvents();
})
.flatMap(Collection::stream).collect(Collectors.toList());
}

private LogbookOperationsResponseDto getUserOperations(List<String> userIdentifiers) {
VitamContext vitamContext = internalSecurityService.buildVitamContext(internalSecurityService.getTenantIdentifier());
VitamContext vitamContext =
internalSecurityService.buildVitamContext(internalSecurityService.getTenantIdentifier());
ObjectNode usersQuery = logbookService.buildQuery(userIdentifiers, MongoDbCollections.USERS);

try {
RequestResponse<LogbookOperation> usersLogbookOperations = logbookService.selectOperations(usersQuery, vitamContext);
return VitamRestUtils.responseMapping(usersLogbookOperations.toJsonNode(), LogbookOperationsResponseDto.class);
RequestResponse<LogbookOperation> usersLogbookOperations =
logbookService.selectOperations(usersQuery, vitamContext);
return VitamRestUtils.responseMapping(usersLogbookOperations.toJsonNode(),
LogbookOperationsResponseDto.class);
} catch (VitamClientException exception) {
throw new InternalServerException("An error occurred while fetching user operations", exception);
}
}

private LogbookOperationsResponseDto getUserInfoOperations(List<String> userIdentifiers) {
VitamContext vitamContext = internalSecurityService.buildVitamContext(internalSecurityService.getTenantIdentifier());
VitamContext vitamContext =
internalSecurityService.buildVitamContext(internalSecurityService.getTenantIdentifier());
ObjectNode userInfoQuery = logbookService.buildQuery(userIdentifiers, MongoDbCollections.USER_INFOS);

try {
RequestResponse<LogbookOperation> userInfoLogbookOperations = logbookService.selectOperations(userInfoQuery, vitamContext);
return VitamRestUtils.responseMapping(userInfoLogbookOperations.toJsonNode(), LogbookOperationsResponseDto.class);
RequestResponse<LogbookOperation> userInfoLogbookOperations =
logbookService.selectOperations(userInfoQuery, vitamContext);
return VitamRestUtils.responseMapping(userInfoLogbookOperations.toJsonNode(),
LogbookOperationsResponseDto.class);
} catch (VitamClientException exception) {
throw new InternalServerException("An error occurred while fetching user operations", exception);
}
Expand Down Expand Up @@ -518,7 +532,8 @@ public UserDto update(final UserDto dto) {
return updatedUser;
}

public void saveCurrentPasswordInOldPasswords(final User user, final String newPassword, final Integer maxOldPassword) {
public void saveCurrentPasswordInOldPasswords(final User user, final String newPassword,
final Integer maxOldPassword) {

if (StringUtils.isNotBlank(newPassword)) {
List<String> oldPasswords = user.getOldPasswords();
Expand Down Expand Up @@ -823,7 +838,8 @@ private void checkEmail(final String email, final String customerId, final Strin
Assert.notNull(email, "email : " + email + " format is not allowed");
Assert.isTrue(Pattern.matches(IamUtils.EMAIL_VALID_REGEXP, email),
"email : " + email + " format is not allowed");
Assert.isNull(getRepository().findByEmailIgnoreCaseAndCustomerId(email, customerId), message + ": mail already exists");
Assert.isNull(getRepository().findByEmailIgnoreCaseAndCustomerId(email, customerId),
message + ": mail already exists");
if (email.matches(ADMIN_EMAIL_PATTERN + ".*")) {
final Query query = new Query();
query.addCriteria(Criteria.where("email").regex("^" + ADMIN_EMAIL_PATTERN));
Expand Down Expand Up @@ -1113,7 +1129,7 @@ public UserDto patchAnalytics(final Map<String, Object> partialDto) {
patchLastTenantIdentifier(user, CastUtils.toInteger(value));
break;
case "alerts":
finalObjectMapper objectMapper = new ObjectMapper();
final ObjectMapper objectMapper = new ObjectMapper();
final List<AlertAnalytics> alertAnalytics =
objectMapper.convertValue(CastUtils.toList(value), new TypeReference<>() {
});
Expand Down Expand Up @@ -1172,19 +1188,21 @@ private void checkApplicationAccessPermission(final String applicationId) {
}

private Map<String, String> buildUsersInfoLangMap(final List<String> userInfoIds) {
return userInfoInternalService.getMany(userInfoIds).stream().collect(Collectors.toMap(IdDto::getId, UserInfoDto::getLanguage));
return userInfoInternalService.getMany(userInfoIds).stream()
.collect(Collectors.toMap(IdDto::getId, UserInfoDto::getLanguage));
}

private Map<String, String> buildUsersGroupNamesMap(final List<String> userGroupIds) {
return groupInternalService.getMany(userGroupIds).stream().collect(Collectors.toMap(IdDto::getId, GroupDto::getName));
return groupInternalService.getMany(userGroupIds).stream()
.collect(Collectors.toMap(IdDto::getId, GroupDto::getName));
}

@Override
protected Document groupFields(final Optional<String> criteriaJsonString, final String... fields) {
return super.groupFields(criteriaJsonString, fields);
}

public List<User> findByCustomerId(String customerId){
public List<User> findByCustomerId(String customerId) {
return this.userRepository.findByCustomerId(customerId);
}
}
14 changes: 0 additions & 14 deletions api/api-iam/iam-internal/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ provisioning-client:
key-password: changeme
type: JKS
hostname-verification: false
- idp-identifier: keycloak-idp
uri: https://localhost:8090/users
client:
secure: true
ssl-configuration:
keystore:
key-path: src/main/resources/dev/keystore_iam-internal.jks
key-password: changeme
type: JKS
truststore:
key-path: src/main/resources/dev/truststore_server.jks
key-password: changeme
type: JKS
hostname-verification: false

address:
max-street-length: 250
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public void testLogoutSubrogation() {
subro.setSurrogateCustomerId(surrogateCustomerId);
Mockito.when(subrogationRepository.findBySuperUserAndSuperUserCustomerIdAndSurrogateAndSurrogateCustomerId(
superUser, superUserCustomerId, surrogate, surrogateCustomerId)).thenReturn(Optional.of(subro));
Mockito.when(userRepository.findByEmailIgnoreCaseAndCustomerId(surrogate, surrogateCustomerId)).thenReturn(new User());
Mockito.when(userRepository.findByEmailIgnoreCaseAndCustomerId(surrogate, surrogateCustomerId))
.thenReturn(new User());
Mockito.when(userRepository.findByEmailIgnoreCaseAndCustomerId(superUser, superUserCustomerId))
.thenReturn(new User());
casService.deleteSubrogationBySuperUserAndSurrogate(superUser, superUserCustomerId,
Expand Down Expand Up @@ -225,9 +226,16 @@ private Subrogation getUsersByEmail(final UserDto user) {
Mockito.when(internalUserService.findUsersByEmail(email))
.thenReturn(List.of(user));
Mockito.when(internalUserService.loadGroupAndProfiles(ArgumentMatchers.any())).thenReturn(authUser);
Mockito.when(subrogationRepository.findOneBySurrogate(ArgumentMatchers.anyString())).thenReturn(subro);
Mockito.when(userRepository.findByEmail(ArgumentMatchers.anyString())).thenReturn(new User());
casService.getUserByEmail(email, Optional.of(CommonConstants.AUTH_TOKEN_PARAMETER + "," + CommonConstants.SURROGATION_PARAMETER));
Mockito.when(subrogationRepository.findOneBySurrogateAndSurrogateCustomerId(email, customerId))
.thenReturn(subro);
Mockito.when(userRepository.findAllByEmailIgnoreCase(email))
.thenReturn(List.of(new User()));
Mockito.when(userRepository.findByEmailIgnoreCaseAndCustomerId(email, customerId))
.thenReturn(new User());
Mockito.when(userRepository.findByEmailIgnoreCaseAndCustomerId("superuser@vitamui.com", "customer_system"))
.thenReturn(new User());
casService.getUsersByEmail(email,
CommonConstants.AUTH_TOKEN_PARAMETER + "," + CommonConstants.SURROGATION_PARAMETER);
return subro;
}

Expand Down
Loading

0 comments on commit 8c837c0

Please sign in to comment.