Skip to content

Commit

Permalink
Find users from multiple emails
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 81d9e9a7eca51c8659f421d5e6ef75e433a6983d
  • Loading branch information
pdesgarets authored and Gitlab-CI committed Dec 4, 2024
1 parent 29c64ca commit 66e9686
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@Transactional(readOnly = true)
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
List<User> findByEmailIn(Collection<String> emails);
List<User> findDistinctByEmailContainingAndRolesNameIsNot(@NotBlank String email, String rolesName);
Page<User> findDistinctByEmailContainingAndRolesNameIn(
@NotBlank String email, Collection<String> rolesName, Pageable pageable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import fr.centralesupelec.thuv.model.User;
import fr.centralesupelec.thuv.repository.RoleRepository;
import fr.centralesupelec.thuv.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -12,12 +14,14 @@
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

@Primary
@Service
public class MyUserDetailsService implements UserDetailsService {
private static final Logger logger = LoggerFactory.getLogger(UserDetailsService.class);
private final UserRepository userRepository;
private final RoleRepository roleRepository;

Expand All @@ -38,13 +42,27 @@ public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException
}

public User upsertUser(String email, String name, String lastName) {
// Eager loading gets roles as well
User user = userRepository.findByEmail(email)
.orElseGet(() -> {
User newUser = new User();
newUser.setEmail(email);
return newUser;
});
return this.upsertUser(new String[]{email}, name, lastName);
}

public User upsertUser(String[] emails, String name, String lastName) {
List<User> users = userRepository.findByEmailIn(Arrays.stream(emails).toList());
logger.debug(
"Found users {} for emails {}",
Arrays.toString(users.stream().map(User::getId).toArray()),
Arrays.toString(emails)
);
User user;
if (users.size() > 1) {
logger.error("Multiple users found with emails {}, using the first one", Arrays.toString(emails));
user = users.get(0);
} else if (users.size() == 1) {
user = users.get(0);
logger.debug("Upserting user {}", user.getId());
} else {
user = new User();
user.setEmail(emails[0]);
}
// Update with latest informations
user.setName(name);
user.setLastname(lastName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public String getLocalTokenFromOIDCToken(String oidcToken) {
DecodedJWT jwt = JWT.decode(oidcToken);
this.verifyToken(jwt, 0);

String email = jwt.getClaim("preferred_username").asString();
String username = jwt.getClaim("preferred_username").asString();
String email = jwt.getClaim("email").asString();
String name = jwt.getClaim("given_name").asString();
String lastName = jwt.getClaim("family_name").asString();

// FindorCreate user

User user = myUserDetailsService.upsertUser(email, name, lastName);
User user = myUserDetailsService.upsertUser(new String[]{username, email}, name, lastName);
List<String> roles = (List<String>) jwt.getClaim("realm_access").asMap().get("roles");
if (roles.contains("teacher")) {
myUserDetailsService.ensureTeacher(user);
Expand Down

0 comments on commit 66e9686

Please sign in to comment.