]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
index 10689c62bbf82e631dda0da5f708b23dccc163f2..6ee2986ad9eec4fe5c7b8f7def0a70975440efd2 100644 (file)
@@ -27,27 +27,19 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
     private $logger;
 
     /**
-     * @var Wallabag\UserBundle\Entity\User|null
+     * @var TokenStorage
      */
-    private $currentUser;
+    private $token;
 
     /**
      * GrabySiteConfigBuilder constructor.
-     *
-     * @param ConfigBuilder            $grabyConfigBuilder
-     * @param TokenStorage             $token
-     * @param SiteCredentialRepository $credentialRepository
-     * @param LoggerInterface          $logger
      */
     public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
     {
         $this->grabyConfigBuilder = $grabyConfigBuilder;
         $this->credentialRepository = $credentialRepository;
         $this->logger = $logger;
-
-        if ($token->getToken()) {
-            $this->currentUser = $token->getToken()->getUser();
-        }
+        $this->token = $token;
     }
 
     /**
@@ -55,17 +47,32 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
      */
     public function buildForHost($host)
     {
+        $user = $this->getUser();
+
         // required by credentials below
         $host = strtolower($host);
-        if (substr($host, 0, 4) === 'www.') {
+        if ('www.' === substr($host, 0, 4)) {
             $host = substr($host, 4);
         }
 
-        $credentials = null;
-        if ($this->currentUser) {
-            $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId());
+        if (!$user) {
+            $this->logger->debug('Auth: no current user defined.');
+
+            return false;
         }
 
+        $hosts = [$host];
+        // will try to see for a host without the first subdomain (fr.example.org & .example.org)
+        $split = explode('.', $host);
+
+        if (\count($split) > 1) {
+            // remove first subdomain
+            array_shift($split);
+            $hosts[] = '.' . implode('.', $split);
+        }
+
+        $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $user->getId());
+
         if (null === $credentials) {
             $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
 
@@ -106,13 +113,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
      */
     protected function processExtraFields($extraFieldsStrings)
     {
-        if (!is_array($extraFieldsStrings)) {
+        if (!\is_array($extraFieldsStrings)) {
             return [];
         }
 
         $extraFields = [];
         foreach ($extraFieldsStrings as $extraField) {
-            if (strpos($extraField, '=') === false) {
+            if (false === strpos($extraField, '=')) {
                 continue;
             }
 
@@ -122,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;
+    }
 }