MainController.java
5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.PFE.ServerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.io.*;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashSet;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.web.context.request.RequestContextHolder;
import javax.servlet.annotation.MultipartConfig;
@Controller
@MultipartConfig(fileSizeThreshold = 20971520)
public class MainController {
@Autowired
CustomerRepository customerRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@GetMapping(value="/")
public String homeRedirection(){
return "redirect:home";
}
@GetMapping(value="/home")
public ModelAndView home() {
ModelAndView modelAndView = new ModelAndView();
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;
}
@GetMapping(path="/registration")
public ModelAndView registration() {
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByPseudo(auth.getName());
modelAndView.addObject("customerName", customer.getPseudo());
modelAndView.setViewName("registration");
return modelAndView;
}
@GetMapping(path="/denied")
public String denied() {
return "denied";
}
@PostMapping(path="/registration")
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();
n.setPseudo(pseudo);
n.setPassword(bCryptPasswordEncoder.encode(password));
n.setId((int)(customerRepository.count()+1));
n.setActive(1);
Customer temp = customerRepository.findByPseudo(pseudo);
Role userRole = roleRepository.findByRole(role);
n.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
if(temp != null) {
modelAndView.addObject("ok", "l'utilisateur existe déjà");
}
else {
modelAndView.addObject("ok", "l'utilisateur a bien été ajouté");
customerRepository.save(n);
}
modelAndView.setViewName("registration");
return modelAndView;
}
@RequestMapping(value = "/file", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void submit(
@RequestParam MultipartFile file, @RequestParam String nodes,
@RequestParam String filename) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Customer customer = customerRepository.findByPseudo(auth.getName());
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
File dirs = new File("files/" + customer.getPseudo() + "_" + timestamp.getTime());
dirs.mkdirs();
OutputStream outputStream = null;
InputStream inputStream = null;
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);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
outputStream.close();
}
catch(IOException e) {
}
}
}
@GetMapping(path="/login")
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
@GetMapping(path="/all")
public @ResponseBody Iterable<Customer> getAllUsers() {
return customerRepository.findAll();
}
@GetMapping(value="/success")
public String success(){
return "success";
}
}