| 1 | package com.edtech.service; | |
| 2 | ||
| 3 | import com.edtech.dto.RegisterRequestDto; | |
| 4 | import com.edtech.model.User; | |
| 5 | import com.edtech.model.UserRole; | |
| 6 | import com.edtech.repository.UserRepository; | |
| 7 | import java.util.Locale; | |
| 8 | import java.util.Optional; | |
| 9 | import org.springframework.security.crypto.password.PasswordEncoder; | |
| 10 | import org.springframework.stereotype.Service; | |
| 11 | import org.springframework.transaction.annotation.Transactional; | |
| 12 | ||
| 13 | /** Documentação para UserService. */ | |
| 14 | @Service | |
| 15 | public class UserService { | |
| 16 | ||
| 17 | private static final String INSTITUTIONAL_DOMAIN = "@unb.br"; | |
| 18 | ||
| 19 | private final UserRepository userRepository; | |
| 20 | private final PasswordEncoder passwordEncoder; | |
| 21 | private final com.edtech.repository.VerificationTokenRepository verificationTokenRepository; | |
| 22 | private final EmailService emailService; | |
| 23 | private static final java.security.SecureRandom SECURE_RANDOM = new java.security.SecureRandom(); | |
| 24 | ||
| 25 | /** Documentação para o método UserService. */ | |
| 26 | public UserService( | |
| 27 | UserRepository userRepository, | |
| 28 | PasswordEncoder passwordEncoder, | |
| 29 | com.edtech.repository.VerificationTokenRepository verificationTokenRepository, | |
| 30 | EmailService emailService) { | |
| 31 | this.userRepository = userRepository; | |
| 32 | this.passwordEncoder = passwordEncoder; | |
| 33 | this.verificationTokenRepository = verificationTokenRepository; | |
| 34 | this.emailService = emailService; | |
| 35 | } | |
| 36 | ||
| 37 | /** Documentação. */ | |
| 38 | @Transactional(readOnly = true) | |
| 39 | public User authenticate(String email, String password) { | |
| 40 | String normalizedEmail = email.trim().toLowerCase(Locale.ROOT); | |
| 41 | User user = | |
| 42 | userRepository | |
| 43 | .findByEmailIgnoreCase(normalizedEmail) | |
| 44 | .filter(User::isActive) | |
| 45 |
1
1. lambda$authenticate$0 : replaced return value with null for com/edtech/service/UserService::lambda$authenticate$0 → KILLED |
.orElseThrow(() -> new InvalidCredentialsException("Credenciais inválidas.")); |
| 46 | ||
| 47 |
1
1. authenticate : negated conditional → KILLED |
if (!passwordEncoder.matches(password, user.getPasswordHash())) { |
| 48 | throw new InvalidCredentialsException("Credenciais inválidas."); | |
| 49 | } | |
| 50 | ||
| 51 | boolean roleChanged = false; | |
| 52 |
2
1. authenticate : negated conditional → KILLED 2. authenticate : negated conditional → KILLED |
if (normalizedEmail.contains("auditor") && user.getRole() != UserRole.AUDITOR) { |
| 53 |
1
1. authenticate : removed call to com/edtech/model/User::setRole → KILLED |
user.setRole(UserRole.AUDITOR); |
| 54 | roleChanged = true; | |
| 55 |
2
1. authenticate : negated conditional → SURVIVED 2. authenticate : negated conditional → KILLED |
} else if ((normalizedEmail.contains("orientador") || normalizedEmail.contains("advisor")) |
| 56 |
1
1. authenticate : negated conditional → KILLED |
&& user.getRole() != UserRole.ADVISOR) { |
| 57 |
1
1. authenticate : removed call to com/edtech/model/User::setRole → KILLED |
user.setRole(UserRole.ADVISOR); |
| 58 | roleChanged = true; | |
| 59 | } | |
| 60 | ||
| 61 |
1
1. authenticate : negated conditional → KILLED |
if (roleChanged) { |
| 62 | userRepository.save(user); | |
| 63 | } | |
| 64 | ||
| 65 |
1
1. authenticate : replaced return value with null for com/edtech/service/UserService::authenticate → KILLED |
return user; |
| 66 | } | |
| 67 | ||
| 68 | /** Documentação. */ | |
| 69 | @Transactional | |
| 70 | public User register(RegisterRequestDto request) { | |
| 71 | String normalizedEmail = request.email().trim().toLowerCase(Locale.ROOT); | |
| 72 | ||
| 73 |
2
1. register : negated conditional → KILLED 2. register : negated conditional → KILLED |
if (!normalizedEmail.endsWith("@unb.br") && !normalizedEmail.endsWith(".unb.br")) { |
| 74 | throw new InvalidInstitutionalEmailException("O e-mail deve pertencer ao dominio unb.br."); | |
| 75 | } | |
| 76 | ||
| 77 |
1
1. register : negated conditional → KILLED |
if (userRepository.existsByEmailIgnoreCase(normalizedEmail)) { |
| 78 | throw new DuplicateEmailException("E-mail ja cadastrado."); | |
| 79 | } | |
| 80 | ||
| 81 | UserRole initialRole = request.role(); | |
| 82 | ||
| 83 | User user = | |
| 84 | new User( | |
| 85 | request.name().trim(), | |
| 86 | normalizedEmail, | |
| 87 | passwordEncoder.encode(request.password()), | |
| 88 | initialRole, | |
| 89 | java.util.UUID.fromString("00000000-0000-0000-0000-000000000001")); | |
| 90 | ||
| 91 |
1
1. register : removed call to com/edtech/model/User::setActive → KILLED |
user.setActive(false); |
| 92 | userRepository.save(user); | |
| 93 | ||
| 94 |
1
1. register : removed call to com/edtech/repository/VerificationTokenRepository::deleteByEmail → KILLED |
verificationTokenRepository.deleteByEmail(normalizedEmail); |
| 95 | String code = String.format("%06d", SECURE_RANDOM.nextInt(999999)); | |
| 96 | com.edtech.model.VerificationToken token = | |
| 97 | new com.edtech.model.VerificationToken( | |
| 98 | code, normalizedEmail, java.time.LocalDateTime.now().plusMinutes(15)); | |
| 99 | verificationTokenRepository.save(token); | |
| 100 | ||
| 101 |
1
1. register : removed call to com/edtech/service/EmailService::sendVerificationEmail → KILLED |
emailService.sendVerificationEmail(normalizedEmail, code); |
| 102 | ||
| 103 |
1
1. register : replaced return value with null for com/edtech/service/UserService::register → KILLED |
return user; |
| 104 | } | |
| 105 | ||
| 106 | /** Documentação. */ | |
| 107 | @Transactional | |
| 108 | public User verifyRegistration(String email, String code) { | |
| 109 | String normalizedEmail = email.trim().toLowerCase(Locale.ROOT); | |
| 110 | Optional<com.edtech.model.VerificationToken> tokenOpt = | |
| 111 | verificationTokenRepository.findByEmailAndToken(normalizedEmail, code); | |
| 112 | ||
| 113 |
2
1. verifyRegistration : negated conditional → KILLED 2. verifyRegistration : negated conditional → KILLED |
if (tokenOpt.isPresent() && !tokenOpt.get().isExpired()) { |
| 114 | Optional<User> userOpt = userRepository.findByEmailIgnoreCase(normalizedEmail); | |
| 115 |
1
1. verifyRegistration : negated conditional → KILLED |
if (userOpt.isPresent()) { |
| 116 | User user = userOpt.get(); | |
| 117 |
1
1. verifyRegistration : removed call to com/edtech/model/User::setActive → SURVIVED |
user.setActive(true); |
| 118 | userRepository.save(user); | |
| 119 |
1
1. verifyRegistration : removed call to com/edtech/repository/VerificationTokenRepository::deleteByEmail → SURVIVED |
verificationTokenRepository.deleteByEmail(normalizedEmail); |
| 120 |
1
1. verifyRegistration : replaced return value with null for com/edtech/service/UserService::verifyRegistration → KILLED |
return user; |
| 121 | } | |
| 122 | } | |
| 123 | throw new InvalidCredentialsException("Código inválido ou expirado."); | |
| 124 | } | |
| 125 | ||
| 126 | @Transactional | |
| 127 | public User saveUserWithoutHash(User user) { | |
| 128 |
1
1. saveUserWithoutHash : replaced return value with null for com/edtech/service/UserService::saveUserWithoutHash → SURVIVED |
return userRepository.save(user); |
| 129 | } | |
| 130 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 47 |
1.1 |
|
| 52 |
1.1 2.2 |
|
| 53 |
1.1 |
|
| 55 |
1.1 2.2 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |
|
| 61 |
1.1 |
|
| 65 |
1.1 |
|
| 73 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 91 |
1.1 |
|
| 94 |
1.1 |
|
| 101 |
1.1 |
|
| 103 |
1.1 |
|
| 113 |
1.1 2.2 |
|
| 115 |
1.1 |
|
| 117 |
1.1 |
|
| 119 |
1.1 |
|
| 120 |
1.1 |
|
| 128 |
1.1 |