patch session utilisateur

This commit is contained in:
jleroy
2024-04-24 08:50:09 +02:00
6 changed files with 93 additions and 14 deletions

View File

@@ -1,24 +1,22 @@
package fr.eni.enchere.controllers; package fr.eni.enchere.controllers;
import fr.eni.enchere.bo.UserProfil; import fr.eni.enchere.bo.UserProfil;
import fr.eni.enchere.dal.UserRepository;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bll.UserService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@Controller() @Controller()
@RequestMapping("/profile") @RequestMapping("/profil")
public class ProfileController { public class ProfilController {
private final UserService userService; private final UserService userService;
public ProfileController(UserService userService) { public ProfilController(UserService userService) {
this.userService = userService; this.userService = userService;
} }
@@ -27,7 +25,7 @@ public class ProfileController {
// Obtenez l'authentification actuelle // Obtenez l'authentification actuelle
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Vérifiez si l'utilisateur est authentifié // Vérifiez si l'utilisateur est authentifié
if (!authentication.getName().equals("anonymousUser")) { //Retirer le true pour le bon fonctionnement if (!authentication.getName().equals("anonymousUser")) {
// Obtenez les détails de l'utilisateur authentifié // Obtenez les détails de l'utilisateur authentifié
String username = authentication.getName(); String username = authentication.getName();
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur // Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
@@ -35,14 +33,33 @@ public class ProfileController {
// Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML // Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML
// model.addAttribute("user", new UserProfil()); // model.addAttribute("user", new UserProfil());
model.addAttribute("userProfile", userProfile); model.addAttribute("userProfile", userProfile);
return "profile"; return "profil";
}else {
return "accueil";
}
}
@PostMapping("/edit")
public String editProfile(Model model) {
// Obtenez l'authentification actuelle
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Vérifiez si l'utilisateur est authentifié
if (!authentication.getName().equals("anonymousUser")) {
// Obtenez les détails de l'utilisateur authentifié
String username = authentication.getName();
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
UserProfil userProfile = userService.utilisateurByName(username);
// Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML
// model.addAttribute("user", new UserProfil());
model.addAttribute("userProfile", userProfile);
return "editProfil";
}else { }else {
return "accueil"; return "accueil";
} }
} }
@PostMapping("/delete") @PostMapping("/delete")
public String setUser() { public String deleteUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Obtenez les détails de l'utilisateur authentifié // Obtenez les détails de l'utilisateur authentifié
String username = authentication.getName(); String username = authentication.getName();

View File

@@ -17,9 +17,9 @@ public class WebSecurityConfig{
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/accueil").permitAll() .requestMatchers("/", "/accueil").permitAll()
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/profile/**", "/change-language").permitAll() .requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/article/**", "/change-language", "/profile/**").permitAll()
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll() .requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll()
.requestMatchers("/article/**").authenticated() .requestMatchers("/profile/**").authenticated()
.requestMatchers("/admin").hasRole("ADMIN") .requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated()) .anyRequest().authenticated())
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true)) .formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="fr" th:replace="~{modele-page :: layout('Profile',~{::link} , ~{::#container-main})}" xmlns:th="http://www.thymeleaf.org"> <html lang="fr" th:replace="~{modele-page :: layout('Modifier votre profile',~{::link} , ~{::#container-main})}" xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
</head> </head>
@@ -107,6 +107,17 @@
</span> </span>
</div> </div>
<!-- Champ Mot de passe--> <!-- Champ Mot de passe-->
<div class="champ-saisie">
<label for="password">Mot de passe actuel: </label>
<div>
<input type="password" th:field="*{password}" id="password" />
</div>
<span style="color: red;" th:if="${#fields.hasErrors('password')}">
<ul>
<li th:each="erreur: ${#fields.errors('password')}" th:text="${erreur}"></li>
</ul>
</span>
</div>
<div class="champ-saisie"> <div class="champ-saisie">
<label for="password">Nouveau mot de passe: </label> <label for="password">Nouveau mot de passe: </label>
<div> <div>
@@ -125,10 +136,11 @@
</div> </div>
</div> </div>
<div>Crédits: <span th:text="${userProfile.credit}"></span></div> <div>Crédits: <span th:text="${userProfile.credit}"></span></div>
<!-- Input cacher qui permet de stocker l'id du compte utilisateur à mettre à jour -->
<input type="hidden" id="userId" name="userId" th:field="*{id}" th:value="${userProfile.id}" /> <input type="hidden" id="userId" name="userId" th:field="*{id}" th:value="${userProfile.id}" />
<input type="submit" value="Enregistrer"/> <input type="submit" value="Enregistrer"/>
</form> </form>
<form th:action="@{/profile/delete}" method="post"> <form th:action="@{/profil/delete}" method="post">
<button type="submit">Supprimer mon compte</button> <button type="submit">Supprimer mon compte</button>
</form> </form>
</div> </div>

View File

@@ -125,7 +125,7 @@
</div> </div>
<input type="submit" value="Créer" /> <input type="submit" value="Créer" />
</form> </form>
<form action="/" method="post"> <form th:action="@{/accueil}" method="post">
<button type="submit">Annuler</button> <button type="submit">Annuler</button>
</form> </form>
</div> </div>

View File

@@ -25,7 +25,7 @@
<a class="nav-link" href="#">Vendre un article</a> <a class="nav-link" href="#">Vendre un article</a>
</li> </li>
<li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}"> <li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}">
<a class="nav-link" href="/profile">Mon profile</a> <a class="nav-link" href="/profil">Mon profile</a>
</li> </li>
<li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}"> <li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true" >Déconnection</a> <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true" >Déconnection</a>

View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="fr" th:replace="~{modele-page :: layout('Votre profile',~{::link} , ~{::#container-main})}" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="container-main">
<h1>Mon profil</h1>
<div class="champ-affichage">
<label>Pseudo: </label>
<span th:text="${userProfile.pseudo}"></span>
</div>
<div class="champ-affichage">
<label>Prénom: </label>
<span th:text="${userProfile.prenom}"></span>
</div>
<div class="champ-affichage">
<label>Nom: </label>
<span th:text="${userProfile.nom}"></span>
</div>
<div class="champ-affichage">
<label>Email: </label>
<span th:text="${userProfile.email}"></span>
</div>
<div class="champ-affichage">
<label>Téléphone: </label>
<span th:text="${userProfile.telephone}"></span>
</div>
<div class="champ-affichage">
<label>Rue: </label>
<span th:text="${userProfile.rue}"></span>
</div>
<div class="champ-affichage">
<label>Code postal: </label>
<span th:text="${userProfile.code_postal}"></span>
</div>
<div class="champ-affichage">
<label>Ville: </label>
<span th:text="${userProfile.ville}"></span>
</div>
<div class="champ-affichage">
<label>Crédits: </label>
<span th:text="${userProfile.credit}"></span>
</div>
<form th:action="@{/profil/edit}" method="post">
<button type="submit">Modifier</button>
</form>
</div>
</body>
</html>