AccountController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package fr.triplea.demovote.web.controller;
  2. import java.util.Locale;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.MessageSource;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.crypto.password.PasswordEncoder;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.PutMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import org.springframework.web.servlet.LocaleResolver;
  16. import fr.triplea.demovote.dao.ParticipantRepository;
  17. import fr.triplea.demovote.dto.MessagesTransfer;
  18. import fr.triplea.demovote.dto.ParticipantRecord;
  19. import fr.triplea.demovote.dto.ParticipantTransfer;
  20. import fr.triplea.demovote.model.Participant;
  21. import jakarta.servlet.http.HttpServletRequest;
  22. @RestController
  23. @RequestMapping("/account")
  24. public class AccountController
  25. {
  26. @SuppressWarnings("unused")
  27. private static final Logger LOG = LoggerFactory.getLogger(AccountController.class);
  28. @Autowired
  29. private ParticipantRepository participantRepository;
  30. @Autowired
  31. private PasswordEncoder passwordEncoder;
  32. @Autowired
  33. private LocaleResolver localeResolver;
  34. @Autowired
  35. private MessageSource messageSource;
  36. @GetMapping(value = "/form")
  37. public ResponseEntity<ParticipantRecord> getForm(final Authentication authentication)
  38. {
  39. if (authentication != null)
  40. {
  41. ParticipantRecord found = participantRepository.searchByPseudonyme(authentication.getName());
  42. if (found != null) { return ResponseEntity.ok(found); }
  43. }
  44. return ResponseEntity.notFound().build();
  45. }
  46. @PutMapping(value = "/update")
  47. public ResponseEntity<Object> update(@RequestBody(required = true) ParticipantTransfer participant, final Authentication authentication, HttpServletRequest request)
  48. {
  49. Locale locale = localeResolver.resolveLocale(request);
  50. if (authentication != null)
  51. {
  52. Participant found = participantRepository.findByPseudonyme(authentication.getName());
  53. if (found != null)
  54. {
  55. found.setEnabled(true);
  56. found.setNom(participant.getNom());
  57. found.setPrenom(participant.getPrenom());
  58. final String mdp = participant.getMotDePasse();
  59. if (mdp != null) { if (!(mdp.isBlank())) { found.setMotDePasse(passwordEncoder.encode(mdp.trim())); } }
  60. found.setGroupe(participant.getGroupe());
  61. found.setAdresse(participant.getAdresse());
  62. found.setCodePostal(participant.getCodePostal());
  63. found.setVille(participant.getVille());
  64. found.setPays(participant.getPays());
  65. found.setNumeroTelephone(participant.getNumeroTelephone());
  66. found.setEmail(participant.getEmail());
  67. found.setCommentaire(participant.getCommentaire());
  68. // TODO: modify password in session
  69. participantRepository.save(found);
  70. MessagesTransfer mt = new MessagesTransfer();
  71. mt.setInformation(messageSource.getMessage("participant.updated", null, locale));
  72. return ResponseEntity.ok(mt);
  73. }
  74. }
  75. return ResponseEntity.notFound().build();
  76. }
  77. }