allo
This commit is contained in:
31
src/main/java/fr/eni/enchere/config/WebConfig.java
Normal file
31
src/main/java/fr/eni/enchere/config/WebConfig.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package fr.eni.enchere.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver resolver = new SessionLocaleResolver();
|
||||
resolver.setDefaultLocale(Locale.FRENCH); // Définit la locale par défaut
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
|
||||
source.setBasenames("i18n/messages");
|
||||
source.setDefaultEncoding("UTF-8");
|
||||
source.setUseCodeAsDefaultMessage(true);
|
||||
return source;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import fr.eni.enchere.bll.ArticleService;
|
||||
import fr.eni.enchere.bll.CategorieService;
|
||||
import fr.eni.enchere.bo.Article;
|
||||
import fr.eni.enchere.bo.SearchArticleCritere;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -12,9 +14,11 @@ 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.RequestParam;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
|
||||
@Controller
|
||||
@@ -24,6 +28,7 @@ public class AccueilController {
|
||||
private ArticleService articleService;
|
||||
private CategorieService categorieService;
|
||||
|
||||
|
||||
public AccueilController(ArticleService articleService, CategorieService categorieService) {
|
||||
super();
|
||||
this.categorieService = categorieService;
|
||||
@@ -32,7 +37,6 @@ public class AccueilController {
|
||||
|
||||
@GetMapping({"/", "/accueil"})
|
||||
public String viewAccueil(@RequestParam(required = false) String searchTitle, @RequestParam(required = false) Integer searchCategory, Model model) {
|
||||
System.out.println("Liste cat: "+categorieService.findAllCategories());
|
||||
model.addAttribute("categories", categorieService.findAllCategories());
|
||||
SearchArticleCritere critere = new SearchArticleCritere();
|
||||
critere.setNoCategorie(searchCategory);
|
||||
@@ -48,4 +52,5 @@ public class AccueilController {
|
||||
return viewAccueil(searchTitle, searchCategory, model);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import fr.eni.enchere.bll.ArticleService;
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import fr.eni.enchere.bo.Article;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller()
|
||||
@RequestMapping("/article")
|
||||
public class ArticleController {
|
||||
|
||||
private final ArticleService articleService;
|
||||
private final UserService userService;
|
||||
|
||||
public ArticleController(ArticleService articleService, UserService userService) {
|
||||
this.articleService = articleService;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String viewArticle(Model model) {
|
||||
return "accueil";
|
||||
}
|
||||
|
||||
@GetMapping("/article")
|
||||
public String showArticle(@RequestParam(name = "slug")int id, Model model) {
|
||||
Article article = articleService.findArticleById(id);
|
||||
return "article";
|
||||
}
|
||||
|
||||
@GetMapping("/{slug}")
|
||||
public String testShowArticle(@PathVariable(name = "slug")int id, Model model) {
|
||||
Article article = articleService.findArticleById(id);
|
||||
model.addAttribute("article", article);
|
||||
return "article";
|
||||
}
|
||||
|
||||
@GetMapping("/new")
|
||||
public String test(@PathVariable(name = "slug")int id, Model model) {
|
||||
|
||||
return "article";
|
||||
}
|
||||
|
||||
@PostMapping("/new/add")
|
||||
public String newArticle(@ModelAttribute("article") Article article) {
|
||||
articleService.saveArticle(article);
|
||||
return "redirect:/article";
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public String updateArticle() {
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public String deleteArticle() {
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Controller
|
||||
public class LanguageController {
|
||||
|
||||
@GetMapping("/change-language")
|
||||
public String changeLanguage(HttpServletRequest request, HttpServletResponse response, @RequestParam("lang") String lang, @RequestHeader String referer) {
|
||||
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
|
||||
if (localeResolver != null) {
|
||||
localeResolver.setLocale(request, response, new Locale(lang));
|
||||
}
|
||||
return "redirect:" + referer;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,22 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
import fr.eni.enchere.dal.UserRepository;
|
||||
import org.springframework.ui.Model;
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
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.RequestMapping;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
@Controller()
|
||||
@RequestMapping("/profile")
|
||||
public class ProfileController {
|
||||
@RequestMapping("/profil")
|
||||
public class ProfilController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public ProfileController(UserService userService) {
|
||||
public ProfilController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@@ -27,7 +25,7 @@ public class ProfileController {
|
||||
// Obtenez l'authentification actuelle
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// 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é
|
||||
String username = authentication.getName();
|
||||
// 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
|
||||
// model.addAttribute("user", new UserProfil());
|
||||
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 {
|
||||
return "accueil";
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public String setUser() {
|
||||
public String deleteUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// Obtenez les détails de l'utilisateur authentifié
|
||||
String username = authentication.getName();
|
||||
@@ -11,7 +11,6 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@@ -101,6 +100,12 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
//Mettre à jour
|
||||
String sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
if(utilisateur.getPassword().isEmpty()){
|
||||
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville WHERE no_utilisateur = :id";
|
||||
}else{
|
||||
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
||||
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getPassword()));
|
||||
}
|
||||
parameters.addValue("pseudo", utilisateur.getPseudo());
|
||||
parameters.addValue("nom", utilisateur.getNom());
|
||||
parameters.addValue("prenom", utilisateur.getPrenom());
|
||||
|
||||
@@ -17,9 +17,9 @@ public class WebSecurityConfig{
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeHttpRequests((requests) -> requests
|
||||
.requestMatchers("/", "/accueil").permitAll()
|
||||
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/profile/**").permitAll()
|
||||
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/article/**", "/change-language", "/profile/**").permitAll()
|
||||
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll()
|
||||
.requestMatchers("/article/**").authenticated()
|
||||
.requestMatchers("/profile/**").authenticated()
|
||||
.requestMatchers("/admin").hasRole("ADMIN")
|
||||
.anyRequest().authenticated())
|
||||
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))
|
||||
|
||||
Reference in New Issue
Block a user