LoginController.java
1.98 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
package etunicorn.controller;
import etunicorn.RestrictedTo;
import etunicorn.entity.Personne;
import etunicorn.entity.Session;
import etunicorn.generated.model.UpdateLoginRequest;
import etunicorn.repository.PersonneRepository;
import etunicorn.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
/**
* etunicorn-server
* Copyright © 2017 Le Club Info Polytech Lille
* Tous droits réservés
*/
@RestController
public class LoginController extends BaseController implements etunicorn.generated.LoginController {
@Autowired
HttpServletRequest request;
@Autowired
private PersonneRepository personneRepository;
@Autowired
private SessionService sessionService;
@Override
@RestrictedTo(authentifie = false)
public ResponseEntity<?> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
Personne personne = personneRepository.findByLogin(updateLoginRequest.getLogin());
if (personne == null) {
return generateError(HttpStatus.UNAUTHORIZED);
}
// TODO Vraie vérification du mot de passe
if (!updateLoginRequest.getPassword().equals("test")) {
return generateError(HttpStatus.UNAUTHORIZED);
}
Session session = sessionService.createSession(personne);
return new ResponseEntity<Object>(session, HttpStatus.OK);
}
@Override
@RestrictedTo(authentifie = false)
public ResponseEntity<?> deleteLogin() {
Session session = sessionService.getSession(request);
if (session != null) {
sessionService.deleteSession(session);
}
return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
}