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

[BUG] 🐛 Fix conflict when getting by mail a user with a similar mail #872

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- [BUG] :goal_net: Catch all LdapException when using mono connection
- [REL] :rocket: release version 2.1.2


# 2.1.2

- [BUG] :bug: Fix max time before a connection is dropped from ldap connection pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,15 @@ public Optional<User> getUserByMail(String mail) {
logger.debug("Searching user with mail {}", mail);
User searchedUser = new User();
searchedUser.setMail(mail);
PageResult<User> users =
searchUsers(searchedUser, new PageableResult(2, 0, null), SearchType.OR.name());
List<User> users =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part got me confused at first, and it is better this way

searchUsers(searchedUser, new PageableResult(2, 0, null), SearchType.OR.name())
.getResults()
.stream()
.filter(u -> u.getMail().equalsIgnoreCase(mail))
.collect(Collectors.toList());
User user = null;
if (users.getResults().size() == 1) {
user = users.getResults().get(0);
if (users.size() == 1) {
user = users.get(0);
if (user.getAddress() != null && user.getAddress().getId() != null) {
PostalAddress address = getAddress(user.getAddress().getId());
if (address != null) {
Expand All @@ -381,7 +385,7 @@ public Optional<User> getUserByMail(String mail) {
if (user.getOrganization() != null) {
user.setOrganization(getOrganization(user.getOrganization().getIdentifiant()).orElse(null));
}
} else if (users.getResults().size() > 1) {
} else if (users.size() > 1) {
throw new MultipleUserWithSameMailException(mail);
}
return Optional.ofNullable(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import fr.insee.sugoi.ldap.utils.config.LdapConfigKeys;
import fr.insee.sugoi.model.*;
import fr.insee.sugoi.model.exceptions.MultipleUserWithSameMailException;
import fr.insee.sugoi.model.fixtures.StoreMappingFixture;
import fr.insee.sugoi.model.paging.PageResult;
import fr.insee.sugoi.model.paging.PageableResult;
Expand Down Expand Up @@ -371,4 +373,44 @@ public void userWithoutPasswordShouldHaveValueHasPassword() {
ldapReaderStore.getUser("agarder").get().getAttributes().get("hasPassword"),
is(false));
}

@Test
@DisplayName(
"Given a user which mail is unique in the realm"
+ "the user should be retrievable via its mail")
public void getUserByMailTest() {
assertThat(
"Should be the user mail1",
ldapReaderStore.getUserByMail("[email protected]").get().getUsername(),
is("mail1"));
}

@Test
@DisplayName(
"Given a user which mail is unique in the realm "
+ "even though his mail is a sub of another user mail "
+ "the user should be retrievable via its mail")
public void getUserByMailWithSubMailTest() {
assertThat(
"Should be the user mailsub",
ldapReaderStore.getUserByMail("[email protected]").get().getUsername(),
is("mailsub"));
}

@Test
@DisplayName("Given we search a user by a mail that does not exist, " + "no user should be given")
public void getNoneExistingUserByMailTest() {
assertThat(
"Should not get a user",
ldapReaderStore.getUserByMail("[email protected]").isEmpty(),
is(true));
}

@Test
@DisplayName("Given several users have the same mail, " + "an exception should be raised")
public void getConflictingMailUserTest() {
assertThrows(
MultipleUserWithSameMailException.class,
() -> ldapReaderStore.getUserByMail("[email protected]"));
}
}
55 changes: 55 additions & 0 deletions sugoi-api-ldap-store-provider/src/test/resources/ldap.ldif
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,61 @@ pwdReset: false
uid: havepwdreset
cn: havepwdreset

dn: uid=mail1,ou=contacts,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeCompte
objectClass: inseeContact
objectClass: inseeAttributsAuthentification
objectClass: inseeAttributsHabilitation
objectClass: inseeAttributsCommunication
uid: mail1
cn: mail1
mail: [email protected]

dn: uid=mailsub,ou=contacts,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeCompte
objectClass: inseeContact
objectClass: inseeAttributsAuthentification
objectClass: inseeAttributsHabilitation
objectClass: inseeAttributsCommunication
uid: mailsub
cn: mailsub
mail: [email protected]

dn: uid=mailext,ou=contacts,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeCompte
objectClass: inseeContact
objectClass: inseeAttributsAuthentification
objectClass: inseeAttributsHabilitation
objectClass: inseeAttributsCommunication
uid: mailext
cn: mailext
mail: [email protected]

dn: uid=mailconflict1,ou=contacts,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeCompte
objectClass: inseeContact
objectClass: inseeAttributsAuthentification
objectClass: inseeAttributsHabilitation
objectClass: inseeAttributsCommunication
uid: mailconflict1
cn: mailconflict1
mail: [email protected]

dn: uid=mailconflict2,ou=contacts,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeCompte
objectClass: inseeContact
objectClass: inseeAttributsAuthentification
objectClass: inseeAttributsHabilitation
objectClass: inseeAttributsCommunication
uid: mailconflict2
cn: mailconflict2
mail: [email protected]

dn: uid=testo,ou=organisations,ou=clients_domaine1,o=insee,c=fr
objectClass: top
objectClass: inseeOrganisation
Expand Down
Loading