diff --git a/src/main/java/com/projet/projetIndu/ProjetInduApplication.java b/src/main/java/com/projet/projetIndu/ProjetInduApplication.java index ab5f191cad9d2f52ef1d5f5ffaf37ede7850ba16..a7037d536d98754e6f63519370decb47c03f3cd5 100644 --- a/src/main/java/com/projet/projetIndu/ProjetInduApplication.java +++ b/src/main/java/com/projet/projetIndu/ProjetInduApplication.java @@ -1,27 +1,21 @@ package com.projet.projetIndu; -import com.projet.projetIndu.entities.Role; -import com.projet.projetIndu.entities.User; -import com.projet.projetIndu.services.UserService; -import jakarta.annotation.PostConstruct; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProjetInduApplication { - @Autowired - private UserService userService; +// @Autowired +// private UserService userService; +// +// @PostConstruct +// public void init() { +// User aichatou = userService.registerUser("aichatou", "test", Role.ADMIN); +// System.out.println(aichatou); +// } - @PostConstruct - public void init() { - User aichatou = userService.registerUser("aichatou", "test", Role.ADMIN); - System.out.println(aichatou); - } - - public static void main(String[] args) { - SpringApplication.run(ProjetInduApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ProjetInduApplication.class, args); + } } diff --git a/src/main/java/com/projet/projetIndu/config/SecurityConfig.java b/src/main/java/com/projet/projetIndu/config/SecurityConfig.java deleted file mode 100644 index 481194185edcd6b552908a3bacf76340bfa93350..0000000000000000000000000000000000000000 --- a/src/main/java/com/projet/projetIndu/config/SecurityConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.projet.projetIndu.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.web.SecurityFilterChain; - -@Configuration -public class SecurityConfig { - - @Bean - public BCryptPasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - @Bean - public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http - .csrf(csrf -> csrf.disable()) // Désactiver CSRF pour les tests (à activer en prod) - .authorizeHttpRequests(auth -> auth - .requestMatchers("/", "/register", "/login", "/css/**", "/js/**").permitAll() // Pages publiques - .requestMatchers("/dashboard/patient").hasAuthority("PATIENT") // Tableau de bord patient - .requestMatchers("/dashboard/doctor").hasAuthority("DOCTOR") // Tableau de bord médecin - .requestMatchers("/medical-files/**").authenticated() // Tout utilisateur connecté peut voir ses dossiers médicaux - .anyRequest().authenticated() - ) - .formLogin(form -> form - .loginPage("/login") - .defaultSuccessUrl("/dashboard", true) // Redirection après connexion - .permitAll() - ) - .logout(logout -> logout - .logoutUrl("/logout") - .logoutSuccessUrl("/login?logout") - .permitAll() - ); - - return http.build(); - } -} diff --git a/src/main/java/com/projet/projetIndu/controllers/AuthController.java b/src/main/java/com/projet/projetIndu/controllers/AuthController.java index 4c7a21ede1e20ac28f4e462d3387899dc06fabe4..8a20b5ddb10029e855fbddd0adc5dab3dd0dbf34 100644 --- a/src/main/java/com/projet/projetIndu/controllers/AuthController.java +++ b/src/main/java/com/projet/projetIndu/controllers/AuthController.java @@ -1,11 +1,15 @@ package com.projet.projetIndu.controllers; +import com.projet.projetIndu.dto.UserRegistrationDTO; import com.projet.projetIndu.entities.Role; -import com.projet.projetIndu.entities.User; import com.projet.projetIndu.services.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.time.LocalDate; @Controller public class AuthController { @@ -24,24 +28,52 @@ public class AuthController { // Traite l'inscription @PostMapping("/register") public String registerUser( - @RequestParam String username, + @RequestParam String firstName, + @RequestParam String lastName, + @RequestParam String email, @RequestParam String password, - @RequestParam Role role, + @RequestParam String role, + @RequestParam(required = false) String dateOfBirth, // Facultatif + @RequestParam(required = false) String address, // Facultatif + @RequestParam(required = false) String speciality, // Facultatif Model model) { - if (userService.getUserByUsername(username).isPresent()) { + if (userService.getUserByUsername(email).isPresent()) { model.addAttribute("error", "L'utilisateur existe déjà !"); return "register"; } - // Enregistrer l'utilisateur - User newUser = userService.registerUser(username, password, role); + Role userRole; + try { + userRole = Role.valueOf(role.toUpperCase()); + } catch (IllegalArgumentException e) { + model.addAttribute("error", "Rôle invalide !"); + return "register"; + } + + UserRegistrationDTO dto = new UserRegistrationDTO(); + dto.setFirstName(firstName); + dto.setLastName(lastName); + dto.setEmail(email); + dto.setPassword(password); + dto.setRole(userRole); - // Redirection selon le rôle - if (newUser.getRole() == Role.DOCTOR) { - return "redirect:/doctor-dashboard"; - } else { - return "redirect:/patient-dashboard"; + if (userRole == Role.PATIENT) { + dto.setDateOfBirth(dateOfBirth != null ? LocalDate.parse(dateOfBirth) : null); + dto.setAddress(address); + } else if (userRole == Role.DOCTOR) { + dto.setSpeciality(speciality); } + + // Enregistrer l'utilisateur + try { + userService.registerUser(dto); + } catch (IllegalArgumentException e) { + model.addAttribute("error", e.getMessage()); + return "register"; + } + + return "redirect:/login?registered=true"; + } } diff --git a/src/main/java/com/projet/projetIndu/controllers/DashboardController.java b/src/main/java/com/projet/projetIndu/controllers/DashboardController.java deleted file mode 100644 index 37d97daba37cfae63df30ef1e3a9cd4a2cb066f8..0000000000000000000000000000000000000000 --- a/src/main/java/com/projet/projetIndu/controllers/DashboardController.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.projet.projetIndu.controllers; - -import org.springframework.security.core.Authentication; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -@Controller -@RequestMapping("/dashboard") -public class DashboardController { - - @GetMapping - public String redirectDashboard(Authentication authentication) { - System.out.println("Roles de l'utilisateur: " + authentication.getAuthorities()); - - if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_DOCTOR"))) { - return "redirect:/dashboard/doctor"; - } else if (authentication.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_PATIENT"))) { - return "redirect:/dashboard/patient"; - } - return "redirect:/login"; - } - - - @GetMapping("/patient") - public String patientDashboard() { - System.out.println("Redirection vers le dashboard patient..."); - return "patient-dashboard"; - } - - @GetMapping("/doctor") - public String doctorDashboard() { - return "doctor-dashboard"; // Vue pour le médecin - } -} - diff --git a/src/main/java/com/projet/projetIndu/controllers/LoginController.java b/src/main/java/com/projet/projetIndu/controllers/LoginController.java index c444a66db08224e96c5a657fd0c7d94a377664f0..c543594981c451c97858355611e2ed9a7628d76e 100644 --- a/src/main/java/com/projet/projetIndu/controllers/LoginController.java +++ b/src/main/java/com/projet/projetIndu/controllers/LoginController.java @@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.GetMapping; public class LoginController { @GetMapping("/login") - public String login() { + public String loginPage() { return "login"; } } diff --git a/src/main/java/com/projet/projetIndu/controllers/PatientController.java b/src/main/java/com/projet/projetIndu/controllers/PatientController.java new file mode 100644 index 0000000000000000000000000000000000000000..ceb65ab10c362058f1f1be272927899782dafd17 --- /dev/null +++ b/src/main/java/com/projet/projetIndu/controllers/PatientController.java @@ -0,0 +1,10 @@ +package com.projet.projetIndu.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@RequestMapping("/patients") // doit concorder avec la route dans le SecurityConfig +@Controller +public class PatientController { + +} diff --git a/src/main/java/com/projet/projetIndu/controllers/UserController.java b/src/main/java/com/projet/projetIndu/controllers/UserController.java deleted file mode 100644 index beeefe3c6ead8f6d96f98e636ae58906aa4ae386..0000000000000000000000000000000000000000 --- a/src/main/java/com/projet/projetIndu/controllers/UserController.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.projet.projetIndu.controllers; - -import com.projet.projetIndu.dto.UserRegistrationDTO; -import com.projet.projetIndu.services.UserService; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; - -@Controller -public class UserController { - private final UserService userService; - - public UserController(UserService userService) { - this.userService = userService; - } - - @GetMapping("/register") - public String showRegistrationForm(Model model) { - model.addAttribute("userDTO", new UserRegistrationDTO()); - return "register"; - } - - @PostMapping("/register") - public String registerUser(@ModelAttribute("userDTO") UserRegistrationDTO userDTO, Model model) { - try { - userService.registerUser(userDTO); - return "redirect:/login"; - } catch (IllegalArgumentException e) { - model.addAttribute("error", e.getMessage()); - return "register"; - } - } -} diff --git a/src/main/java/com/projet/projetIndu/dto/RegisterRequest.java b/src/main/java/com/projet/projetIndu/dto/RegisterRequest.java deleted file mode 100644 index e5a363d2512e5f60bae6d2e60b42a0029c904adc..0000000000000000000000000000000000000000 --- a/src/main/java/com/projet/projetIndu/dto/RegisterRequest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.projet.projetIndu.dto; - -import com.projet.projetIndu.entities.Role; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class RegisterRequest { - private String username; - private String password; - private Role role; -} diff --git a/src/main/java/com/projet/projetIndu/security/SecurityConfig.java b/src/main/java/com/projet/projetIndu/security/SecurityConfig.java index 51f4368f0bd663cd4c7436f1cff46cc6bc965784..da719e5a33f845f3eb6f4d628f3835459ad7e32b 100644 --- a/src/main/java/com/projet/projetIndu/security/SecurityConfig.java +++ b/src/main/java/com/projet/projetIndu/security/SecurityConfig.java @@ -21,17 +21,16 @@ public class SecurityConfig { return new BCryptPasswordEncoder(); } - @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http.authorizeHttpRequests(auth -> auth - .requestMatchers("/", "/login", "/register", "/css/**", "/js/**").permitAll() + http + .authorizeHttpRequests(auth -> auth + .requestMatchers("/", "/register", "/login", "/css/**", "/js/**").permitAll() // Pages publiques .requestMatchers("/admin/**").hasAuthority("ADMIN") .requestMatchers("/doctors/**").hasAuthority("DOCTOR") .requestMatchers("/patients/**").hasAuthority("PATIENT") .anyRequest().authenticated() ) - .formLogin(form -> form .loginPage("/login") .usernameParameter("email") @@ -89,5 +88,4 @@ public class SecurityConfig { return new GrantedAuthorityDefaults(""); } - -} \ No newline at end of file +} diff --git a/src/main/java/com/projet/projetIndu/services/UserService.java b/src/main/java/com/projet/projetIndu/services/UserService.java index e731fcb8b2f405fb6b4100d7e201ae7b798d4624..ba62a2d3a93967a42ad289ac9ffd1c6572841ab7 100644 --- a/src/main/java/com/projet/projetIndu/services/UserService.java +++ b/src/main/java/com/projet/projetIndu/services/UserService.java @@ -1,7 +1,10 @@ package com.projet.projetIndu.services; import com.projet.projetIndu.dto.UserRegistrationDTO; -import com.projet.projetIndu.entities.*; +import com.projet.projetIndu.entities.Admin; +import com.projet.projetIndu.entities.Doctor; +import com.projet.projetIndu.entities.Patient; +import com.projet.projetIndu.entities.User; import com.projet.projetIndu.repositories.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; @@ -53,29 +56,31 @@ public class UserService implements UserDetailsService { String encodedPassword = passwordEncoder.encode(userDTO.getPassword()); User user; - Role role; - try { - role = Role.valueOf(String.valueOf(userDTO.getRole())); - - switch (role) { - case DOCTOR -> { - Doctor doctor = new Doctor(); - doctor.setSpeciality(userDTO.getSpeciality()); - user = doctor; + switch (userDTO.getRole()) { + case DOCTOR -> { + if (userDTO.getSpeciality() == null || userDTO.getSpeciality().isEmpty()) { + throw new IllegalArgumentException("Speciality is required for doctors"); } - case PATIENT -> { - Patient patient = new Patient(); - patient.setDateOfBirth(userDTO.getDateOfBirth()); - patient.setAddress(userDTO.getAddress()); - user = patient; + Doctor doctor = new Doctor(); + doctor.setSpeciality(userDTO.getSpeciality()); + user = doctor; + } + case PATIENT -> { + if (userDTO.getDateOfBirth() == null) { + throw new IllegalArgumentException("Date of birth is required for patients"); } - case ADMIN -> { - user = new Admin(); + if (userDTO.getAddress() == null || userDTO.getAddress().isBlank()) { + throw new IllegalArgumentException("Address is required for patients"); } - default -> throw new IllegalArgumentException("Invalid role"); + Patient patient = new Patient(); + patient.setDateOfBirth(userDTO.getDateOfBirth()); + patient.setAddress(userDTO.getAddress()); + user = patient; } - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("Invalid role provided: " + userDTO.getRole()); + case ADMIN -> { + user = new Admin(); + } + default -> throw new IllegalArgumentException("Invalid role"); } user.setFirstName(userDTO.getFirstName()); @@ -83,8 +88,7 @@ public class UserService implements UserDetailsService { user.setPhoneNumber(userDTO.getPhoneNumber()); user.setEmail(userDTO.getEmail()); user.setPassword(encodedPassword); - user.setRole(role); - + user.setRole(userDTO.getRole()); return userRepository.save(user); } diff --git a/src/main/resources/templates/doctor-dashboard.html b/src/main/resources/templates/doctor-dashboard.html index b5115573e560ce0816588d9c867a22c2767d8f4d..b6c86c374b55ebf8b4dd70991f7615955d143da1 100644 --- a/src/main/resources/templates/doctor-dashboard.html +++ b/src/main/resources/templates/doctor-dashboard.html @@ -22,7 +22,8 @@ <main class="mt-12"> <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"> <h2 class="text-3xl font-bold text-gray-800 text-center">Bienvenue sur votre tableau de bord</h2> - <p class="mt-4 text-lg text-gray-600 text-center">Consultez vos dossiers médicaux et gérez vos patients efficacement.</p> + <p class="mt-4 text-lg text-gray-600 text-center">Consultez vos dossiers médicaux et gérez vos patients + efficacement.</p> <div class="mt-8 grid grid-cols-1 md:grid-cols-2 gap-6"> <a href="#" class="p-6 bg-white shadow-md rounded-lg hover:shadow-lg transition"> @@ -35,15 +36,15 @@ <p class="mt-2 text-gray-600">Accéder aux dossiers médicaux de vos patients.</p> </a> - <a href="/appointments" class="p-6 bg-white shadow-md rounded-lg hover:shadow-lg transition"> - <h3 class="text-xl font-semibold text-gray-800">Rendez-vous</h3> - <p class="mt-2 text-gray-600">Gérer votre planning et vos rendez-vous.</p> + <a href="/appointments" + class="p-6 bg-white shadow-md rounded-lg hover:shadow-lg transition md:col-span-2 flex items-center justify-center text-center"> + <div class="space-y-2"> + <h3 class="text-xl font-semibold text-gray-800">Rendez-vous</h3> + <p class="text-gray-600">Gérer votre planning et vos rendez-vous.</p> + </div> </a> - <a href="#" class="p-6 bg-white shadow-md rounded-lg hover:shadow-lg transition"> - <h3 class="text-xl font-semibold text-gray-800">Statistiques</h3> - <p class="mt-2 text-gray-600">Voir vos consultations et performances.</p> - </a> + </div> </div> </main> diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html index 5dbff8b24e3bd665934389ef8ff4146a63e16954..df68dc55dc35dfb0a8efb016397f11a902c4402f 100644 --- a/src/main/resources/templates/register.html +++ b/src/main/resources/templates/register.html @@ -6,6 +6,13 @@ <link rel="stylesheet" th:href="@{/css/styles.css}"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> + <script> + function toggleFields() { + var role = document.getElementById("role").value; + document.getElementById("doctorFields").style.display = (role === "DOCTOR") ? "block" : "none"; + document.getElementById("patientFields").style.display = (role === "PATIENT") ? "block" : "none"; + } + </script> </head> <body class="bg-light"> <div class="container d-flex justify-content-center align-items-center vh-100"> @@ -28,6 +35,36 @@ <label for="password" class="form-label">Mot de passe</label> <input type="password" class="form-control" id="password" name="password" required> </div> + <div class="mb-3"> + <label for="role" class="form-label">Rôle</label> + <select class="form-control" id="role" name="role" required onchange="toggleFields()"> + <option value="PATIENT">Patient</option> + <option value="DOCTOR">Médecin</option> + <option value="ADMIN">Administrateur</option> + </select> + </div> + + <!-- Champs spécifiques pour Médecin --> + <div id="doctorFields" style="display: none;"> + <div class="mb-3"> + <label for="speciality" class="form-label">Spécialité</label> + <input type="text" class="form-control" id="speciality" name="speciality"> + </div> + </div> + + <!-- Champs spécifiques pour Patient --> + <div id="patientFields"> + <div class="mb-3"> + <label for="dateOfBirth" class="form-label">Date de naissance</label> + <input type="date" class="form-control" id="dateOfBirth" name="dateOfBirth"> + </div> + <div class="mb-3"> + <label for="address" class="form-label">Adresse</label> + <input type="text" class="form-control" id="address" name="address"> + </div> + </div> + + <button type="submit" class="btn btn-success w-100">S'inscrire</button> </form> <div class="text-center mt-3">