Commit f8ff1584c823163d9c009e1a0f7565405ed19ae8

Authored by sfeutrie
1 parent 14f89f89

ajout de l'interface pour ajouter des groupes

PFE06/src/main/java/com/PFE/ServerManager/MainController.java
1 1 package com.PFE.ServerManager;
2 2  
3 3 import org.springframework.beans.factory.annotation.Autowired;
  4 +import org.springframework.data.repository.query.Param;
4 5 import org.springframework.http.HttpStatus;
5 6 import org.springframework.stereotype.Controller;
6 7 import org.springframework.web.bind.annotation.*;
... ... @@ -102,12 +103,16 @@ public class MainController {
102 103 }
103 104  
104 105 @GetMapping(path="/registration")
105   - public ModelAndView registration() {
  106 + public ModelAndView registration(@RequestParam(required=false) String message, @RequestParam(required=false) Integer succeed) {
106 107 ModelAndView modelAndView = new ModelAndView();
107 108 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
108 109 Customer customer = customerRepository.findByEmail(auth.getName());
109 110 modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
110 111 modelAndView.addObject("customerRole", customer.getRole());
  112 + modelAndView.addObject("message", message);
  113 + modelAndView.addObject("succeed", succeed);
  114 + System.out.println("all teams : " + teamRepository.findAll());
  115 + modelAndView.addObject("allTeams", teamRepository.findAll());
111 116 modelAndView.setViewName("registration");
112 117 return modelAndView;
113 118 }
... ... @@ -117,40 +122,43 @@ public class MainController {
117 122 return "denied";
118 123 }
119 124  
120   - @PostMapping(path="/registration")
121   - public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) {
122   - //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
123   - ModelAndView modelAndView = new ModelAndView(); // il n'est peut être pas utile d'utiliser ModelAndView
124   - Customer n = new Customer();
125   - n.setEmail(email);
126   - n.setPassword(bCryptPasswordEncoder.encode(password));
127   - n.setCustomerId((int)(customerRepository.count() + 1));
128   - n.setActive(1);
129   - Customer temp = customerRepository.findByEmail(email);
130   - Role userRole = roleRepository.findByRole(role);
131   - n.addRole(userRole);
132   -
133   - //utilisé uniquement pour continuer à afficher l'utilisateur connecté//
134   - Authentication auth = SecurityContextHolder.getContext().getAuthentication();
135   - Customer customer = customerRepository.findByEmail(auth.getName());
136   - modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
137   - modelAndView.addObject("customerRole", customer.getRole());
138   - modelAndView.setViewName("registration");
  125 + @PostMapping(path="/addTeam")
  126 + public String addNewTeam(@RequestParam String teamName){
139 127  
140   - List<Customer> list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable
141   - modelAndView.addObject("list", list);
  128 + if(teamRepository.findByTeam(teamName) != null) {
  129 + return "redirect:/registration?message=Le groupe&succeed=-1";
  130 + }
  131 + else {
  132 + Team t = new Team();
  133 + t.setTeam(teamName);
  134 + t.setTeamId((int)(teamRepository.count()+1));
  135 + teamRepository.save(t);
  136 + return "redirect:/registration?message=Le groupe&succeed=1";
  137 + }
  138 + }
  139 +
  140 + @PostMapping(path="/addUser")
  141 + public String addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role, @RequestParam String team) {
142 142  
143   - if(temp != null) {
144   - modelAndView.addObject("message", "L'utilisateur existe déjà !");
145   - modelAndView.addObject("fail", true);
  143 + if(customerRepository.findByEmail(email) != null) {
  144 + return "redirect:/registration?message=L'utilisateur&succeed=-1";
146 145 }
147 146 else {
148   - modelAndView.addObject("message", "L'utilisateur a bien été ajouté !");
149   - modelAndView.addObject("ok", true);
  147 + Customer n = new Customer();
  148 + n.setEmail(email);
  149 + n.setPassword(bCryptPasswordEncoder.encode(password));
  150 + n.setCustomerId((int)(customerRepository.count() + 1));
  151 + n.setActive(1);
  152 + Role userRole = roleRepository.findByRole(role);
  153 + n.addRole(userRole);
150 154 customerRepository.save(n);
  155 + System.out.println("team found : " + team);
  156 + Team temp = teamRepository.findByTeam(team);
  157 + temp.addCustomer(n);
  158 + teamRepository.save(temp);
  159 + return "redirect:/registration?message=L'utilisateur&succeed=1";
151 160 }
152   - modelAndView.setViewName("registration");
153   - return modelAndView;
  161 +
154 162 }
155 163  
156 164 @RequestMapping(value = "/file", method = RequestMethod.POST)
... ...
PFE06/src/main/java/com/PFE/ServerManager/TeamRepository.java
... ... @@ -9,6 +9,8 @@ import java.util.List;
9 9 @Repository
10 10 public interface TeamRepository extends JpaRepository<Team, Integer> {
11 11  
  12 + Team findByTeam(String team);
  13 +
12 14 Team findByCustomersContaining(Customer customer);
13 15  
14 16 @Override
... ...
PFE06/src/main/resources/templates/login.html
... ... @@ -29,11 +29,11 @@
29 29 <form id="Login" th:action="@{/login}" method="POST">
30 30  
31 31 <div class="form-team">
32   - <input type="email" class="form-control" id="inputEmail" placeholder="Email" name="email">
  32 + <input type="email" class="form-control" id="inputEmail" placeholder="Email" name="email" minlength="6" required>
33 33 </div>
34 34 <br/>
35 35 <div class="form-team">
36   - <input type="password" class="form-control" id="inputPassword" placeholder="Mot de passe" name="password">
  36 + <input type="password" class="form-control" id="inputPassword" placeholder="Mot de passe" name="password" minlength="6" required>
37 37 </div>
38 38 <br/>
39 39 <button @click.prevent="login" type="submit" class="btn btn-primary">Se connecter</button>
... ...
PFE06/src/main/resources/templates/registration.html
... ... @@ -38,32 +38,19 @@
38 38 </div>
39 39 </nav>
40 40  
  41 + <div class="container">
  42 + <h1 style="margin-bottom:50px; margin-top:50px; border-bottom:1px solid #CCC; padding-bottom:20px;">Formulaire d'ajout d'un utilisateur</h1>
  43 + <p>Merci d'entrer le login et le mot de passe du nouvel utilisateur</p>
  44 + </div>
41 45 <div class="login-form">
42 46 <div class="main-div">
43   - <div class="panel">
44   - <h2>Formulaire d'ajout</h2>
45   - <p>Merci d'entrer le login et le mot de passe du nouvel utilisateur</p>
46   - </div>
47   -
48   - <div th:if="${ok}">
49   - <div class="alert alert-success" role="alert">
50   - <span th:utext="${message}"></span>
51   - </div>
52   - </div>
53   -
54   - <div th:if="${fail}">
55   - <div class="alert alert-danger" role="alert">
56   - <span th:utext="${message}"></span>
57   - </div>
58   - </div>
59   -
60   - <form id="Login" th:action="@{/registration}" method="POST">
  47 + <form id="Login" th:action="@{/addUser}" method="POST">
61 48 <div class="form-team">
62   - <input type="email" class="form-control" id="username" placeholder="Entrer l'email" name="email">
  49 + <input type="email" class="form-control" id="username" placeholder="Entrer l'email" name="email" minlength="6" required>
63 50 </div>
64 51 <br/>
65 52 <div class="form-team">
66   - <input type="password" class="form-control" id="password" placeholder="Entrer le mot de passe" name="password">
  53 + <input type="password" class="form-control" id="password" placeholder="Entrer le mot de passe (6 caractères min.)" name="password" minlength="6" required>
67 54 </div>
68 55 <br/>
69 56 <div class="form3">
... ... @@ -74,10 +61,45 @@
74 61 <label for="role2">User</label>
75 62 </div>
76 63 <br/>
  64 + <div class="form-group">
  65 + <label for="sel2">Selectionnez un groupe de travail</label>
  66 + <select class="form-control" id="sel2" name="team" required>
  67 + <option name="team" th:each="team : ${allTeams}" th:value="${team.getTeam()}" th:utext="${team.getTeam()}"/>
  68 + </select>
  69 + </div>
77 70 <button @click.prevent="registration" type="submit" class="btn btn-primary">Ajouter</button>
78 71 </form>
79 72 </div>
80 73 </div>
  74 + <div style="max-width:38%; margin:50px auto; padding:10px 70px 10px 71px">
  75 + <div th:if="${succeed == 1}">
  76 + <div class="alert alert-success" role="alert">
  77 + <span th:utext="${message + ' a été ajouté'}"></span>
  78 + </div>
  79 + </div>
  80 +
  81 + <div th:if="${succeed == -1}">
  82 + <div class="alert alert-danger" role="alert">
  83 + <span th:utext="${message + ' existe déjà'}"></span>
  84 + </div>
  85 + </div>
  86 + </div>
  87 + <div class="container">
  88 + <h1 style="margin-bottom:50px; margin-top:50px; border-bottom:1px solid #CCC; padding-bottom:20px;">Formulaire d'ajout d'un groupe de travail</h1>
  89 + <p>Merci d'entrer le nom du nouveau groupe</p>
  90 + </div>
  91 + <div class="login-form">
  92 + <div class="main-div">
  93 + <form id="addTeam" th:action="@{/addTeam}" method="POST">
  94 + <div class="form-team">
  95 + <input type="text" class="form-control" id="teamName" placeholder="Entrer le nom du groupe" name="teamName" required>
  96 + </div>
  97 + <br/>
  98 + <button @click.prevent="registration" type="submit" class="btn btn-primary">Ajouter</button>
  99 + </form>
  100 + </div>
  101 + </div>
  102 + </div>
81 103  
82 104 <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
83 105 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
... ...