]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Changed authentication order in GrabySiteConfigBuilder 4318/head
authorNicolas Lœuillet <nicolas@loeuillet.org>
Tue, 7 Apr 2020 08:29:47 +0000 (10:29 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Tue, 7 Apr 2020 15:12:19 +0000 (17:12 +0200)
src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php

index b0be21763c4db127bfd6e08ccfc26c832f5de365..6ee2986ad9eec4fe5c7b8f7def0a70975440efd2 100644 (file)
@@ -8,7 +8,6 @@ use Graby\SiteConfig\ConfigBuilder;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
 use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
-use Wallabag\UserBundle\Entity\User;
 
 class GrabySiteConfigBuilder implements SiteConfigBuilder
 {
@@ -28,9 +27,9 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
     private $logger;
 
     /**
-     * @var User|null
+     * @var TokenStorage
      */
-    private $currentUser;
+    private $token;
 
     /**
      * GrabySiteConfigBuilder constructor.
@@ -40,10 +39,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
         $this->grabyConfigBuilder = $grabyConfigBuilder;
         $this->credentialRepository = $credentialRepository;
         $this->logger = $logger;
-
-        if ($token->getToken()) {
-            $this->currentUser = $token->getToken()->getUser();
-        }
+        $this->token = $token;
     }
 
     /**
@@ -51,13 +47,15 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
      */
     public function buildForHost($host)
     {
+        $user = $this->getUser();
+
         // required by credentials below
         $host = strtolower($host);
         if ('www.' === substr($host, 0, 4)) {
             $host = substr($host, 4);
         }
 
-        if (!$this->currentUser) {
+        if (!$user) {
             $this->logger->debug('Auth: no current user defined.');
 
             return false;
@@ -73,7 +71,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             $hosts[] = '.' . implode('.', $split);
         }
 
-        $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
+        $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $user->getId());
 
         if (null === $credentials) {
             $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
@@ -131,4 +129,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
 
         return $extraFields;
     }
+
+    private function getUser()
+    {
+        if ($this->token->getToken() && null !== $this->token->getToken()->getUser()) {
+            return $this->token->getToken()->getUser();
+        }
+
+        return null;
+    }
 }