e1305e8c
sfeutrie
projet Spring boo...
|
1
2
3
|
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
4
|
import org.springframework.http.HttpStatus;
|
e1305e8c
sfeutrie
projet Spring boo...
|
5
|
import org.springframework.stereotype.Controller;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
6
|
import org.springframework.web.bind.annotation.*;
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
7
|
import org.springframework.web.multipart.MultipartFile;
|
d0a03db7
sfeutrie
restructuration d...
|
8
|
import org.springframework.web.servlet.ModelAndView;
|
57e157a4
sfeutrie
optimisation de l...
|
9
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
10
11
|
import java.io.*;
import java.sql.Timestamp;
|
115e3f68
sfeutrie
amélioration du f...
|
12
13
|
import java.util.*;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
14
15
16
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
|
cb678ccc
Unknown
Changement pour l...
|
17
18
|
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
|
e1305e8c
sfeutrie
projet Spring boo...
|
19
|
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
20
|
import javax.servlet.annotation.MultipartConfig;
|
57e157a4
sfeutrie
optimisation de l...
|
21
|
|
d0a03db7
sfeutrie
restructuration d...
|
22
|
@Controller
|
2df8b522
Unknown
Choix du fichier ...
|
23
24
|
@MultipartConfig(fileSizeThreshold = 20971520) //à changer, taille max des fichiers
|
e1305e8c
sfeutrie
projet Spring boo...
|
25
|
public class MainController {
|
d0a03db7
sfeutrie
restructuration d...
|
26
27
|
@Autowired
|
a13d6d0f
Unknown
Fichier de config...
|
28
29
30
|
NodeRepository nodeRepository;
@Autowired
|
e1305e8c
sfeutrie
projet Spring boo...
|
31
32
|
CustomerRepository customerRepository;
|
d0a03db7
sfeutrie
restructuration d...
|
33
|
@Autowired
|
65f1240c
sfeutrie
Ajout des groupes...
|
34
35
36
|
TeamRepository teamRepository;
@Autowired
|
d0a03db7
sfeutrie
restructuration d...
|
37
38
|
RoleRepository roleRepository;
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
39
|
@Autowired
|
65f1240c
sfeutrie
Ajout des groupes...
|
40
|
UpdateRepository updateRepository;
|
d1d16f06
sfeutrie
implementation de...
|
41
42
|
@Autowired
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
43
44
45
|
BCryptPasswordEncoder bCryptPasswordEncoder;
@GetMapping(value="/")
|
f1c88101
sfeutrie
Ergonomie du site
|
46
|
public String uploadRedirection(){
|
cb678ccc
Unknown
Changement pour l...
|
47
|
return "redirect:home";
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
48
49
50
51
52
53
|
}
@GetMapping(value="/home")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
54
55
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
16791f79
sfeutrie
routage du site
|
56
|
modelAndView.addObject("customerRole", customer.getRole());
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
57
58
|
modelAndView.setViewName("home");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
59
60
|
}
|
f1c88101
sfeutrie
Ergonomie du site
|
61
62
63
64
65
66
67
68
69
70
71
|
@GetMapping(value="/upload")
public ModelAndView upload() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("upload");
return modelAndView;
}
|
65f1240c
sfeutrie
Ajout des groupes...
|
72
73
|
@GetMapping(value="/update")
public ModelAndView update() {
|
deabfae9
sfeutrie
ajout d'une class...
|
74
75
76
77
78
|
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
modelAndView.addObject("customerRole", customer.getRole());
|
0e18634e
sfeutrie
modifications min...
|
79
|
modelAndView.addObject("customerMaj", (teamRepository.findByCustomersContaining(customer)).getUpdates());
|
a13d6d0f
Unknown
Fichier de config...
|
80
81
|
List<Node> nodes = nodeRepository.findAll();
modelAndView.addObject("nodes", nodes);
|
2df8b522
Unknown
Choix du fichier ...
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
File file = new File("files");
File[] dirs = file.listFiles();
List<String> filesName = new ArrayList<String>();
if (dirs != null) {
for (int i = 0; i < dirs.length; i++) {
if (dirs[i].isDirectory() == true) {
if ((dirs[i].getName().split("_")[0]).equals(customer.getEmail().split("@")[0])) {
File dir = new File(dirs[i].getAbsolutePath());
File[] f = dir.listFiles();
filesName.add(f[0].getName());
}
}
}
}
modelAndView.addObject("customerFiles", filesName);
|
65f1240c
sfeutrie
Ajout des groupes...
|
100
|
modelAndView.setViewName("update");
|
deabfae9
sfeutrie
ajout d'une class...
|
101
102
103
|
return modelAndView;
}
|
d0a03db7
sfeutrie
restructuration d...
|
104
|
@GetMapping(path="/registration")
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
105
106
107
|
public ModelAndView registration() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
108
109
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
110
|
modelAndView.addObject("customerRole", customer.getRole());
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
111
112
|
modelAndView.setViewName("registration");
return modelAndView;
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
113
114
|
}
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
115
116
117
118
119
|
@GetMapping(path="/denied")
public String denied() {
return "denied";
}
|
d0a03db7
sfeutrie
restructuration d...
|
120
|
@PostMapping(path="/registration")
|
072e7fc2
Unknown
Passage de pseudo...
|
121
|
public ModelAndView addNewUser(@RequestParam String email, @RequestParam String password, @RequestParam String role) {
|
d0a03db7
sfeutrie
restructuration d...
|
122
123
|
//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
|
e1305e8c
sfeutrie
projet Spring boo...
|
124
|
Customer n = new Customer();
|
072e7fc2
Unknown
Passage de pseudo...
|
125
|
n.setEmail(email);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
126
|
n.setPassword(bCryptPasswordEncoder.encode(password));
|
0e18634e
sfeutrie
modifications min...
|
127
|
n.setCustomerId((int)(customerRepository.count() + 1));
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
128
|
n.setActive(1);
|
072e7fc2
Unknown
Passage de pseudo...
|
129
|
Customer temp = customerRepository.findByEmail(email);
|
16791f79
sfeutrie
routage du site
|
130
|
Role userRole = roleRepository.findByRole(role);
|
0e18634e
sfeutrie
modifications min...
|
131
|
n.addRole(userRole);
|
f1c88101
sfeutrie
Ergonomie du site
|
132
|
|
115e3f68
sfeutrie
amélioration du f...
|
133
134
|
//utilisé uniquement pour continuer à afficher l'utilisateur connecté//
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
135
136
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
137
138
|
modelAndView.addObject("customerRole", customer.getRole());
modelAndView.setViewName("registration");
|
072e7fc2
Unknown
Passage de pseudo...
|
139
|
|
f1c88101
sfeutrie
Ergonomie du site
|
140
141
142
|
List<Customer> 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);
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
143
|
if(temp != null) {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
144
145
|
modelAndView.addObject("message", "L'utilisateur existe déjà !");
modelAndView.addObject("fail", true);
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
146
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
147
|
else {
|
e23a6c00
Antoine Duquenoy
UI : modification...
|
148
149
|
modelAndView.addObject("message", "L'utilisateur a bien été ajouté !");
modelAndView.addObject("ok", true);
|
d0a03db7
sfeutrie
restructuration d...
|
150
|
customerRepository.save(n);
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
151
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
152
153
154
|
modelAndView.setViewName("registration");
return modelAndView;
}
|
e743b1b9
Antoine Duquenoy
Ajout d'un utilis...
|
155
|
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
156
157
|
@RequestMapping(value = "/file", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
|
3e6ff772
Unknown
Page home restruc...
|
158
|
public void submit(@RequestParam MultipartFile file) {
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
159
160
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
161
|
Customer customer = customerRepository.findByEmail(auth.getName());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
162
|
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
072e7fc2
Unknown
Passage de pseudo...
|
163
|
File dirs = new File("files/" + customer.getEmail().split("@")[0] + "_" + timestamp.getTime());
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
164
165
166
|
dirs.mkdirs();
OutputStream outputStream = null;
InputStream inputStream = null;
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
try {
inputStream = file.getInputStream();
File newFile = new File(dirs.getPath() + "/" + file.getOriginalFilename());
if (!newFile.exists()) {
newFile.createNewFile();
}
outputStream = new FileOutputStream(newFile);
int read = 0;
byte[] bytes = new byte[1024];
while((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
181
182
183
|
catch (IOException e) {
e.printStackTrace();
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
184
185
186
187
188
|
finally {
try {
outputStream.close();
}
catch(IOException e) {
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
189
190
|
}
}
|
66a8b43c
Antoine Duquenoy
Intégration du fr...
|
191
192
|
}
|
cb678ccc
Unknown
Changement pour l...
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
@RequestMapping(value = "/config", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void submitConfig(@RequestParam MultipartFile file) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
nodeRepository.deleteAll();
Yaml yaml = new Yaml(new Constructor(ConfigNodes.class));
ConfigNodes conf = yaml.load(inputStream);
Iterator<NodeYAML> iter = conf.getNodes().iterator();
while (iter.hasNext()) {
NodeYAML element = iter.next();
Node node = new Node();
node.setName(element.getName());
node.setArch(element.getArch());
node.setIp(element.getIp());
|
cb678ccc
Unknown
Changement pour l...
|
218
219
220
|
nodeRepository.save(node);
}
}
|
303fdc72
Antoine Duquenoy
Sauvegarde des fi...
|
221
|
|
d0a03db7
sfeutrie
restructuration d...
|
222
|
@GetMapping(path="/login")
|
6029d52b
Antoine Duquenoy
Spring Security o...
|
223
|
public ModelAndView login() {
|
d0a03db7
sfeutrie
restructuration d...
|
224
225
226
|
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
227
|
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
228
|
|
115e3f68
sfeutrie
amélioration du f...
|
229
230
231
232
233
234
|
@GetMapping(path="/all")
public ModelAndView getAllUsers() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("all");
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
072e7fc2
Unknown
Passage de pseudo...
|
235
236
|
Customer customer = customerRepository.findByEmail(auth.getName());
modelAndView.addObject("customerName", customer.getEmail().split("@")[0]);
|
115e3f68
sfeutrie
amélioration du f...
|
237
238
|
modelAndView.addObject("customerRole", customer.getRole());
|
115e3f68
sfeutrie
amélioration du f...
|
239
|
List<Customer> list = customerRepository.findAll(); // attention : la méthode findAll() de JpaRepository retourne une liste alors que celle de CrudRepository retourne un itérable
|
072e7fc2
Unknown
Passage de pseudo...
|
240
|
modelAndView.addObject("list", list);
|
115e3f68
sfeutrie
amélioration du f...
|
241
|
return modelAndView;
|
e1305e8c
sfeutrie
projet Spring boo...
|
242
|
}
|
d0a03db7
sfeutrie
restructuration d...
|
243
|
|
f6a02b6c
Unknown
Mise à jour front...
|
244
|
@PostMapping(path="/savemaj")
|
2df8b522
Unknown
Choix du fichier ...
|
245
|
public String saveMaj(@RequestParam String name, @RequestParam String date, @RequestParam String nodes, @RequestParam String file){
|
d1d16f06
sfeutrie
implementation de...
|
246
247
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
|
f1c88101
sfeutrie
Ergonomie du site
|
248
|
|
65f1240c
sfeutrie
Ajout des groupes...
|
249
250
251
252
253
254
255
|
Update update_c = new Update();
update_c.setUpdate(name);
update_c.setDate(date);
update_c.setNodes(nodes);
update_c.setFile(file);
updateRepository.save(update_c); // ajouter la mise a jour dans la table
|
f1c88101
sfeutrie
Ergonomie du site
|
256
|
|
65f1240c
sfeutrie
Ajout des groupes...
|
257
258
259
260
|
System.out.println("team name : " + (teamRepository.findByCustomersContaining(customer)).getTeam());
Team teamOfCustomer = teamRepository.findByCustomersContaining(customer);
teamOfCustomer.addUpdate(update_c);
teamRepository.save(teamOfCustomer); // permet de rendre effective la jointure entre customer et maj
|
f1c88101
sfeutrie
Ergonomie du site
|
261
|
|
65f1240c
sfeutrie
Ajout des groupes...
|
262
|
return "redirect:/update";
|
deabfae9
sfeutrie
ajout d'une class...
|
263
|
}
|
4ca22bac
Unknown
Bouton "Lancer la...
|
264
265
266
267
268
269
|
@PostMapping(path="/runmaj")
public String runMaj(@RequestParam String name, @RequestParam String date, @RequestParam String nodes, @RequestParam String file){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
|
65f1240c
sfeutrie
Ajout des groupes...
|
270
271
272
273
274
|
Update update_c = new Update();
update_c.setUpdate(name);
update_c.setDate(date);
update_c.setNodes(nodes);
update_c.setFile(file);
|
4ca22bac
Unknown
Bouton "Lancer la...
|
275
276
|
Map<String, Object> data = new HashMap<String, Object>();
|
65f1240c
sfeutrie
Ajout des groupes...
|
277
278
279
280
|
data.put("name", update_c.getUpdate());
data.put("date", update_c.getDate());
data.put("file", update_c.getFile());
data.put("nodes", update_c.getNodes().split(";"));
|
4ca22bac
Unknown
Bouton "Lancer la...
|
281
282
283
|
Yaml yaml = new Yaml();
FileWriter writer = null;
try {
|
65f1240c
sfeutrie
Ajout des groupes...
|
284
|
writer = new FileWriter("toflash/" + customer.getEmail().split("@")[0] + "_" + update_c.getUpdate() + ".yaml");
|
4ca22bac
Unknown
Bouton "Lancer la...
|
285
286
287
288
289
|
} catch (IOException e) {
e.printStackTrace();
}
yaml.dump(data, writer);
|
65f1240c
sfeutrie
Ajout des groupes...
|
290
|
return "redirect:/update";
|
4ca22bac
Unknown
Bouton "Lancer la...
|
291
292
293
294
295
296
297
|
}
@PostMapping(path="/startsavedmaj")
@ResponseStatus(value = HttpStatus.OK)
public void startSavedMaj(@RequestParam String majname){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByEmail(auth.getName());
|
65f1240c
sfeutrie
Ajout des groupes...
|
298
|
Update update = updateRepository.findByUpdate(majname);
|
4ca22bac
Unknown
Bouton "Lancer la...
|
299
|
Map<String, Object> data = new HashMap<String, Object>();
|
65f1240c
sfeutrie
Ajout des groupes...
|
300
301
302
303
|
data.put("name", update.getUpdate());
data.put("date", update.getDate());
data.put("file", update.getFile());
data.put("nodes", update.getNodes().split(";"));
|
4ca22bac
Unknown
Bouton "Lancer la...
|
304
305
306
|
Yaml yaml = new Yaml();
FileWriter writer = null;
try {
|
65f1240c
sfeutrie
Ajout des groupes...
|
307
|
writer = new FileWriter("toflash/" + customer.getEmail().split("@")[0] + "_" + update.getUpdate() + ".yaml");
|
4ca22bac
Unknown
Bouton "Lancer la...
|
308
309
310
311
312
|
} catch (IOException e) {
e.printStackTrace();
}
yaml.dump(data, writer);
}
|
e1305e8c
sfeutrie
projet Spring boo...
|
313
|
}
|