From 072e7fc2bad8cab7d490ca189da9f38cf86b4b2b Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 29 Nov 2018 16:02:24 +0100 Subject: [PATCH] Passage de pseudo à email pour les utilisateurs --- PFE06/src/main/java/com/PFE/ServerManager/Customer.java | 10 +++++----- PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java | 3 +-- PFE06/src/main/java/com/PFE/ServerManager/MainController.java | 33 ++++++++++++++++----------------- PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java | 7 +++---- PFE06/src/main/resources/data.sql | 4 ++-- PFE06/src/main/resources/templates/login.html | 4 ++-- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java index 54722dc..a04b014 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java @@ -16,8 +16,8 @@ public class Customer{ @Basic(optional = false) private Integer customer_id; - @Column(name = "pseudo") - private String pseudo; + @Column(name = "email") + private String email; @Column(name = "password") private String password; @@ -40,10 +40,10 @@ public class Customer{ } public Integer getCustomer_id() { return customer_id; } - public void setPseudo(String pseudo) { - this.pseudo = pseudo; + public void setEmail(String email) { + this.email = email; } - public String getPseudo() { return pseudo; } + public String getEmail() { return email; } public void setPassword(String password) { this.password = password; diff --git a/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java b/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java index be0c204..5ec049e 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/CustomerRepository.java @@ -1,7 +1,6 @@ package com.PFE.ServerManager; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository @@ -12,6 +11,6 @@ import org.springframework.stereotype.Repository; @Repository public interface CustomerRepository extends JpaRepository { - Customer findByPseudo(String pseudo); + Customer findByEmail(String email); } diff --git a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java index a69e5ec..cb8401b 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java @@ -39,8 +39,8 @@ public class MainController { public ModelAndView home() { ModelAndView modelAndView = new ModelAndView(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByPseudo(auth.getName()); - modelAndView.addObject("customerName", customer.getPseudo()); + Customer customer = customerRepository.findByEmail(auth.getName()); + modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); modelAndView.addObject("customerRole", customer.getRole()); modelAndView.setViewName("home"); return modelAndView; @@ -50,8 +50,8 @@ public class MainController { public ModelAndView registration() { ModelAndView modelAndView = new ModelAndView(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByPseudo(auth.getName()); - modelAndView.addObject("customerName", customer.getPseudo()); + Customer customer = customerRepository.findByEmail(auth.getName()); + modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); modelAndView.addObject("customerRole", customer.getRole()); modelAndView.setViewName("registration"); return modelAndView; @@ -63,24 +63,24 @@ public class MainController { } @PostMapping(path="/registration") - public ModelAndView addNewUser(@RequestParam String pseudo, @RequestParam String password, @RequestParam String role) { + public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) { //Model map, ModelAndView ou l'utilisation direct comme dans la méthode précédente sont 3 méthodes qui permettent d'envoyer des informations et donc de changer l'apparence d'une page ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView Customer n = new Customer(); - n.setPseudo(pseudo); + n.setEmail(email); n.setPassword(bCryptPasswordEncoder.encode(password)); - n.setId((int)(customerRepository.count()+1)); + n.setId((int)(customerRepository.count() + 1)); n.setActive(1); - Customer temp = customerRepository.findByPseudo(pseudo); + Customer temp = customerRepository.findByEmail(email); Role userRole = roleRepository.findByRole(role); n.setRoles(new HashSet(Arrays.asList(userRole))); //utilisé uniquement pour continuer à afficher l'utilisateur connecté// Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByPseudo(auth.getName()); - modelAndView.addObject("customerName", customer.getPseudo()); + Customer customer = customerRepository.findByEmail(auth.getName()); + modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); modelAndView.addObject("customerRole", customer.getRole()); modelAndView.setViewName("registration"); - /////// + if(temp != null) { modelAndView.addObject("message", "L'utilisateur existe déjà !"); modelAndView.addObject("fail", true); @@ -102,9 +102,9 @@ public class MainController { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByPseudo(auth.getName()); + Customer customer = customerRepository.findByEmail(auth.getName()); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - File dirs = new File("files/" + customer.getPseudo() + "_" + timestamp.getTime()); + File dirs = new File("files/" + customer.getEmail().split("@")[0] + "_" + timestamp.getTime()); dirs.mkdirs(); OutputStream outputStream = null; InputStream inputStream = null; @@ -136,7 +136,6 @@ public class MainController { } } - } @@ -157,8 +156,8 @@ public class MainController { modelAndView.setViewName("all"); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - Customer customer = customerRepository.findByPseudo(auth.getName()); - modelAndView.addObject("customerName", customer.getPseudo()); + Customer customer = customerRepository.findByEmail(auth.getName()); + modelAndView.addObject("customerName", customer.getEmail().split("@")[0]); modelAndView.addObject("customerRole", customer.getRole()); /*List list = new ArrayList(); @@ -167,7 +166,7 @@ public class MainController { list.add(listIterator.next()); }*/ List list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable - modelAndView.addObject("list",list); + modelAndView.addObject("list", list); return modelAndView; } diff --git a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java index dd3ad91..757d04c 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java @@ -8,7 +8,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; - import javax.sql.DataSource; @Configuration @@ -26,8 +25,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { auth.jdbcAuthentication() .dataSource(dataSource) .passwordEncoder(passwordEncoder) - .usersByUsernameQuery("select pseudo, password , active from customer where pseudo=?") - .authoritiesByUsernameQuery("select c.pseudo, r.role from customer c inner join customer_role cr on(c.customer_id=cr.customer_id) inner join role r on(cr.role_id=r.role_id) where c.pseudo=?"); + .usersByUsernameQuery("select email, password , active from customer where email=?") + .authoritiesByUsernameQuery("select c.email, r.role from customer c inner join customer_role cr on(c.customer_id=cr.customer_id) inner join role r on(cr.role_id=r.role_id) where c.email=?"); } @Override @@ -45,7 +44,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { .formLogin() .loginPage("/login").failureUrl("/login?error=true") .defaultSuccessUrl("/home") - .usernameParameter("pseudo") + .usernameParameter("email") .passwordParameter("password") .and() .logout() diff --git a/PFE06/src/main/resources/data.sql b/PFE06/src/main/resources/data.sql index 8afda4a..4cd4081 100644 --- a/PFE06/src/main/resources/data.sql +++ b/PFE06/src/main/resources/data.sql @@ -1,7 +1,7 @@ /* ce fichier doit être placé dans les ressources afin d'être utilisé */ INSERT INTO "role" VALUES (1,'ADMIN'); INSERT INTO "role" VALUES (2,'USER'); -INSERT INTO "customer" VALUES (1,1,'$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2','admin'); /*pseudo : admin // password : admin // role : ADMIN*/ +INSERT INTO "customer" VALUES (1,1,'admin@admin.fr','$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2'); /*pseudo : admin // password : admin // role : ADMIN*/ INSERT INTO "customer_role" VALUES (1,1); -INSERT INTO "customer" VALUES (2,1,'$2a$10$0Fnls/gTQS1zA6rj1ZlxfuyyKNpCBDA1tcCqQMroPDIj1fRyhgv/O','user'); /*pseudo : user // password : user // role : USER*/ +INSERT INTO "customer" VALUES (2,1,'user@user.fr','$2a$10$0Fnls/gTQS1zA6rj1ZlxfuyyKNpCBDA1tcCqQMroPDIj1fRyhgv/O'); /*pseudo : user // password : user // role : USER*/ INSERT INTO "customer_role" VALUES (2,2); \ No newline at end of file diff --git a/PFE06/src/main/resources/templates/login.html b/PFE06/src/main/resources/templates/login.html index de6db82..2a8b489 100644 --- a/PFE06/src/main/resources/templates/login.html +++ b/PFE06/src/main/resources/templates/login.html @@ -29,11 +29,11 @@
- +
- +
-- libgit2 0.21.2