Browse Source

dev en cours

rajah 7 months ago
parent
commit
acfb9557e2

+ 12 - 0
WEB-INF/web.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
+	<display-name>demovote-backend</display-name>
+	<welcome-file-list>
+		<welcome-file>index.html</welcome-file>
+		<welcome-file>index.htm</welcome-file>
+		<welcome-file>index.jsp</welcome-file>
+		<welcome-file>default.html</welcome-file>
+		<welcome-file>default.htm</welcome-file>
+		<welcome-file>default.jsp</welcome-file>
+	</welcome-file-list>
+</web-app>

+ 1 - 0
bin/main/application.properties

@@ -5,6 +5,7 @@ spring.datasource.username=vote
 spring.datasource.password=Atari$Impact2024
 spring.jpa.hibernate.ddl-auto=validate
 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
+spring.jpa.open-in-view=false
 
 spring.messages.basename=messages,langs.messages
 

+ 0 - 67
src/main/java/fr/triplea/demovote/dao/ParticipantRepository.java

@@ -8,7 +8,6 @@ import org.springframework.data.repository.query.Param;
 
 import fr.triplea.demovote.dto.ParticipantList;
 import fr.triplea.demovote.dto.ParticipantOptionList;
-import fr.triplea.demovote.dto.ParticipantRecord;
 import fr.triplea.demovote.model.Participant;
 import fr.triplea.demovote.model.Role;
 
@@ -18,39 +17,6 @@ public interface ParticipantRepository extends JpaRepository<Participant, Intege
   @NativeQuery("SELECT DISTINCT p.* FROM vote.participants AS p WHERE p.numero_participant = :id AND p.flag_actif IS TRUE ")
   Participant findById(@Param("id") int id);
 
-  @NativeQuery("SELECT DISTINCT "
-      + "TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
-      + "TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, "
-      + "p.numero_participant, "
-      + "'' AS role, "
-      + "p.nom, "
-      + "p.prenom, "
-      + "p.pseudonyme, "
-      + "'' AS mot_de_passe, "
-      + "p.groupe, "
-      + "p.delai_deconnexion, "
-      + "p.adresse, "
-      + "p.code_postal, "
-      + "p.ville, "
-      + "p.pays, "
-      + "p.numero_telephone, "
-      + "p.email, "
-      + "p.statut, "
-      + "p.flag_machine, "
-      + "p.commentaire, "
-      + "p.flag_jour1, "
-      + "p.flag_jour2, "
-      + "p.flag_jour3, "
-      + "p.flag_dodo_sur_place, "
-      + "p.flag_amigabus, "
-      + "p.mode_paiement, "
-      + "TO_CHAR(p.date_inscription, 'DD/MM/YYYY HH24:MI:SS') as date_inscription, "
-      + "CAST(p.somme_recue AS VARCHAR) AS somme_recue, "
-      + "p.flag_arrive "
-      + "FROM vote.participants AS p "
-      + "WHERE p.numero_participant = :id AND p.flag_actif IS TRUE ")
-  ParticipantRecord searchById(@Param("id") int id);
-
   @NativeQuery("SELECT DISTINCT "
       + "p.numero_participant, "
       + "p.nom, "
@@ -99,39 +65,6 @@ public interface ParticipantRepository extends JpaRepository<Participant, Intege
   @NativeQuery("SELECT DISTINCT p.* FROM vote.participants_roles AS rp INNER JOIN vote.participants AS p ON rp.numero_participant = p.numero_participant INNER JOIN vote.roles AS r ON rp.numero_role = r.numero_role WHERE p.flag_actif IS TRUE AND r.flag_actif IS TRUE AND rp.numero_role = :role ORDER BY p.nom ASC, p.prenom ASC, p.pseudonyme ASC ")
   List<Participant> findByRole(@Param("role") Role role);
 
-  @NativeQuery("SELECT DISTINCT "
-      + "TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
-      + "TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, "
-      + "p.numero_participant, "
-      + "'' AS role, "
-      + "p.nom, "
-      + "p.prenom, "
-      + "p.pseudonyme, "
-      + "'' AS mot_de_passe, "
-      + "p.groupe, "
-      + "p.delai_deconnexion, "
-      + "p.adresse, "
-      + "p.code_postal, "
-      + "p.ville, "
-      + "p.pays, "
-      + "p.numero_telephone, "
-      + "p.email, "
-      + "p.statut, "
-      + "p.flag_machine, "
-      + "p.commentaire, "
-      + "p.flag_jour1, "
-      + "p.flag_jour2, "
-      + "p.flag_jour3, "
-      + "p.flag_dodo_sur_place, "
-      + "p.flag_amigabus, "
-      + "p.mode_paiement, "
-      + "TO_CHAR(p.date_inscription, 'DD/MM/YYYY HH24:MI:SS') as date_inscription, "
-      + "CAST(p.somme_recue AS VARCHAR) AS somme_recue, "
-      + "p.flag_arrive "
-      + "FROM vote.participants AS p "
-      + "WHERE p.pseudonyme = :pseudo AND p.flag_actif IS TRUE ")
-  ParticipantRecord searchByPseudonyme(@Param("pseudo") String pseudonyme);
-
   @NativeQuery("SELECT DISTINCT p.* FROM vote.participants AS p WHERE p.flag_actif IS TRUE AND p.pseudonyme = :pseudo ORDER BY p.nom ASC, p.prenom ASC, p.pseudonyme ASC ")
   Participant findByPseudonyme(@Param("pseudo") String pseudonyme);
   

+ 0 - 36
src/main/java/fr/triplea/demovote/dto/ParticipantRecord.java

@@ -1,36 +0,0 @@
-package fr.triplea.demovote.dto;
-
-public record ParticipantRecord
-(
-  String dateCreation,
-  String dateModification,
-  int numeroParticipant,
-  String role,
-  String nom,
-  String prenom,
-  String pseudonyme,
-  String motDePasse,
-  String groupe,
-  int delaiDeconnexion,
-  String adresse,
-  String codePostal,
-  String ville,
-  String pays,
-  String numeroTelephone,
-  String email,
-  String statut,
-  boolean withMachine,
-  String commentaire,
-  boolean hereDay1,
-  boolean hereDay2,
-  boolean hereDay3,
-  boolean sleepingOnSite,
-  boolean useAmigabus,
-  String modePaiement,
-  String dateInscription,
-  String sommeRecue,
-  boolean arrived
-) 
-{
- 
-}

+ 60 - 7
src/main/java/fr/triplea/demovote/web/controller/AccountController.java

@@ -1,5 +1,7 @@
 package fr.triplea.demovote.web.controller;
 
+import java.time.format.DateTimeFormatter;
+import java.util.List;
 import java.util.Locale;
 
 import org.slf4j.Logger;
@@ -18,15 +20,18 @@ import org.springframework.web.servlet.LocaleResolver;
 
 import fr.triplea.demovote.dao.ParticipantRepository;
 import fr.triplea.demovote.dto.MessagesTransfer;
-import fr.triplea.demovote.dto.ParticipantRecord;
 import fr.triplea.demovote.dto.ParticipantTransfer;
 import fr.triplea.demovote.model.Participant;
+import fr.triplea.demovote.model.Role;
 import jakarta.servlet.http.HttpServletRequest;
 
 @RestController
 @RequestMapping("/account")
 public class AccountController 
 {
+
+  // TODO : changement du mot de passe
+
   @SuppressWarnings("unused") 
   private static final Logger LOG = LoggerFactory.getLogger(AccountController.class);
 
@@ -42,15 +47,65 @@ public class AccountController
   @Autowired
   private MessageSource messageSource;
 
+  private final DateTimeFormatter dtf_fr = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); 
+  private final DateTimeFormatter dft_en = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss"); 
   
   @GetMapping(value = "/form")
-  public ResponseEntity<ParticipantRecord> getForm(final Authentication authentication) 
+  public ResponseEntity<ParticipantTransfer> getForm(final Authentication authentication, HttpServletRequest request) 
   {         
+    Locale locale = localeResolver.resolveLocale(request);
+
+    DateTimeFormatter dtf = this.dtf_fr; if (locale == Locale.ENGLISH) { dtf = this.dft_en; }
+
     if (authentication != null)
     {
-      ParticipantRecord found = participantRepository.searchByPseudonyme(authentication.getName());
+      Participant found = participantRepository.findByPseudonyme(authentication.getName());
       
-      if (found != null) { return ResponseEntity.ok(found); }
+      if (found != null) 
+      { 
+        ParticipantTransfer p = new ParticipantTransfer();
+        
+        p.setDateCreation(found.hasDateCreation() ? dtf.format(found.getDateCreation()) : "");
+        p.setDateModification(found.hasDateCreation() ? dtf.format(found.getDateModification()) : ""); 
+        p.setNumeroParticipant(found.getNumeroParticipant());
+        
+        p.setNom(found.getNom());
+        p.setPrenom(found.getPrenom());
+        p.setPseudonyme(found.getPseudonyme());
+        
+        p.setGroupe(found.getGroupe()); 
+        p.setDelaiDeconnexion(15);
+        p.setAdresse(found.getAdresse());
+        p.setCodePostal(found.getCodePostal());
+        p.setVille(found.getVille());
+        p.setPays(found.getPays());
+        p.setNumeroTelephone(found.getNumeroTelephone());
+        p.setEmail(found.getEmail());
+                 
+        p.setStatut("");
+        
+        p.setWithMachine(found.isWithMachine());
+        p.setCommentaire(found.getCommentaire());
+        p.setHereDay1(found.isHereDay1());
+        p.setHereDay2(found.isHereDay2());
+        p.setHereDay3(found.isHereDay3());
+        p.setSleepingOnSite(found.isSleepingOnSite());
+        p.setUseAmigabus(found.isUseAmigabus());
+         
+        p.setModePaiement("");        
+        p.setSommeRecue("");
+        
+        p.setDateInscription(found.hasDateInscription() ? dtf.format(found.getDateInscription()) : "");
+        p.setArrived(found.isArrived());
+       
+        List<Role> roles = found.getRoles();       
+        
+        if (!(p.hasRole())) { for (Role role : roles) { if (role.isRole("ADMIN")) { p.setRole("ADMIN"); } } }
+        if (!(p.hasRole())) { for (Role role : roles) { if (role.isRole("ORGA")) { p.setRole("ORGA"); } } }
+        if (!(p.hasRole())) { p.setRole("USER"); } 
+
+        return ResponseEntity.ok(p); 
+      }
     }
    
     return ResponseEntity.notFound().build();
@@ -84,9 +139,7 @@ public class AccountController
         found.setEmail(participant.getEmail());
          
         found.setCommentaire(participant.getCommentaire());
-       
-        // TODO: modify password in session
-        
+               
         participantRepository.save(found);
        
         MessagesTransfer mt = new MessagesTransfer();

+ 2 - 3
src/main/java/fr/triplea/demovote/web/controller/AuthController.java

@@ -22,7 +22,6 @@ import org.springframework.web.servlet.LocaleResolver;
 
 import fr.triplea.demovote.dao.ParticipantRepository;
 import fr.triplea.demovote.dto.JourneesTransfer;
-import fr.triplea.demovote.dto.ParticipantRecord;
 import fr.triplea.demovote.dto.RefreshTokenTransfer;
 import fr.triplea.demovote.dto.UserCredentials;
 import fr.triplea.demovote.model.Participant;
@@ -181,9 +180,9 @@ public class AuthController
   {
     if (authentication != null)
     {
-      ParticipantRecord found = participantRepository.searchByPseudonyme(authentication.getName());
+      Participant found = participantRepository.findByPseudonyme(authentication.getName());
       
-      if (found != null) { refreshTokenService.deleteByNumeroParticipant(found.numeroParticipant()); }
+      if (found != null) { refreshTokenService.deleteByNumeroParticipant(found.getNumeroParticipant()); }
     }
 
     SecurityContextHolder.clearContext();

+ 2 - 0
src/main/java/fr/triplea/demovote/web/controller/BulletinController.java

@@ -30,6 +30,8 @@ import jakarta.servlet.http.HttpServletRequest;
 public class BulletinController 
 {
 
+  // TODO
+
   @Autowired
   private BulletinRepository bulletinRepository;
 

+ 2 - 0
src/main/java/fr/triplea/demovote/web/controller/MessageController.java

@@ -17,6 +17,8 @@ import fr.triplea.demovote.model.Message;
 public class MessageController 
 {
 
+  // TODO
+
   @Autowired
   private MessageRepository messageRepository;
   

+ 80 - 20
src/main/java/fr/triplea/demovote/web/controller/ParticipantController.java

@@ -1,18 +1,21 @@
 package fr.triplea.demovote.web.controller;
 
 import java.math.BigDecimal;
-import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.MessageSource;
 import org.springframework.http.ResponseEntity;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.security.core.Authentication;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -26,6 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.servlet.LocaleResolver;
 
 import fr.triplea.demovote.dao.ParticipantRepository;
+import fr.triplea.demovote.dao.RoleRepository;
 import fr.triplea.demovote.dto.MessagesTransfer;
 import fr.triplea.demovote.dto.ParticipantList;
 import fr.triplea.demovote.dto.ParticipantOptionList;
@@ -41,6 +45,9 @@ import jakarta.servlet.http.HttpServletRequest;
 public class ParticipantController 
 {
 
+  @Autowired
+  private RoleRepository roleRepository;
+
   @Autowired
   private ParticipantRepository participantRepository;
   
@@ -52,9 +59,6 @@ public class ParticipantController
   
   @Autowired
   private MessageSource messageSource;
-
-  private final SimpleDateFormat sdt_fr = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
-  private final SimpleDateFormat sdt_en = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); 
   
 
   @GetMapping(value = "/list")
@@ -76,25 +80,26 @@ public class ParticipantController
     return participantRepository.getOptionList(); 
   }
 
+
+  private final DateTimeFormatter dtf_fr = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); 
+  private final DateTimeFormatter dft_en = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss"); 
+ 
   @GetMapping(value = "/form/{id}")
   @PreAuthorize("hasRole('ORGA')")
   public ResponseEntity<ParticipantTransfer> getForm(@PathVariable int id, HttpServletRequest request) 
   { 
     Locale locale = localeResolver.resolveLocale(request);
 
-    SimpleDateFormat sdt = this.sdt_fr; if (locale == Locale.ENGLISH) { sdt = this.sdt_en; }
-    
-    Participant found = participantRepository.findById(id);
+    DateTimeFormatter dtf = this.dtf_fr; if (locale == Locale.ENGLISH) { dtf = this.dft_en; }
     
+    Participant found = participantRepository.findById(id);   
     
     if (found != null)
     {
       ParticipantTransfer p = new ParticipantTransfer();
       
-      // TODO: dates
-      
-      //p.setDateCreation(found.hasDateCreation() ? sdt.format(found.getDateCreation()) : "");
-      //p.setDateModification(found.hasDateCreation() ? sdt.format(found.getDateModification()) : ""); 
+      p.setDateCreation(found.hasDateCreation() ? dtf.format(found.getDateCreation()) : "");
+      p.setDateModification(found.hasDateCreation() ? dtf.format(found.getDateModification()) : ""); 
       p.setNumeroParticipant(found.getNumeroParticipant());
       
       p.setNom(found.getNom());
@@ -133,7 +138,7 @@ public class ParticipantController
       else { p.setModePaiement("AUTRE"); }
       
       try { p.setSommeRecue(found.getSommeRecue().toString()); } catch (Exception e) { p.setSommeRecue("0.00"); }
-      //p.setDateInscription(found.hasDateInscription() ? sdt.format(found.getDateInscription()) : "");
+      p.setDateInscription(found.hasDateInscription() ? dtf.format(found.getDateInscription()) : "");
       p.setArrived(found.isArrived());
      
       List<Role> roles = found.getRoles();       
@@ -142,7 +147,7 @@ public class ParticipantController
       if (!(p.hasRole())) { for (Role role : roles) { if (role.isRole("ORGA")) { p.setRole("ORGA"); } } }
       if (!(p.hasRole())) { p.setRole("USER"); } 
 
-     return ResponseEntity.ok(p); 
+      return ResponseEntity.ok(p); 
     }
     
     return ResponseEntity.notFound().build();
@@ -150,7 +155,7 @@ public class ParticipantController
 
   @PostMapping(value = "/create")
   @PreAuthorize("hasRole('ORGA')")
-  public ResponseEntity<Object> create(@RequestBody(required = true) ParticipantTransfer participant, HttpServletRequest request) 
+  public ResponseEntity<Object> create(@RequestBody(required = true) ParticipantTransfer participant, final Authentication authentication, HttpServletRequest request) 
   { 
     Locale locale = localeResolver.resolveLocale(request);
 
@@ -210,8 +215,36 @@ public class ParticipantController
           found.setDateInscription(LocalDateTime.now());
           found.setArrived(participant.isArrived());
           
-          // TODO: set roles
-          
+          Role userRole = roleRepository.findByLibelle("ROLE_USER");
+
+          if (authentication != null)
+          {
+            Role adminRole = roleRepository.findByLibelle("ROLE_ADMIN");
+            Role orgaRole = roleRepository.findByLibelle("ROLE_ORGA");
+           
+            if ((adminRole != null) && (orgaRole != null) && (userRole != null))
+            {
+              List<String> granter_roles = authentication.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toList());
+              
+              if (participant.getRole().equals("ADMIN") && granter_roles.contains("ROLE_ADMIN"))
+              {
+                found.setRoles(Arrays.asList(adminRole, orgaRole, userRole));
+              }
+              else if (participant.getRole().equals("ORGA") && granter_roles.contains("ROLE_ORGA"))
+              {
+                found.setRoles(Arrays.asList(orgaRole, userRole));
+              }
+              else
+              {
+                found.setRoles(Arrays.asList(userRole));
+              }
+            }
+          }
+          else
+          {
+            if (userRole != null) { found.setRoles(Arrays.asList(userRole)); }
+          }
+                    
           participantRepository.save(found);
           
           MessagesTransfer mt = new MessagesTransfer();
@@ -227,7 +260,7 @@ public class ParticipantController
 
   @PutMapping(value = "/update/{id}")
   @PreAuthorize("hasRole('ORGA')")
-  public ResponseEntity<Object> update(@PathVariable int id, @RequestBody(required = true) ParticipantTransfer participant, HttpServletRequest request) 
+  public ResponseEntity<Object> update(@PathVariable int id, @RequestBody(required = true) ParticipantTransfer participant, final Authentication authentication, HttpServletRequest request) 
   { 
     Locale locale = localeResolver.resolveLocale(request);
 
@@ -278,10 +311,37 @@ public class ParticipantController
       else { found.setModePaiement(ParticipantModePaiement.AUTRE); }
       
       try { found.setSommeRecue(new BigDecimal(participant.getSommeRecue())); } catch (Exception e) { found.setSommeRecue(new BigDecimal("0.00")); }
-      found.setArrived(participant.isArrived());
+      found.setArrived(participant.isArrived());  
       
-      // TODO: modify password in session
-      // TODO: modify roles
+      Role userRole = roleRepository.findByLibelle("ROLE_USER");
+
+      if (authentication != null)
+      {
+        Role adminRole = roleRepository.findByLibelle("ROLE_ADMIN");
+        Role orgaRole = roleRepository.findByLibelle("ROLE_ORGA");
+       
+        if ((adminRole != null) && (orgaRole != null) && (userRole != null))
+        {
+          List<String> granter_roles = authentication.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toList());
+          
+          if (participant.getRole().equals("ADMIN") && granter_roles.contains("ROLE_ADMIN"))
+          {
+            found.setRoles(Arrays.asList(adminRole, orgaRole, userRole));
+          }
+          else if (participant.getRole().equals("ORGA") && granter_roles.contains("ROLE_ORGA"))
+          {
+            found.setRoles(Arrays.asList(orgaRole, userRole));
+          }
+          else
+          {
+            found.setRoles(Arrays.asList(userRole));
+          }
+        }
+      }
+      else
+      {
+        if (userRole != null) { found.setRoles(Arrays.asList(userRole)); }
+      }
 
       participantRepository.save(found);
       

+ 2 - 0
src/main/java/fr/triplea/demovote/web/controller/PreferenceController.java

@@ -22,6 +22,8 @@ import fr.triplea.demovote.model.Preference;
 public class PreferenceController 
 {
 
+  // TODO
+
   @Autowired
   private PreferenceRepository preferenceRepository;
 

+ 2 - 0
src/main/java/fr/triplea/demovote/web/controller/PresentationController.java

@@ -17,6 +17,8 @@ import fr.triplea.demovote.model.Presentation;
 public class PresentationController 
 {
 
+  // TODO
+  
   @Autowired
   private PresentationRepository presentationRepository;
  

+ 1 - 0
src/main/java/fr/triplea/demovote/web/controller/ProductionController.java

@@ -55,6 +55,7 @@ public class ProductionController
   private MessageSource messageSource;
 
   // TODO : externaliser le stockage des fichiers
+  // TODO : verrrouiller gestion au (ROLE_ADMIN) et à (ROLE_USER + uniquement les productions qu'il gère)
  
   @GetMapping(value = "/list")
   @PreAuthorize("hasRole('USER')")

+ 1 - 0
src/main/resources/application.properties

@@ -5,6 +5,7 @@ spring.datasource.username=vote
 spring.datasource.password=Atari$Impact2024
 spring.jpa.hibernate.ddl-auto=validate
 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
+spring.jpa.open-in-view=false
 
 spring.messages.basename=messages,langs.messages