浏览代码

Merge branch 'hotfix/V8-4025_file_get_on_v1'

Olivier Massot 2 年之前
父节点
当前提交
a4c5340396

+ 6 - 2
src/DataProvider/Core/DownloadRequestDataProvider.php

@@ -12,6 +12,8 @@ use App\Service\File\FileManager;
 use Symfony\Component\HttpFoundation\HeaderUtils;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
+use Symfony\Component\Security\Core\Security;
 
 /**
  * Custom provider pour le téléchargement des fichiers du LocalStorage
@@ -20,7 +22,8 @@ final class DownloadRequestDataProvider implements ItemDataProviderInterface, Re
 {
     public function __construct(
         private FileRepository $fileRepository,
-        private FileManager $fileManager
+        private FileManager $fileManager,
+        private Security $security,
     )
     {}
 
@@ -40,7 +43,8 @@ final class DownloadRequestDataProvider implements ItemDataProviderInterface, Re
         }
 
         // Read the file
-        $content = $this->fileManager->read($file);
+        $token = $this->security->getToken();
+        $content = $this->fileManager->read($file, $token);
 
         // Build the response and attach the file to it
         // @see https://symfony.com/doc/current/components/http_foundation.html#serving-files

+ 15 - 0
src/Entity/Organization/Organization.php

@@ -116,6 +116,9 @@ class Organization
     #[ORM\Column(length: 10, nullable: true)]
     private ?string $waldecNumber = null;
 
+    #[ORM\Column(length: 255, nullable: true)]
+    private ?string $volumeAndFolioNumber = null;
+
     #[ORM\Column(length: 5, nullable: true)]
     private ?string $apeNumber = null;
 
@@ -324,6 +327,7 @@ class Organization
     #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id')]
     private Collection $tags;
 
+
     #[Pure] public function __construct()
     {
         $this->accesses = new ArrayCollection();
@@ -592,6 +596,17 @@ class Organization
         return $this;
     }
 
+    public function getVolumeAndFolioNumber(): ?string
+    {
+        return $this->volumeAndFolioNumber;
+    }
+
+    public function setVolumeAndFolioNumber(?string $volumeAndFolioNumber): self
+    {
+        $this->volumeAndFolioNumber = $volumeAndFolioNumber;
+        return $this;
+    }
+
     public function getApeNumber(): ?string
     {
         return $this->apeNumber;

+ 6 - 1
src/Security/Voter/ModuleVoter.php

@@ -59,7 +59,12 @@ class ModuleVoter extends Voter
         /** @var Organization $organization */
         $organization = $currentAccess->getOrganization();
 
-        return $this->isOrganizationHaveThisModule($organization, $module);
+        if (!$this->isOrganizationHaveThisModule($organization, $module)) {
+            throw new AccessDeniedHttpException(
+                sprintf("The organization doesn't have the module '%s'", $module)
+            );
+        }
+        return true;
     }
 
     /**

+ 9 - 2
src/Service/ApiLegacy/ApiLegacyRequestService.php

@@ -4,6 +4,7 @@ namespace App\Service\ApiLegacy;
 
 use App\Service\Rest\ApiRequestService;
 use JetBrains\PhpStorm\Pure;
+use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\Security\Core\Authentication\Token\NullToken;
 use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
@@ -17,7 +18,11 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
 class ApiLegacyRequestService extends ApiRequestService
 {
     #[Pure]
-    public function __construct(HttpClientInterface $apiLegacyClient, private Security $security)
+    public function __construct(
+        HttpClientInterface $apiLegacyClient,
+        private Security $security,
+        private JWTTokenManagerInterface $jwtManager
+    )
     {
         parent::__construct($apiLegacyClient);
     }
@@ -36,7 +41,6 @@ class ApiLegacyRequestService extends ApiRequestService
         }
 
         $headers = [
-            'authorization' => 'BEARER ' . $_REQUEST['BEARER'],
             'Accept' => '*/*',
             'Charset' => 'UTF-8',
             'Accept-Encoding' => 'gzip, deflate, br',
@@ -49,12 +53,15 @@ class ApiLegacyRequestService extends ApiRequestService
                 throw new HttpException(500, 'Request error : Switch original user missing');
             }
 
+            $jwt = $this->jwtManager->create($originalUser->getPerson());
             $headers['x-accessid'] = $originalUser->getId();
             $headers['x-switch-access'] = $token->getUser()->getId();
         } else {
+            $jwt = $this->jwtManager->create($token->getUser()->getPerson());
             $headers['x-accessid'] = $token->getUser()->getId();
         }
 
+        $headers['authorization'] = 'BEARER ' . $jwt;
         $options['headers'] = array_merge($options['headers'] ?? [], $headers);
 
         return parent::request($method, $url, $parameters, $options);

+ 10 - 3
src/Service/File/FileManager.php

@@ -12,6 +12,8 @@ use App\Service\File\Storage\ApiLegacyStorage;
 use App\Service\File\Storage\FileStorageInterface;
 use App\Service\File\Storage\LocalStorage;
 use Mimey\MimeTypes;
+use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
 
 /**
  * Le gestionnaire de fichiers permet d'effectuer de nombreuses opérations sur les fichiers stockés dans les différents
@@ -45,14 +47,19 @@ class FileManager
     }
 
     /**
-     * Lis le fichier et retourne son contenu
+     * Lit le fichier et retourne son contenu
      *
      * @param File $file
+     * @param TokenInterface|null $token  Used to read files from API v1 with a switch account
      * @return string
      * @throws FileNotFoundException
      */
-    public function read(File $file): string {
-        return $this->getStorageFor($file)->read($file);
+    public function read(File $file, ?TokenInterface $token=null): string {
+        $storage = $this->getStorageFor($file);
+        if ($storage instanceof ApiLegacyStorage && $token instanceof SwitchUserToken) {
+            $storage->setSwitchAccount($token->getOriginalToken()->getUser()->getId(), $token->getUser()->getId());
+        }
+        return $storage->read($file);
     }
 
 

+ 20 - 2
src/Service/File/Storage/ApiLegacyStorage.php

@@ -17,9 +17,23 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
  */
 class ApiLegacyStorage implements FileStorageInterface
 {
+    /**
+     *  Si ces ids sont renseignés, on est en mode 'switch' et l'url de téléchargement est un peu différente
+     */
+    private ?int $originalAccessId;
+    private ?int $asAccessId;
+
     public function __construct(
         private ApiLegacyRequestService $apiLegacyRequestService
-    ) {}
+    ) {
+        $this->originalAccessId = null;
+        $this->asAccessId = null;
+    }
+
+    public function setSwitchAccount(int $originalAccessId, int $asAccessId) {
+        $this->originalAccessId = $originalAccessId;
+        $this->asAccessId = $asAccessId;
+    }
 
     /**
      * Reads the given file and returns its content as a string
@@ -29,6 +43,10 @@ class ApiLegacyStorage implements FileStorageInterface
      */
     public function read(File $file): string
     {
-        return $this->apiLegacyRequestService->getContent('api/files/' . $file->getId() .'/download');
+        $url = 'api/files/' . $file->getId() .'/download';
+        if ($this->originalAccessId !== null) {
+            $url = 'api/' . $this->originalAccessId . '/'. $this->asAccessId . '/files/' . $file->getId() .'/download';
+        }
+        return $this->apiLegacyRequestService->getContent($url);
     }
 }