mise gestion
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package com.pmu.betengine.service;
|
||||
|
||||
import com.pmu.betengine.model.User;
|
||||
import com.pmu.betengine.model.statut.StatutUser;
|
||||
import com.pmu.betengine.repository.UserRepository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -12,15 +17,47 @@ public class UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public UserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
// CREATE
|
||||
public User create(User user) {
|
||||
// 1. Vérification des champs obligatoires
|
||||
if (!StringUtils.hasText(user.getNom())) {throw new RuntimeException("Le nom est obligatoire");}
|
||||
if (!StringUtils.hasText(user.getPrenom())) {throw new RuntimeException("Le prénom est obligatoire");}
|
||||
if (!StringUtils.hasText(user.getIdentifiant())) {throw new RuntimeException("L'identifiant est obligatoire");}
|
||||
if (!StringUtils.hasText(user.getPassword())) {throw new RuntimeException("Le mot de passe est obligatoire");}
|
||||
if (user.getRoleId() == null) {throw new RuntimeException("L'ID du rôle est obligatoire");}
|
||||
// 2. Vérification de l'unicité
|
||||
if (userRepository.existsByIdentifiant(user.getIdentifiant())) {throw new RuntimeException("Cet identifiant existe déjà");}
|
||||
if (user.getMatriculeAgent() != null &&
|
||||
userRepository.existsByMatriculeAgent(user.getMatriculeAgent())) {throw new RuntimeException("Ce matricule agent existe déjà");}
|
||||
// 3. Validation du mot de passe
|
||||
if (!user.getPassword().matches("^(?=.*[0-9])(?=.*[a-zA-Z]).{8,}$")) {throw new RuntimeException("Le mot de passe doit contenir au moins 8 caractères, incluant lettres et chiffres");}
|
||||
// 4. Définition des valeurs par défaut et encodage du mot de passe
|
||||
user.setId(null);
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
user.setStatut(StatutUser.ACTIF.name());
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setUpdatedAt(LocalDateTime.now());
|
||||
user.setDerniereConnexion(null);
|
||||
// 5. Vérification des champs numériques
|
||||
if (user.getNombreIpAutorise() == null || user.getNombreIpAutorise() < 0) {
|
||||
user.setNombreIpAutorise(0);
|
||||
}
|
||||
if (user.getNombreIpAutoAutorise() == null || user.getNombreIpAutoAutorise() < 0) {
|
||||
user.setNombreIpAutoAutorise(0);
|
||||
}
|
||||
// 6. Nettoyage des chaînes
|
||||
user.setIdentifiant(user.getIdentifiant().trim().toLowerCase());
|
||||
if (user.getMatriculeAgent() != null) {
|
||||
user.setMatriculeAgent(user.getMatriculeAgent().trim().toUpperCase());
|
||||
}
|
||||
// 7. Enregistrement
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@@ -44,13 +81,13 @@ public class UserService {
|
||||
|
||||
return userRepository.save(user);
|
||||
|
||||
}).orElseThrow(() -> new RuntimeException("User not found"));
|
||||
}).orElseThrow(() -> new RuntimeException("Utilisateur introuvable"));
|
||||
}
|
||||
|
||||
// READ by ID
|
||||
public User getById(Long id) {
|
||||
return userRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
||||
.orElseThrow(() -> new RuntimeException("Utilisateur introuvable"));
|
||||
}
|
||||
|
||||
// READ all
|
||||
|
||||
Reference in New Issue
Block a user