]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Retrieve username/password from database
authorJeremy Benoist <jeremy.benoist@gmail.com>
Mon, 1 May 2017 20:13:17 +0000 (22:13 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Tue, 20 Jun 2017 14:03:13 +0000 (16:03 +0200)
Inject the current user & the repo to retrieve username/password from the database

app/config/parameters.yml.dist
src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
src/Wallabag/CoreBundle/Resources/config/services.yml

index 914fb1efcba94fdec85a66d0d2b33b4d83f63673..b3fe11c87d5a89486e3e8eeeea5b7b07594f4297 100644 (file)
@@ -60,6 +60,3 @@ parameters:
     redis_port: 6379
     redis_path: null
     redis_password: null
-
-    # sites credentials
-    sites_credentials: {}
index 1c56fa9f44279c0fa800abc5726bbd9ad876e043..94615687ea45418773c6cbc40da36670be0e8f0d 100644 (file)
@@ -6,6 +6,8 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
 use Graby\SiteConfig\ConfigBuilder;
 use Psr\Log\LoggerInterface;
+use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
+use Wallabag\UserBundle\Entity\User;
 
 class GrabySiteConfigBuilder implements SiteConfigBuilder
 {
@@ -13,26 +15,36 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
      * @var ConfigBuilder
      */
     private $grabyConfigBuilder;
+
     /**
-     * @var array
+     * @var SiteCredentialRepository
      */
-    private $credentials;
+    private $credentialRepository;
+
     /**
      * @var LoggerInterface
      */
     private $logger;
 
+    /**
+     * @var User
+     */
+    private $currentUser;
+
+
     /**
      * GrabySiteConfigBuilder constructor.
      *
-     * @param ConfigBuilder   $grabyConfigBuilder
-     * @param array           $credentials
+     * @param ConfigBuilder            $grabyConfigBuilder
+     * @param User                     $currentUser
+     * @param SiteCredentialRepository $credentialRepository
      * @param LoggerInterface $logger
      */
-    public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
+    public function __construct(ConfigBuilder $grabyConfigBuilder, User $currentUser, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
     {
         $this->grabyConfigBuilder = $grabyConfigBuilder;
-        $this->credentials = $credentials;
+        $this->credentialRepository = $credentialRepository;
+        $this->currentUser = $currentUser;
         $this->logger = $logger;
     }
 
@@ -47,7 +59,9 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             $host = substr($host, 4);
         }
 
-        if (empty($this->credentials[$host])) {
+        $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId());
+
+        if (null === $credentials) {
             $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
 
             return false;
@@ -62,8 +76,8 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
             'passwordField' => $config->login_password_field ?: null,
             'extraFields' => $this->processExtraFields($config->login_extra_fields),
             'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
-            'username' => $this->credentials[$host]['username'],
-            'password' => $this->credentials[$host]['password'],
+            'username' => $credentials['username'],
+            'password' => $credentials['password'],
         ];
 
         $config = new SiteConfig($parameters);
index 88aee6d583da41fa777e6987365c7e176937aea5..316ecc75020e5400ef272a7e97664a50b6aa8910 100644 (file)
@@ -7,4 +7,22 @@ namespace Wallabag\CoreBundle\Repository;
  */
 class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository
 {
+    /**
+     * Retrieve one username/password for the given host and userId.
+     *
+     * @param string $host
+     * @param int    $userId
+     *
+     * @return null|array
+     */
+    public function findOneByHostAndUser($host, $userId)
+    {
+        return $this->createQueryBuilder('s')
+            ->select('s.username', 's.password')
+            ->where('s.host = :hostname')->setParameter('hostname', $host)
+            ->andWhere('s.user = :userId')->setParameter('userId', $userId)
+            ->setMaxResults(1)
+            ->getQuery()
+            ->getOneOrNullResult();
+    }
 }
index 4be7954702cba1a26f46bd06ce32404583129f30..a59152d354fa4ec25057701cd2f753a70b3fd818 100644 (file)
@@ -63,7 +63,8 @@ services:
         class: Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder
         arguments:
             - "@wallabag_core.graby.config_builder"
-            - "%sites_credentials%"
+            - "@=service('security.token_storage').getToken().getUser()"
+            - "@wallabag_core.site_credential_repository"
             - '@logger'
         tags:
             - { name: monolog.logger, channel: graby }
@@ -120,6 +121,12 @@ services:
         arguments:
             - WallabagCoreBundle:Tag
 
+    wallabag_core.site_credential_repository:
+        class: Wallabag\CoreBundle\Repository\SiteCredentialRepository
+        factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
+        arguments:
+            - WallabagCoreBundle:SiteCredential
+
     wallabag_core.helper.entries_export:
         class: Wallabag\CoreBundle\Helper\EntriesExport
         arguments: