diff --git a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java index b4e43b3..54722dc 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/Customer.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/Customer.java @@ -1,7 +1,5 @@ package com.PFE.ServerManager; -import org.springframework.beans.factory.annotation.Autowired; - import javax.persistence.*; import java.util.Set; @@ -34,6 +32,9 @@ public class Customer{ public void setRoles(Set roles) { this.roles = roles; } public Set getRoles() { return roles; } + public String getRole(){ + return roles.iterator().next().getRole(); + } public void setId(Integer id) { this.customer_id = id; } diff --git a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java index e46a72b..a3599ad 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/MainController.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/MainController.java @@ -36,6 +36,7 @@ public class MainController { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Customer customer = customerRepository.findByPseudo(auth.getName()); modelAndView.addObject("customerName", customer.getPseudo()); + modelAndView.addObject("customerRole", customer.getRole()); modelAndView.setViewName("home"); return modelAndView; } @@ -51,7 +52,7 @@ public class MainController { } @PostMapping(path="/registration") - public ModelAndView addNewUser(@RequestParam String pseudo, @RequestParam String password) { + public ModelAndView addNewUser(@RequestParam String pseudo, @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(); @@ -60,12 +61,11 @@ public class MainController { n.setId((int)(customerRepository.count()+1)); n.setActive(1); Customer temp = customerRepository.findByPseudo(pseudo); - Role userRole = roleRepository.findByRole("ADMIN"); + Role userRole = roleRepository.findByRole(role); n.setRoles(new HashSet(Arrays.asList(userRole))); if(temp != null) { modelAndView.addObject("ok", "l'utilisateur existe déjà"); - //return "login?fail"; } else { modelAndView.addObject("ok", "l'utilisateur a bien été ajouté"); diff --git a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java index dff960a..bea11a8 100644 --- a/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java +++ b/PFE06/src/main/java/com/PFE/ServerManager/SecurityConfig.java @@ -34,15 +34,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() - .antMatchers("/home").hasAuthority("ADMIN") - .antMatchers("/registration").permitAll() + .antMatchers("/home").hasAnyAuthority("USER","ADMIN") + .antMatchers("/registration").hasAuthority("ADMIN") .antMatchers("/login").permitAll() .antMatchers("/denied").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login").failureUrl("/login?error=true") - .defaultSuccessUrl("/success") + .defaultSuccessUrl("/home") .usernameParameter("pseudo") .passwordParameter("password") .and() diff --git a/PFE06/src/main/resources/data.sql b/PFE06/src/main/resources/data.sql index b220a93..e23b382 100644 --- a/PFE06/src/main/resources/data.sql +++ b/PFE06/src/main/resources/data.sql @@ -1,6 +1,7 @@ /* ce fichier doit être placé dans les ressources afin d'être utilisé */ INSERT INTO "role" VALUES (1,'ADMIN'); -INSERT INTO "customer" VALUES (1,1,'$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2','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_role" VALUES (1,1); -INSERT INTO "customer" VALUES (2,1,'$2a$10$GflhaD2IYuErynuOlxS2W.Gp1kXksVdiSviYN/lDYCsuH.lVm6Ph2','root'); -INSERT INTO "customer_role" VALUES (2,1); \ No newline at end of file +INSERT INTO "customer" VALUES (2,1,'$2a$10$0Fnls/gTQS1zA6rj1ZlxfuyyKNpCBDA1tcCqQMroPDIj1fRyhgv/O','user'); /*pseudo : user // password : password // role : USER*/ +INSERT INTO "customer_role" VALUES (2,2); \ No newline at end of file diff --git a/PFE06/src/main/resources/templates/home.html b/PFE06/src/main/resources/templates/home.html index 4a2b035..15c4273 100644 --- a/PFE06/src/main/resources/templates/home.html +++ b/PFE06/src/main/resources/templates/home.html @@ -7,16 +7,15 @@ - Connexion - Enregistrer des utilisateurs - +
+
Enregistrer des utilisateurs
+
+
- -

est connecté(e) !

- +

est connecté(e) !

\ No newline at end of file diff --git a/PFE06/src/main/resources/templates/registration.html b/PFE06/src/main/resources/templates/registration.html index 2176330..8a5f144 100644 --- a/PFE06/src/main/resources/templates/registration.html +++ b/PFE06/src/main/resources/templates/registration.html @@ -16,10 +16,17 @@ +
+ + + + + +
- connexion + Accueil