From b47c3fdf6d61b46ee3a71847c036ae20fe28afc0 Mon Sep 17 00:00:00 2001 From: Hedi Date: Mon, 24 Jan 2022 23:06:27 +0100 Subject: [PATCH 1/7] feat(mail):ajout d'envoi de mail au format text, HTML, avec piece jointe --- pom.xml | 5 +- .../organizee/controller/MailController.java | 40 +++++++++++ src/main/java/fr/organizee/model/Mail.java | 41 +++++++++++ .../fr/organizee/service/SendMailService.java | 15 ++++ .../service/SendMailServiceImpl.java | 68 +++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/organizee/controller/MailController.java create mode 100644 src/main/java/fr/organizee/model/Mail.java create mode 100644 src/main/java/fr/organizee/service/SendMailService.java create mode 100644 src/main/java/fr/organizee/service/SendMailServiceImpl.java diff --git a/pom.xml b/pom.xml index dbaf653..0d03dbb 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-mail + mysql mysql-connector-java diff --git a/src/main/java/fr/organizee/controller/MailController.java b/src/main/java/fr/organizee/controller/MailController.java new file mode 100644 index 0000000..ad07b51 --- /dev/null +++ b/src/main/java/fr/organizee/controller/MailController.java @@ -0,0 +1,40 @@ +package fr.organizee.controller; + +import fr.organizee.model.Mail; +import fr.organizee.service.SendMailService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.mail.MessagingException; + +@RestController +@RequestMapping("/mail") +public class MailController { + SendMailService service; + + public MailController(SendMailService service) { + this.service = service; + } + + @PostMapping("/send") + public ResponseEntity sendMail(@RequestBody Mail mail) { + service.sendMail(mail); + return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK); + } + + @PostMapping("/html") + public ResponseEntity sendMailHTML(@RequestBody Mail mail) throws MessagingException { + service.sendMailHTML(mail); + return new ResponseEntity<>("HTML mail sent successfully", HttpStatus.OK); + } + + @PostMapping("/attachment") + public ResponseEntity sendAttachmentEmail(@RequestBody Mail mail) throws MessagingException { + service.sendMailWithAttachments(mail); + return new ResponseEntity<>("Attachment mail sent successfully", HttpStatus.OK); + } +} \ No newline at end of file diff --git a/src/main/java/fr/organizee/model/Mail.java b/src/main/java/fr/organizee/model/Mail.java new file mode 100644 index 0000000..cbd551b --- /dev/null +++ b/src/main/java/fr/organizee/model/Mail.java @@ -0,0 +1,41 @@ +package fr.organizee.model; + +public class Mail { + private String recipient; + private String subject; + private String message; + + public Mail() { + } + + public Mail(String recipient, String subject, String message) { + this.recipient = recipient; + this.subject = subject; + this.message = message; + } + + public String getRecipient() { + return recipient; + } + + public void setRecipient(String recipient) { + this.recipient = recipient; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} + diff --git a/src/main/java/fr/organizee/service/SendMailService.java b/src/main/java/fr/organizee/service/SendMailService.java new file mode 100644 index 0000000..9437b10 --- /dev/null +++ b/src/main/java/fr/organizee/service/SendMailService.java @@ -0,0 +1,15 @@ +package fr.organizee.service; + +import fr.organizee.model.Mail; + +import javax.mail.MessagingException; + +public interface SendMailService { + void sendMail(Mail mail); + + void sendMailHTML(Mail mail) throws MessagingException; + + void sendMailWithAttachments(Mail mail) throws MessagingException; +} + + diff --git a/src/main/java/fr/organizee/service/SendMailServiceImpl.java b/src/main/java/fr/organizee/service/SendMailServiceImpl.java new file mode 100644 index 0000000..c77f675 --- /dev/null +++ b/src/main/java/fr/organizee/service/SendMailServiceImpl.java @@ -0,0 +1,68 @@ +package fr.organizee.service; + + +import fr.organizee.model.Mail; +import org.springframework.core.io.ClassPathResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Service; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +@Service +public class SendMailServiceImpl implements SendMailService { + private final JavaMailSender javaMailSender; + + public SendMailServiceImpl(JavaMailSender javaMailSender) { + this.javaMailSender = javaMailSender; + } + + @Override + public void sendMail(Mail mail) { + + SimpleMailMessage msg = new SimpleMailMessage(); + msg.setTo(mail.getRecipient(), mail.getRecipient()); + + msg.setSubject(mail.getSubject()); + msg.setText(mail.getMessage()); + + javaMailSender.send(msg); + } + + @Override + public void sendMailHTML(Mail mail) throws MessagingException { + MimeMessage msg = javaMailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8"); + + helper.setTo(mail.getRecipient()); + + helper.setSubject(mail.getSubject()); + + String htmlMsg = "

Im testing send a HTML email

" + +""; + + msg.setContent(htmlMsg, "text/html"); + + javaMailSender.send(msg); + } + + @Override + public void sendMailWithAttachments(Mail mail) throws MessagingException { + MimeMessage msg = javaMailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(msg, true); + + helper.setTo("to_@email"); + + helper.setSubject("Testing from Spring Boot"); + + helper.setText("Find the attached image", true); + + helper.addAttachment("hero.jpg", new ClassPathResource("hero.jpg")); + + javaMailSender.send(msg); + } +} \ No newline at end of file From f23b0fbb87809fad823cc5df0e8d8e3adff9cd9f Mon Sep 17 00:00:00 2001 From: Hedi Date: Fri, 28 Jan 2022 12:08:49 +0100 Subject: [PATCH 2/7] modif route controller mail --- src/main/java/fr/organizee/controller/MailController.java | 4 ++-- src/main/java/fr/organizee/service/SendMailServiceImpl.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/organizee/controller/MailController.java b/src/main/java/fr/organizee/controller/MailController.java index ad07b51..c78984d 100644 --- a/src/main/java/fr/organizee/controller/MailController.java +++ b/src/main/java/fr/organizee/controller/MailController.java @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.mail.MessagingException; @RestController -@RequestMapping("/mail") +@RequestMapping("/sendmail") public class MailController { SendMailService service; @@ -20,7 +20,7 @@ public class MailController { this.service = service; } - @PostMapping("/send") + @PostMapping("/text") public ResponseEntity sendMail(@RequestBody Mail mail) { service.sendMail(mail); return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK); diff --git a/src/main/java/fr/organizee/service/SendMailServiceImpl.java b/src/main/java/fr/organizee/service/SendMailServiceImpl.java index c77f675..ebb8f65 100644 --- a/src/main/java/fr/organizee/service/SendMailServiceImpl.java +++ b/src/main/java/fr/organizee/service/SendMailServiceImpl.java @@ -41,7 +41,7 @@ public class SendMailServiceImpl implements SendMailService { helper.setSubject(mail.getSubject()); - String htmlMsg = "

Im testing send a HTML email

" + String htmlMsg = "

Test d'envoi de mail au format HTML

" +""; msg.setContent(htmlMsg, "text/html"); From aece88e2a81f299f84ee7585b1493be153559609 Mon Sep 17 00:00:00 2001 From: Hedi Date: Sun, 30 Jan 2022 19:15:01 +0100 Subject: [PATCH 3/7] =?UTF-8?q?fix=20des=20class=20pour=20eviter=20la=20re?= =?UTF-8?q?cursivit=C3=A9=20de=20certain=20en=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/organizee/controller/ContactController.java | 10 +++++----- src/main/java/fr/organizee/model/Contact.java | 3 ++- src/main/java/fr/organizee/model/Evenement.java | 7 ++++--- src/main/java/fr/organizee/model/Menu.java | 2 +- src/main/java/fr/organizee/model/Team.java | 8 ++++++++ src/main/java/fr/organizee/model/TodoList.java | 3 ++- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/fr/organizee/controller/ContactController.java b/src/main/java/fr/organizee/controller/ContactController.java index ef79042..e93e926 100644 --- a/src/main/java/fr/organizee/controller/ContactController.java +++ b/src/main/java/fr/organizee/controller/ContactController.java @@ -21,7 +21,7 @@ public class ContactController { private ContactRepository contactRepo; @GetMapping(value = "/{id}") - @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") public ResponseEntity findById(@PathVariable int id){ Optional contact = null; try @@ -35,7 +35,7 @@ public class ContactController { } @GetMapping(value = "team/{team_id}") - @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") public ResponseEntity findByTeamId(@PathVariable int team_id){ List contacts = null; try @@ -49,7 +49,7 @@ public class ContactController { } @PostMapping(value="/add") - @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") public ResponseEntity addContact(@RequestBody Contact contact){ Contact resultContact = null; try { @@ -62,7 +62,7 @@ public class ContactController { } @PutMapping("/update/{id}") - @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") + //@PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')") public ResponseEntity updateContact(@RequestBody Contact contact, @PathVariable Integer id) throws Exception { Contact resultContact = null; try { @@ -76,7 +76,7 @@ public class ContactController { } @DeleteMapping(value = "/delete/{id}") - @PreAuthorize("hasRole('ROLE_PARENT')") + //@PreAuthorize("hasRole('ROLE_PARENT')") public ResponseEntity deleteContact(@PathVariable int id){ try { contactRepo.delete(contactRepo.getById(id)); diff --git a/src/main/java/fr/organizee/model/Contact.java b/src/main/java/fr/organizee/model/Contact.java index 2c5ffb2..826b2db 100644 --- a/src/main/java/fr/organizee/model/Contact.java +++ b/src/main/java/fr/organizee/model/Contact.java @@ -1,5 +1,6 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; @@ -18,7 +19,7 @@ public class Contact { private LocalDate dateNaissance; @ManyToOne @JoinColumn(name="TEAM_ID") - @JsonIgnoreProperties("contact") + @JsonIgnoreProperties({"contact","membre"}) private Team team; public Contact() { diff --git a/src/main/java/fr/organizee/model/Evenement.java b/src/main/java/fr/organizee/model/Evenement.java index 15a355f..936cdaa 100644 --- a/src/main/java/fr/organizee/model/Evenement.java +++ b/src/main/java/fr/organizee/model/Evenement.java @@ -1,5 +1,6 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; @@ -14,13 +15,13 @@ public class Evenement { private LocalDateTime eventFin; private int allDay; private String libelle; - @ManyToOne(cascade = CascadeType.MERGE) + @ManyToOne @JoinColumn(name="MEMBRE_ID") @JsonIgnoreProperties("evenement") private Membre membre; - @ManyToOne(cascade = CascadeType.MERGE) + @ManyToOne @JoinColumn(name="TEAM_ID") - @JsonIgnoreProperties("evenement") + @JsonIgnoreProperties({"evenement", "membre"}) private Team team; public Evenement() { diff --git a/src/main/java/fr/organizee/model/Menu.java b/src/main/java/fr/organizee/model/Menu.java index e11cba4..dcc5cb0 100644 --- a/src/main/java/fr/organizee/model/Menu.java +++ b/src/main/java/fr/organizee/model/Menu.java @@ -15,7 +15,7 @@ public class Menu { private int validationProposition; @ManyToOne @JoinColumn(name="TEAM_ID") - @JsonIgnoreProperties("menu") + @JsonIgnoreProperties({"menu","membre"}) private Team team; public Menu() { diff --git a/src/main/java/fr/organizee/model/Team.java b/src/main/java/fr/organizee/model/Team.java index ded5837..14ff97e 100644 --- a/src/main/java/fr/organizee/model/Team.java +++ b/src/main/java/fr/organizee/model/Team.java @@ -1,5 +1,6 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; @@ -12,17 +13,24 @@ public class Team { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String nom; + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @JsonIgnoreProperties("team") private List membres = new ArrayList<>(); + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @JsonIgnoreProperties("team") + @JsonIgnore private List contacts = new ArrayList<>(); + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @JsonIgnoreProperties("team") + @JsonIgnore private List todolists = new ArrayList<>(); + @OneToMany(mappedBy = "team", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @JsonIgnoreProperties("team") + @JsonIgnore private List menus = new ArrayList<>(); public Team() { diff --git a/src/main/java/fr/organizee/model/TodoList.java b/src/main/java/fr/organizee/model/TodoList.java index 007e68a..f8ec979 100644 --- a/src/main/java/fr/organizee/model/TodoList.java +++ b/src/main/java/fr/organizee/model/TodoList.java @@ -1,5 +1,6 @@ package fr.organizee.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import javax.persistence.*; @@ -14,7 +15,7 @@ public class TodoList { private String nom; @ManyToOne @JoinColumn(name="TEAM_ID") - @JsonIgnoreProperties("todolist") + @JsonIgnoreProperties({"todolist","membre"}) private Team team; @OneToMany(mappedBy = "todolist", fetch=FetchType.LAZY, cascade = CascadeType.ALL) @JsonIgnoreProperties("todolist") From 382b1052d46418071d23f25f8272a68a4f4af265 Mon Sep 17 00:00:00 2001 From: Hedi MJID Date: Tue, 1 Feb 2022 09:17:11 +0100 Subject: [PATCH 4/7] commentaires --- src/main/java/fr/organizee/controller/MailController.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/fr/organizee/controller/MailController.java b/src/main/java/fr/organizee/controller/MailController.java index c78984d..3543668 100644 --- a/src/main/java/fr/organizee/controller/MailController.java +++ b/src/main/java/fr/organizee/controller/MailController.java @@ -20,18 +20,21 @@ public class MailController { this.service = service; } + // Envoi de mail en text brut @PostMapping("/text") public ResponseEntity sendMail(@RequestBody Mail mail) { service.sendMail(mail); return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK); } + // Envoi de mail au format HTML @PostMapping("/html") public ResponseEntity sendMailHTML(@RequestBody Mail mail) throws MessagingException { service.sendMailHTML(mail); return new ResponseEntity<>("HTML mail sent successfully", HttpStatus.OK); } + // Envoi du mail avec une piece jointe @PostMapping("/attachment") public ResponseEntity sendAttachmentEmail(@RequestBody Mail mail) throws MessagingException { service.sendMailWithAttachments(mail); From ce88e646fddb7c6e7f4d0874720083865b073aed Mon Sep 17 00:00:00 2001 From: Hedi MJID Date: Tue, 1 Feb 2022 16:06:16 +0100 Subject: [PATCH 5/7] fix destinataire mail --- src/main/java/fr/organizee/service/SendMailServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/fr/organizee/service/SendMailServiceImpl.java b/src/main/java/fr/organizee/service/SendMailServiceImpl.java index ebb8f65..c9974ca 100644 --- a/src/main/java/fr/organizee/service/SendMailServiceImpl.java +++ b/src/main/java/fr/organizee/service/SendMailServiceImpl.java @@ -55,7 +55,7 @@ public class SendMailServiceImpl implements SendMailService { MimeMessageHelper helper = new MimeMessageHelper(msg, true); - helper.setTo("to_@email"); + helper.setTo("destinataire@email"); helper.setSubject("Testing from Spring Boot"); From 7c352cad951c30a96cbb39937766896865ef5969 Mon Sep 17 00:00:00 2001 From: Hedi Date: Sat, 5 Feb 2022 15:36:35 +0100 Subject: [PATCH 6/7] calendrier : modif model + repository + controller --- .../controller/EvenementController.java | 55 ++++++++++++++++--- .../java/fr/organizee/model/Evenement.java | 44 +++++++-------- .../repository/EvenementRepository.java | 7 +++ src/main/resources/data.sql | 9 ++- 4 files changed, 84 insertions(+), 31 deletions(-) diff --git a/src/main/java/fr/organizee/controller/EvenementController.java b/src/main/java/fr/organizee/controller/EvenementController.java index a5c3673..f6a32a8 100644 --- a/src/main/java/fr/organizee/controller/EvenementController.java +++ b/src/main/java/fr/organizee/controller/EvenementController.java @@ -1,15 +1,14 @@ package fr.organizee.controller; import fr.organizee.model.Evenement; -import fr.organizee.model.Membre; -import fr.organizee.model.Team; +import fr.organizee.model.Menu; import fr.organizee.repository.EvenementRepository; -import fr.organizee.repository.TeamRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import javax.persistence.EntityNotFoundException; import java.util.List; import java.util.Optional; @@ -21,17 +20,59 @@ public class EvenementController { @Autowired private EvenementRepository evenementRepo; - // Recupérer tout les evenements pour une team {id} - @GetMapping(value = "/all/{id}") - public ResponseEntity getAll(){ + // Recupérer tout les evenements pour une team {team_id} + @GetMapping(value = "/team/{team_id}") + public ResponseEntity findByTeamId(@PathVariable int team_id){ List liste = null; try { - liste = evenementRepo.findAll(); + liste = evenementRepo.FindEvenementsByTeam(team_id); } catch (Exception e) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); } return ResponseEntity.status(HttpStatus.OK).body(liste); } + + // Ajoute un evenement au calendrier + @PostMapping(value="/add", produces="application/json", consumes="application/json") + public ResponseEntity addTache(@RequestBody Evenement event){ + Evenement resultEvent = null; + try { + resultEvent = evenementRepo.saveAndFlush(event); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.CREATED).body(resultEvent); + } + + //Mise a jour d'un evenement par son ID + @PutMapping("/update/{id}") + //@PreAuthorize("hasRole('ROLE_PARENT')") + public ResponseEntity updateEvenement(@RequestBody Evenement event, @PathVariable Integer id) throws Exception { + Evenement resultEvenement = null; + try { + resultEvenement = evenementRepo.save(event); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } + + return ResponseEntity.status(HttpStatus.OK).body(resultEvenement); + } + + //Efface un evenement par son ID + @DeleteMapping(value = "/delete/{id}") + //@PreAuthorize("hasRole('ROLE_PARENT')") + public ResponseEntity deleteEvenement(@PathVariable int id){ + try { + evenementRepo.delete(evenementRepo.getById(id)); + return ResponseEntity.status(HttpStatus.OK).body("Evenement effacé !"); + + } catch (EntityNotFoundException e) { + + return ResponseEntity.status(HttpStatus.OK).body("Evenement introuvable !"); + } + } } diff --git a/src/main/java/fr/organizee/model/Evenement.java b/src/main/java/fr/organizee/model/Evenement.java index 936cdaa..f2f83fd 100644 --- a/src/main/java/fr/organizee/model/Evenement.java +++ b/src/main/java/fr/organizee/model/Evenement.java @@ -11,10 +11,10 @@ public class Evenement { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; - private LocalDateTime eventDebut; - private LocalDateTime eventFin; + private LocalDateTime start; + private LocalDateTime end; private int allDay; - private String libelle; + private String text; @ManyToOne @JoinColumn(name="MEMBRE_ID") @JsonIgnoreProperties("evenement") @@ -27,12 +27,12 @@ public class Evenement { public Evenement() { } - public Evenement(int id, LocalDateTime eventDebut, LocalDateTime eventFin, int allDay, String libelle, Membre membre, Team team) { + public Evenement(int id, LocalDateTime start, LocalDateTime end, int allDay, String text, Membre membre, Team team) { this.id = id; - this.eventDebut = eventDebut; - this.eventFin = eventFin; + this.start = start; + this.end = end; this.allDay = allDay; - this.libelle = libelle; + this.text = text; this.membre = membre; this.team = team; } @@ -45,20 +45,20 @@ public class Evenement { this.id = id; } - public LocalDateTime getEventDebut() { - return eventDebut; + public LocalDateTime getStart() { + return start; } - public void setEventDebut(LocalDateTime eventDebut) { - this.eventDebut = eventDebut; + public void setStart(LocalDateTime start) { + this.start = start; } - public LocalDateTime getEventFin() { - return eventFin; + public LocalDateTime getEnd() { + return end; } - public void setEventFin(LocalDateTime eventFin) { - this.eventFin = eventFin; + public void setEnd(LocalDateTime end) { + this.end = end; } public int getAllDay() { @@ -69,12 +69,12 @@ public class Evenement { this.allDay = allDay; } - public String getLibelle() { - return libelle; + public String getText() { + return text; } - public void setLibelle(String libelle) { - this.libelle = libelle; + public void setText(String text) { + this.text = text; } public Membre getMembre() { @@ -97,10 +97,10 @@ public class Evenement { public String toString() { return "Evenement{" + "id=" + id + - ", eventDebut=" + eventDebut + - ", eventFin=" + eventFin + + ", start=" + start + + ", end=" + end + ", allDay=" + allDay + - ", libelle='" + libelle + '\'' + + ", text='" + text + '\'' + ", membre=" + membre + ", team=" + team + '}'; diff --git a/src/main/java/fr/organizee/repository/EvenementRepository.java b/src/main/java/fr/organizee/repository/EvenementRepository.java index 51164c5..e8663ee 100644 --- a/src/main/java/fr/organizee/repository/EvenementRepository.java +++ b/src/main/java/fr/organizee/repository/EvenementRepository.java @@ -2,8 +2,15 @@ package fr.organizee.repository; import fr.organizee.model.Evenement; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + + @Repository public interface EvenementRepository extends JpaRepository { + @Query(value = "select * from evenement where team_id = :team_id", nativeQuery = true) + List FindEvenementsByTeam(@Param("team_id") int team_id); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 0af0130..4bc52c5 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -37,5 +37,10 @@ INSERT INTO `tache` (`id`, `etat`, `texte`, `todolist_id`) VALUES (4, 0, 'Acheter un sapin', 3), (5, 0, 'Trouver un repas', 3); -INSERT INTO `evenement` (`id`, `all_day`, `event_debut`, `event_fin`, `libelle`, `membre_id`, `team_id`) VALUES - (1, 0, '2022-01-13 09:00:33', '2022-01-13 13:04:38', 'Simplon', 1, 1); \ No newline at end of file +INSERT INTO `evenement` (`id`, `all_day`, `start`, `end`, `text`, `membre_id`, `team_id`) VALUES + (1, 0, '2022-02-04 09:00:00', '2022-02-04 13:00:00', 'Simplon', 1, 1), + (2, 0, '2022-02-03 12:00:00', '2022-02-03 13:00:00', 'Footing', 2, 1); + +INSERT INTO `menu` (`id`, `date_menu`, `libelle`, `validation_proposition`, `team_id`) VALUES + (1, '2022-01-13', 'Lasagnes', 1, 1), + (2, '2022-01-03', 'Kebab', 1, 1); \ No newline at end of file From e5bd6709981c3f707baf4c1a9f7e785ff7cb7ce0 Mon Sep 17 00:00:00 2001 From: Hedi Date: Sun, 6 Feb 2022 22:50:59 +0100 Subject: [PATCH 7/7] jwt : ajout du userId et du teamId dans le token --- src/main/java/fr/organizee/security/JwtTokenProvider.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/fr/organizee/security/JwtTokenProvider.java b/src/main/java/fr/organizee/security/JwtTokenProvider.java index 9ec508d..502f9a7 100644 --- a/src/main/java/fr/organizee/security/JwtTokenProvider.java +++ b/src/main/java/fr/organizee/security/JwtTokenProvider.java @@ -27,6 +27,8 @@ import io.jsonwebtoken.JwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import fr.organizee.repository.MembreRepository; + /** * JWT : classe utilitaire chargée de fournir le Jeton (Token) et les vérifications */ @@ -44,6 +46,8 @@ public class JwtTokenProvider { @Autowired private UserDetailsService userDetailsService; + @Autowired + private MembreRepository membreRepo; /** * Cette méthode d'initialisation s'exécute avant le constructeur * Elle encode notre code secret en base64 pour la transmission dans le header @@ -104,6 +108,10 @@ public class JwtTokenProvider { public String createToken(String email, List roles){ Claims claims = Jwts.claims().setSubject(email); + claims.put("userId", membreRepo.findByEmail(email).get().getId()); + if(membreRepo.findByEmail(email).get().getTeam() != null){ + claims.put("teamId", membreRepo.findByEmail(email).get().getTeam().getId()); + } claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList())); System.out.println("claims = "+claims);