diff --git a/.file.swp b/.file.swp
new file mode 100644
index 0000000..97eb5de
Binary files /dev/null and b/.file.swp differ
diff --git a/.idea/Organizee.iml b/.idea/Organizee.iml
deleted file mode 100644
index d6ebd48..0000000
--- a/.idea/Organizee.iml
+++ /dev/null
@@ -1,9 +0,0 @@
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index 6f8f4ad..b197f48 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -6,12 +6,17 @@
+<<<<<<< HEAD
+=======
+
+>>>>>>> b147bc406c22caa9c817c7189a325058d8550a69
diff --git a/pom.xml b/pom.xml
index 32eb628..dbaf653 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,15 @@
org.springframework.boot
spring-boot-starter-jdbc
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+ io.jsonwebtoken
+ jjwt
+ 0.9.1
+
org.springframework.boot
spring-boot-starter-web
diff --git a/src/main/java/fr/organizee/OrganizeeApplication.java b/src/main/java/fr/organizee/OrganizeeApplication.java
index e1323b4..6e2aaa4 100644
--- a/src/main/java/fr/organizee/OrganizeeApplication.java
+++ b/src/main/java/fr/organizee/OrganizeeApplication.java
@@ -1,13 +1,47 @@
package fr.organizee;
+import fr.organizee.model.Membre;
+import fr.organizee.model.Role;
+import fr.organizee.service.MembreService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
@SpringBootApplication
-public class OrganizeeApplication {
+public class OrganizeeApplication implements CommandLineRunner {
+
+ @Autowired
+ private MembreService membreService;
public static void main(String[] args) {
SpringApplication.run(OrganizeeApplication.class, args);
}
+ /**
+ * Ceci est un Bean, un composant
+ * Méthode de Hachage
+ * Bcrypt est un algorithme de hachage considé comme le plus sûr.
+ * bcrypt est un algorithme de hashage unidirectionnel,
+ * vous ne pourrez jamais retrouver le mot de passe sans connaitre à la fois le grain de sel,
+ * la clé et les différentes passes que l'algorithme à utiliser.
+ * Voir le site pour effectuer un test
+ *
+ * @return
+ */
+ @Bean
+ public BCryptPasswordEncoder bCryptPasswordEncoder() {
+ return new BCryptPasswordEncoder();
+ }
+
+ @Override
+ public void run(String... args) throws Exception {
+
+ }
}
+
diff --git a/src/main/java/fr/organizee/controller/ContactController.java b/src/main/java/fr/organizee/controller/ContactController.java
new file mode 100644
index 0000000..5186d12
--- /dev/null
+++ b/src/main/java/fr/organizee/controller/ContactController.java
@@ -0,0 +1,93 @@
+package fr.organizee.controller;
+
+import fr.organizee.model.Contact;
+import fr.organizee.model.Membre;
+import fr.organizee.model.Team;
+import fr.organizee.repository.ContactRepository;
+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.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.persistence.EntityNotFoundException;
+import java.util.List;
+import java.util.Optional;
+
+@RestController
+@CrossOrigin("*")
+@RequestMapping("/contacts")
+public class ContactController {
+
+ @Autowired
+ private ContactRepository contactRepo;
+
+ @GetMapping(value = "/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
+ public ResponseEntity> findById(@PathVariable int id){
+ Optional contact = null;
+ try
+ {
+ contact = contactRepo.findById(id);
+ } catch (Exception e) {
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
+ }
+
+ return ResponseEntity.status(HttpStatus.OK).body(contact);
+ }
+
+ @GetMapping(value = "team/{team_id}")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
+ public ResponseEntity> findByTeamId(@PathVariable int team_id){
+ List contacts = null;
+ try
+ {
+ contacts = contactRepo.FindContactsByTeam(team_id);
+ } catch (Exception e) {
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
+ }
+
+ return ResponseEntity.status(HttpStatus.OK).body(contacts);
+ }
+
+ @PostMapping(value="/add")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
+ public ResponseEntity> addContact(@RequestBody Contact contact){
+ Contact resultContact = null;
+ try {
+ resultContact = contactRepo.saveAndFlush(contact);
+ } catch (Exception e) {
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
+ }
+
+ return ResponseEntity.status(HttpStatus.CREATED).body(resultContact);
+ }
+
+ @PutMapping("/update/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
+ public ResponseEntity> updateContact(@RequestBody Contact contact, @PathVariable Integer id) throws Exception {
+ Contact resultContact = null;
+ try {
+ resultContact = contactRepo.save(contact);
+
+ } catch (Exception e) {
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
+ }
+
+ return ResponseEntity.status(HttpStatus.OK).body(resultContact);
+ }
+
+ @DeleteMapping(value = "/delete/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT')")
+ public ResponseEntity> deleteContact(@PathVariable int id){
+ try {
+ contactRepo.delete(contactRepo.getById(id));
+ return ResponseEntity.status(HttpStatus.OK).body("Contact effacé !");
+
+ } catch (EntityNotFoundException e) {
+
+ return ResponseEntity.status(HttpStatus.OK).body("Contact introuvable !");
+ }
+ }
+}
diff --git a/src/main/java/fr/organizee/controller/MembreController.java b/src/main/java/fr/organizee/controller/MembreController.java
index c69d234..2e9f691 100644
--- a/src/main/java/fr/organizee/controller/MembreController.java
+++ b/src/main/java/fr/organizee/controller/MembreController.java
@@ -1,17 +1,28 @@
package fr.organizee.controller;
+import fr.organizee.dto.JsonWebToken;
+import fr.organizee.dto.MembreDto;
+import fr.organizee.exception.ExistingUsernameException;
+import fr.organizee.exception.InvalidCredentialsException;
import fr.organizee.model.Membre;
import fr.organizee.model.Team;
import fr.organizee.repository.MembreRepository;
+<<<<<<< HEAD
import fr.organizee.repository.TeamRepository;
+=======
+//import fr.organizee.repository.TeamRepository;
+import fr.organizee.service.MembreService;
+>>>>>>> b147bc406c22caa9c817c7189a325058d8550a69
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
+import java.util.stream.Collectors;
/* toto */
@RestController
@@ -22,7 +33,14 @@ public class MembreController {
@Autowired
private MembreRepository membreRepo;
+<<<<<<< HEAD
// @Autowired
+=======
+ @Autowired
+ private MembreService membreService;
+
+// @Autowired
+>>>>>>> b147bc406c22caa9c817c7189a325058d8550a69
// private TeamRepository teamRepo;
// @RequestMapping("/membres")
@@ -36,6 +54,7 @@ public class MembreController {
}
@GetMapping(value = "/all")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity> getAll(){
List liste = null;
try
@@ -48,6 +67,13 @@ public class MembreController {
return ResponseEntity.status(HttpStatus.OK).body(liste);
}
+ @GetMapping("/admin/all")
+ @PreAuthorize("hasRole('ROLE_PARENT')")
+ public List getAllAdminUsers() {
+ return membreService.findAllUsers().stream().map(appUser -> new MembreDto(appUser.getEmail(), appUser.getRoleList())).collect(Collectors.toList());
+
+ }
+
// @GetMapping(value = "/team/all")
// public ResponseEntity> getAllTeam(){
// List liste = null;
@@ -62,6 +88,7 @@ public class MembreController {
// }
@GetMapping(value = "/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
public ResponseEntity> findById(@PathVariable int id){
Optional membre = null;
try
@@ -82,11 +109,12 @@ public class MembreController {
// }
@DeleteMapping(value = "/delete/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT')")
public ResponseEntity> deleteMembre(@PathVariable int id){
try {
membreRepo.delete(membreRepo.getById(id));
//membreRepo.deleteById(id);
- return ResponseEntity.status(HttpStatus.OK).body("Membre effacée !");
+ return ResponseEntity.status(HttpStatus.OK).body("Membre effacé !");
} catch (EntityNotFoundException e) {
@@ -94,19 +122,26 @@ public class MembreController {
}
}
- @PostMapping(value="/add", produces="application/json", consumes="application/json")
- public ResponseEntity> addMembre(@RequestBody Membre membre){
- Membre resultMembre = null;
+ @PostMapping("/sign-up")
+ public ResponseEntity signUp(@RequestBody Membre membre) {
try {
- resultMembre = membreRepo.saveAndFlush(membre);
- } catch (Exception e) {
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
+ return ResponseEntity.ok(new JsonWebToken(membreService.signup(membre)));
+ } catch (ExistingUsernameException ex) {
+ return ResponseEntity.badRequest().build();
}
+ }
- return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre);
+ @PostMapping("/sign-in")
+ public ResponseEntity signIn(@RequestBody Membre membre) {
+ try {
+ return ResponseEntity.ok(new JsonWebToken(membreService.signin(membre.getEmail(), membre.getPassword())));
+ } catch (InvalidCredentialsException ex) {
+ return ResponseEntity.badRequest().build();
+ }
}
@PutMapping("/update/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT')")
public ResponseEntity> updateMembre(@RequestBody Membre membre, @PathVariable Integer id) throws Exception {
Membre resultMembre = null;
try {
@@ -148,22 +183,4 @@ public class MembreController {
//
// return ResponseEntity.status(HttpStatus.OK).body(liste);
// }
-
- @PostMapping(value="/login", produces="application/json", consumes="application/json")
- public ResponseEntity> login(@RequestBody Membre membre){
- Membre resultMembre = null;
- try {
- resultMembre = membreRepo.findByNom(membre.getNom());
- if(resultMembre == null){
- throw new RuntimeException("User inexistant.");
- }
- if(!resultMembre.getPassword().equals(membre.getPassword())){
- throw new RuntimeException("mauvais password.");
- }
- } catch (Exception e) {
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
- }
-
- return ResponseEntity.status(HttpStatus.CREATED).body(resultMembre);
- }
}
diff --git a/src/main/java/fr/organizee/controller/MenuController.java b/src/main/java/fr/organizee/controller/MenuController.java
new file mode 100644
index 0000000..f4e4ec8
--- /dev/null
+++ b/src/main/java/fr/organizee/controller/MenuController.java
@@ -0,0 +1,90 @@
+package fr.organizee.controller;
+
+import fr.organizee.model.Contact;
+import fr.organizee.model.Menu;
+import fr.organizee.repository.MenuRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.persistence.EntityNotFoundException;
+import java.util.List;
+import java.util.Optional;
+
+@RestController
+@CrossOrigin("*")
+@RequestMapping("/menus")
+public class MenuController {
+
+ @Autowired
+ private MenuRepository menuRepository;
+
+ @GetMapping(value = "/{id}")
+ @PreAuthorize("hasRole('ROLE_PARENT') or hasRole('ROLE_ENFANT')")
+ public ResponseEntity> findById(@PathVariable int id){
+ Optional