Browse Source

dev en cours

rajah 6 months ago
parent
commit
c11f6b0e1a

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

@@ -104,11 +104,6 @@ public class CreateDefaultValues implements ApplicationListener<ContextRefreshed
     addVariableIfMissing("Catégories", "ETAPE2_SCRUTIN_CLOTURE", "TRUE");
     addVariableIfMissing("Catégories", "ETAPE3_RESULTATS_DEMASQUES", "TRUE");
     
-    addVariableIfMissing("Productions", "APERCU_SONORE_DEBUT", "10");
-    addVariableIfMissing("Productions", "APERCU_SONORE_LONGUEUR", "10");
-    addVariableIfMissing("Productions", "APERCU_IMAGE_TAILLE_MAX", "480");
-    addVariableIfMissing("Productions", "TAILLE_LIMITE_STOCKAGE_BASE", "4");
-    
     addVariableIfMissing("Résultats", "NOMBRE_CHOIX", "3");
     addVariableIfMissing("Résultats", "POINTS_POSITION_01", "3");
     addVariableIfMissing("Résultats", "POINTS_POSITION_02", "2");

+ 2 - 2
src/main/java/fr/triplea/demovote/dao/CategorieRepository.java

@@ -14,8 +14,8 @@ public interface CategorieRepository extends JpaRepository<Categorie, Integer>
   @NativeQuery("SELECT DISTINCT c.* FROM vote.categories AS c WHERE c.numero_categorie = :id AND c.flag_actif IS TRUE ")
   Categorie findById(@Param("id") int id);
   
-  @NativeQuery("SELECT DISTINCT c.* FROM vote.categories AS c WHERE c.flag_actif IS TRUE ORDER BY c.numero_ordre ASC ")
-  List<Categorie> findAll();
+  @NativeQuery("SELECT DISTINCT c.* FROM vote.categories AS c WHERE c.flag_actif IS TRUE AND ((:numero = 0 AND :admin IS TRUE) OR (:numero > 0 AND c.flag_affiche IS TRUE) OR (:admin IS FALSE AND c.flag_affiche IS TRUE)) ORDER BY c.numero_ordre ASC ")
+  List<Categorie> findAll(@Param("numero") int numeroParticipant, @Param("admin") boolean admin);
 
   @Override
   void delete(Categorie categorie);

+ 33 - 3
src/main/java/fr/triplea/demovote/web/controller/CategorieController.java

@@ -2,11 +2,13 @@ package fr.triplea.demovote.web.controller;
 
 import java.util.List;
 import java.util.Locale;
+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.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -14,12 +16,15 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.servlet.LocaleResolver;
 
 import fr.triplea.demovote.dao.CategorieRepository;
+import fr.triplea.demovote.dao.ParticipantRepository;
 import fr.triplea.demovote.dto.MessagesTransfer;
 import fr.triplea.demovote.model.Categorie;
+import fr.triplea.demovote.model.Participant;
 import jakarta.servlet.http.HttpServletRequest;
 
 @RestController
@@ -30,6 +35,9 @@ public class CategorieController
   @Autowired
   private CategorieRepository categorieRepository;
 
+  @Autowired
+  private ParticipantRepository participantRepository;
+
   @Autowired
   private LocaleResolver localeResolver;
   
@@ -38,10 +46,10 @@ public class CategorieController
 
 
   @GetMapping(value = "/list")
-  @PreAuthorize("hasRole('ADMIN')")
-  public List<Categorie> getList() 
+  @PreAuthorize("hasRole('USER')")
+  public List<Categorie> getList(@RequestParam(required = false) Boolean admin, final Authentication authentication) 
   { 
-    return categorieRepository.findAll(); 
+    return categorieRepository.findAll(this.getNumeroUser(authentication), admin); 
   }
 
   @GetMapping(value = "/form/{id}")
@@ -132,4 +140,26 @@ public class CategorieController
     return ResponseEntity.notFound().build(); 
   }
 
+  /** retourne 0 si ROLE_ADMIN, sinon c'est le numéro identifiant du participant USER */
+  private final int getNumeroUser(Authentication auth)
+  {
+    int numeroParticipant = -1; // -1 pour non trouvé
+    
+    if (auth != null)
+    {
+      Participant found = participantRepository.findByPseudonyme(auth.getName());
+      
+      if (found != null)
+      {
+        numeroParticipant = found.getNumeroParticipant();
+        
+        List<String> roles = auth.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toList());
+
+        if (roles.contains("ROLE_ADMIN")) { numeroParticipant = 0; }
+      }
+    }
+    
+    return numeroParticipant;
+  }
+
 }