]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
CS
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / SiteCredentialRepository.php
index 501b44396e72273b80603e2b2d61e0edd61abd2f..b2e212a41438cc32d7e3bbab212aed78382b76fd 100644 (file)
@@ -2,12 +2,46 @@
 
 namespace Wallabag\CoreBundle\Repository;
 
+use Wallabag\CoreBundle\Helper\CryptoProxy;
+
 /**
  * SiteCredentialRepository.
- *
- * This class was generated by the Doctrine ORM. Add your own custom
- * repository methods below.
  */
 class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository
 {
+    private $cryptoProxy;
+
+    public function setCrypto(CryptoProxy $cryptoProxy)
+    {
+        $this->cryptoProxy = $cryptoProxy;
+    }
+
+    /**
+     * Retrieve one username/password for the given host and userId.
+     *
+     * @param string $host
+     * @param int    $userId
+     *
+     * @return array|null
+     */
+    public function findOneByHostAndUser($host, $userId)
+    {
+        $res = $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();
+
+        if (null === $res) {
+            return;
+        }
+
+        // decrypt user & password before returning them
+        $res['username'] = $this->cryptoProxy->decrypt($res['username']);
+        $res['password'] = $this->cryptoProxy->decrypt($res['password']);
+
+        return $res;
+    }
 }