Update permission accès
This commit is contained in:
@@ -6,7 +6,8 @@ import java.util.List;
|
||||
|
||||
public interface UserService {
|
||||
List<UserProfil> listeUtilisateurs();
|
||||
UserProfil utilisateur(int id);
|
||||
UserProfil utilisateurById(int id);
|
||||
UserProfil utilisateurByName(String username);
|
||||
void setUtilisateur(UserProfil utilisateur);
|
||||
void deleteUtilisateur(int id);
|
||||
}
|
||||
|
||||
@@ -21,10 +21,15 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserProfil utilisateur(int id) {
|
||||
public UserProfil utilisateurById(int id) {
|
||||
return userRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserProfil utilisateurByName(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUtilisateur(UserProfil utilisateur) {
|
||||
userRepository.save(utilisateur);
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/inscription")
|
||||
public class InscriptionController {
|
||||
|
||||
@Autowired
|
||||
private final UserService userService;
|
||||
|
||||
public InscriptionController(UserService userService) {
|
||||
@@ -27,6 +27,7 @@ public class InscriptionController {
|
||||
|
||||
@PostMapping("/newUser")
|
||||
public String setUser(@ModelAttribute UserProfil user) {
|
||||
//Ajouter vérification du formulaire -> @RequestParam("confirmPassword") String confirmPassword
|
||||
userService.setUtilisateur(user);
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
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")
|
||||
@@ -19,7 +24,34 @@ public class ProfileController {
|
||||
|
||||
@GetMapping
|
||||
public String viewProfile(Model model) {
|
||||
// Obtenez l'authentification actuelle
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// Vérifiez si l'utilisateur est authentifié
|
||||
if (!authentication.getName().equals("anonymousUser") || true) { //Retirer le true pour le bon fonctionnement
|
||||
// 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("Jojo");
|
||||
// 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";
|
||||
}else {
|
||||
return "accueil";
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public String setUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// 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("Jojo");
|
||||
System.out.println(userProfile.getId());
|
||||
//Supprimer le compte
|
||||
userService.deleteUtilisateur(userProfile.getId());
|
||||
//ATTENTION AJOUTER LA DECONNEXION
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
|
||||
@Override
|
||||
public UserProfil findByUsername(String username) {
|
||||
String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username";
|
||||
String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username AND isDelete = 0";
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("username", username);
|
||||
UserProfil user = namedParameterJdbcTemplate.queryForObject(sql, params, (rs, rowNum) -> {
|
||||
@@ -69,7 +69,7 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
public void save(UserProfil utilisateur) {
|
||||
if (utilisateur.getId() == 0) {
|
||||
//Création utilisateur
|
||||
String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false)";
|
||||
String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur, isDelete) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false, false)";
|
||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||
parameters.addValue("pseudo", utilisateur.getPseudo());
|
||||
parameters.addValue("nom", utilisateur.getNom());
|
||||
@@ -87,11 +87,25 @@ public class UserRepositoryImpl implements UserRepository {
|
||||
}
|
||||
}else {
|
||||
//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();
|
||||
parameters.addValue("pseudo", utilisateur.getPseudo());
|
||||
parameters.addValue("nom", utilisateur.getNom());
|
||||
parameters.addValue("prenom", utilisateur.getPrenom());
|
||||
parameters.addValue("email", utilisateur.getEmail());
|
||||
parameters.addValue("telephone", utilisateur.getTelephone());
|
||||
parameters.addValue("rue", utilisateur.getRue());
|
||||
parameters.addValue("code_postal", utilisateur.getCode_postal());
|
||||
parameters.addValue("ville", utilisateur.getVille());
|
||||
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getPassword())); // Assurez-vous de hasher le nouveau mot de passe si nécessaire
|
||||
parameters.addValue("id", utilisateur.getId());
|
||||
namedParameterJdbcTemplate.update(sql, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(int id) {
|
||||
|
||||
String sql = "UPDATE UTILISATEURS SET isDelete = 1 WHERE no_utilisateur = ?";
|
||||
jdbcTemplate.update(sql, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,15 @@ public class WebSecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeHttpRequests((requests) -> requests.requestMatchers("/", "/accueil").permitAll()
|
||||
.requestMatchers("/accueil", "/login", "/inscription", "/searchArticle").permitAll()
|
||||
http.authorizeHttpRequests((requests) -> requests
|
||||
.requestMatchers("/", "/accueil").permitAll()
|
||||
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/profile/**").permitAll()
|
||||
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll()
|
||||
.requestMatchers("/profile").hasAnyRole("MEMBRE", "ADMIN")
|
||||
.requestMatchers("/admin").hasRole("ADMIN")
|
||||
.anyRequest().authenticated())
|
||||
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))
|
||||
.logout((logout) -> logout.clearAuthentication(true).invalidateHttpSession(true)
|
||||
.deleteCookies("JSESSIONID").logoutSuccessUrl("/filmLogout")
|
||||
.deleteCookies("JSESSIONID").logoutSuccessUrl("/logout")
|
||||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll());
|
||||
|
||||
return http.build();
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<div class="champ-saisie">
|
||||
<label for="pseudo">Pseudo: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{pseudo}" id="pseudo" />
|
||||
<input type="text" th:field="*{pseudo}" id="pseudo"/>
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('pseudo')}">
|
||||
<ul>
|
||||
@@ -106,7 +106,6 @@
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ Mot de passe-->
|
||||
<div class="champ-saisie">
|
||||
<label for="password">Mot de passe: </label>
|
||||
<div>
|
||||
@@ -119,14 +118,16 @@
|
||||
</span>
|
||||
</div>
|
||||
<div class="champ-saisie">
|
||||
<label for="password">Confirmation du mot de passe: </label>
|
||||
<label for="confirmPassword">Confirmation du mot de passe: </label>
|
||||
<div>
|
||||
<input type="password" id="password" />
|
||||
<input type="password" id="confirmPassword" />
|
||||
</div>
|
||||
</div>
|
||||
<input type="submit" value="Créer"/>
|
||||
<input type="submit" value="Créer" />
|
||||
</form>
|
||||
<form action="/" method="post">
|
||||
<button type="submit">Annuler</button>
|
||||
</form>
|
||||
<a href="/">Annuler</a>
|
||||
</div>
|
||||
<!-- Ajouter le footer -->
|
||||
</body>
|
||||
|
||||
@@ -5,23 +5,133 @@
|
||||
<title>Mon profile</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="container-main">
|
||||
<form th:action="@{/profile/updateUser}" method="post">
|
||||
<h2>Mon profile</h2>
|
||||
<div class="d-flex flew-column">
|
||||
<div><label>Pseudo : <input type="text" name="username"/></label></div>
|
||||
<div><label>Prénom : <input type="text" name="name"/></label></div>
|
||||
<div><label>Téléphone : <input type="text" name="phone"/></label></div>
|
||||
<div><label>Code postal : <input type="text" name="postalCode"/></label></div>
|
||||
<div id="container-main">
|
||||
<h1>Mon profil</h1>
|
||||
<form th:action="@{/inscription/newUser}" method="post" th:object="${userProfile}">
|
||||
<!--<div class="erreur-saisie" th:if="${#fields.hasErrors('*')}" >
|
||||
<p th:text="#{index.erreurs}">Message d'erreur</p>
|
||||
</div>-->
|
||||
<!-- Champ pseudo-->
|
||||
<div class="champ-saisie">
|
||||
<label for="pseudo">Pseudo: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{pseudo}" id="pseudo" />
|
||||
</div>
|
||||
|
||||
<div class="d-flex flew-column">
|
||||
<div><label>Nom : <input type="text" name="lastName"/></label></div>
|
||||
<div><label>Email : <input type="text" name="email"/></label></div>
|
||||
<div><label>Rue : <input type="text" name="address"/></label></div>
|
||||
<div><label>Ville : <input type="text" name="city"/></label></div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('pseudo')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('pseudo')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ prénom-->
|
||||
<div class="champ-saisie">
|
||||
<label for="prenom">Prénom: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{prenom}" id="prenom" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('prenom')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('prenom')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ nom-->
|
||||
<div class="champ-saisie">
|
||||
<label for="nom">Nom: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{nom}" id="nom" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('nom')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('nom')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ email-->
|
||||
<div class="champ-saisie">
|
||||
<label for="email">Email: </label>
|
||||
<div>
|
||||
<input type="email" th:field="*{email}" id="email" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('email')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('email')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ téléphone-->
|
||||
<div class="champ-saisie">
|
||||
<label for="telephone">Téléphone: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{telephone}" id="telephone" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('telephone')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('telephone')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ Rue-->
|
||||
<div class="champ-saisie">
|
||||
<label for="rue">Rue: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{rue}" id="rue" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('rue')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('rue')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ Code postal-->
|
||||
<div class="champ-saisie">
|
||||
<label for="code_postal">Code postal: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{code_postal}" id="code_postal" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('code_postal')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('code_postal')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ Ville-->
|
||||
<div class="champ-saisie">
|
||||
<label for="code_postal">Ville: </label>
|
||||
<div>
|
||||
<input type="text" th:field="*{ville}" id="ville" />
|
||||
</div>
|
||||
<span style="color: red;" th:if="${#fields.hasErrors('ville')}">
|
||||
<ul>
|
||||
<li th:each="erreur: ${#fields.errors('ville')}" th:text="${erreur}"></li>
|
||||
</ul>
|
||||
</span>
|
||||
</div>
|
||||
<!-- Champ Mot de passe-->
|
||||
<div class="champ-saisie">
|
||||
<label for="password">Nouveau mot de passe: </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">
|
||||
<label for="password">Confirmation: </label>
|
||||
<div>
|
||||
<input type="password" id="password" />
|
||||
</div>
|
||||
</div>
|
||||
<div>Crédits: <span th:text="${userProfile.credit}"></span></div>
|
||||
<input type="hidden" id="userId" name="userId" th:field="*{id}" th:value="${userProfile.id}" />
|
||||
<input type="submit" value="Enregistrer"/>
|
||||
</form>
|
||||
</div>
|
||||
<form th:action="@{/profile/delete}" method="post">
|
||||
<button type="submit">Supprimer mon compte</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user