Răsfoiți Sursa

dev en cours

rajah 6 luni în urmă
părinte
comite
74229a1868

+ 2 - 0
bin/main/application.properties

@@ -1,3 +1,5 @@
+production.mode=false
+
 spring.application.name=demovote-backend
 
 spring.datasource.url=jdbc:postgresql://localhost:5432/vote

+ 0 - 1
src/main/java/fr/triplea/demovote/CreateDefaultValues.java

@@ -98,7 +98,6 @@ public class CreateDefaultValues implements ApplicationListener<ContextRefreshed
     addVariableIfMissing("Accueil", "PARTICIPANTS_ARRIVES_SEULEMENT", "TRUE");
     
     addVariableIfMissing("Navigation", "LISTE_PARTICIPANTS_MAX", "300");
-    addVariableIfMissing("Navigation", "LISTE_VARIABLES_MAX", "100");
      
     addVariableIfMissing("Résultats", "NOMBRE_CHOIX", "3");
     addVariableIfMissing("Résultats", "POINTS_POSITION_01", "3");

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

@@ -3,6 +3,7 @@ package fr.triplea.demovote.dao;
 import java.util.List;
 
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.NativeQuery;
 import org.springframework.data.repository.query.Param;
 
@@ -87,4 +88,8 @@ public interface ParticipantRepository extends JpaRepository<Participant, Intege
   @NativeQuery("SELECT DISTINCT p.numero_participant, p.pseudonyme FROM vote.participants AS p WHERE (p.flag_actif IS TRUE) AND (p.numero_participant <> :id) AND (LENGTH(p.pseudonyme) > 0) ORDER BY p.pseudonyme ASC ")
   List<PseudonymeOptionList> getPseudonymeOptionList(@Param("id") int id);
 
+  @Modifying
+  @NativeQuery("UPDATE vote.participants SET flag_arrive = TRUE WHERE numero_participant IN :numeros ")
+  void setFlagArrives(@Param("numeros") List<Integer> listeNumeroParticipants);
+
 }

+ 23 - 3
src/main/java/fr/triplea/demovote/security/GlobalExceptionHandler.java

@@ -1,5 +1,8 @@
 package fr.triplea.demovote.security;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -12,13 +15,20 @@ import fr.triplea.demovote.security.jwt.RefreshTokenException;
 @RestControllerAdvice
 public class GlobalExceptionHandler 
 {
-
-  // TODO : en mode production, masquer les requêtes SQL (ne pas donner d'indices sur le schema)
   
+  //@SuppressWarnings("unused") 
+  private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
+  
+  @Value("${production.mode}")
+  private boolean modeProduction;
+  
+
   @ExceptionHandler(value = RefreshTokenException.class)
   @ResponseStatus(HttpStatus.FORBIDDEN)
   public ResponseEntity<JsonErrorResponse> handleTokenRefreshException(RefreshTokenException ex) 
   {
+    LOG.error(ex.getMessage());
+    
     JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.FORBIDDEN.value(), ex.getMessage());
 
     return new ResponseEntity<>(jer, HttpStatus.FORBIDDEN);
@@ -28,7 +38,13 @@ public class GlobalExceptionHandler
   @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
   public ResponseEntity<JsonErrorResponse> handleAllExceptions(Exception ex) 
   {
-    JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
+    LOG.error(ex.getMessage());
+    
+    String message =  ex.getMessage();
+    
+    if (modeProduction) { if (message.contains("JDBC") || message.contains("SQL")) { message = "JDBC or SQL error, please contact the administrator to look in logs"; } }
+    
+    JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
 
     return new ResponseEntity<>(jer, HttpStatus.INTERNAL_SERVER_ERROR);
   }
@@ -37,6 +53,8 @@ public class GlobalExceptionHandler
   @ResponseStatus(HttpStatus.BAD_REQUEST)
   public ResponseEntity<JsonErrorResponse> handleNullPointerException(NullPointerException ex) 
   {
+    LOG.error(ex.getMessage());
+    
     JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
 
     return new ResponseEntity<>(jer, HttpStatus.BAD_REQUEST);
@@ -45,6 +63,8 @@ public class GlobalExceptionHandler
   @ExceptionHandler(ResourceNotFoundException.class)
   public ResponseEntity<JsonErrorResponse> handleNotFound(ResourceNotFoundException ex) 
   {
+    LOG.error(ex.getMessage());
+    
     JsonErrorResponse jer = new JsonErrorResponse(HttpStatus.NOT_FOUND.value(), ex.getMessage());
     
     return new ResponseEntity<>(jer, HttpStatus.NOT_FOUND);

+ 4 - 0
src/main/java/fr/triplea/demovote/web/controller/AuthController.java

@@ -38,6 +38,8 @@ import jakarta.validation.Valid;
 @RequestMapping("/sign")
 public class AuthController 
 {
+  
+  // TODO : liste des participants arrivés sur le formulaire de login pour aider le participant ?
   // TODO : captcha ?
   
   @SuppressWarnings("unused") 
@@ -95,6 +97,8 @@ public class AuthController
       
       if (passwordEncoder.matches(pass, userDetails.getPassword()))
       {
+        // TODO : restreindre la connexion aux participants avec flag 'arrivés' à true ?
+        
         SecurityContextHolder.getContext().setAuthentication(authentication);
         
         String token = jwtTokenUtil.generateJwtToken(authentication);

+ 31 - 1
src/main/java/fr/triplea/demovote/web/controller/ParticipantController.java

@@ -17,6 +17,7 @@ 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.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -44,7 +45,7 @@ import jakarta.servlet.http.HttpServletRequest;
 @RequestMapping("/participant")
 public class ParticipantController 
 {
-
+  
   @Autowired
   private RoleRepository roleRepository;
 
@@ -383,4 +384,33 @@ public class ParticipantController
     return ResponseEntity.notFound().build(); 
   }
 
+  
+
+  @PutMapping(value = "/arrived")
+  @PreAuthorize("hasRole('ORGA')")
+  @Transactional
+  public ResponseEntity<Object> update(@RequestBody List<Integer> numerosParticipants, final Authentication authentication, HttpServletRequest request) 
+  { 
+    Locale locale = localeResolver.resolveLocale(request);
+
+    if (numerosParticipants != null)
+    {
+      if (numerosParticipants.size() > 0)
+      {
+        participantRepository.setFlagArrives(numerosParticipants);
+        participantRepository.flush();
+        
+        Map<String, Boolean> response = new HashMap<>();
+        response.put("changed", Boolean.TRUE);
+
+        MessagesTransfer mt = new MessagesTransfer();
+        mt.setAlerte(messageSource.getMessage("participant.arrived", null, locale));
+
+        return ResponseEntity.ok(response); 
+      }
+    }
+    
+    return ResponseEntity.notFound().build();
+  }
+
 }

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

@@ -1,3 +1,5 @@
+production.mode=false
+
 spring.application.name=demovote-backend
 
 spring.datasource.url=jdbc:postgresql://localhost:5432/vote