aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
blob: 36906761243616bd458c5f83682a0be92c165751 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace Wallabag\CoreBundle\Repository;

use Wallabag\CoreBundle\Helper\CryptoProxy;

/**
 * SiteCredentialRepository.
 */
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 null|array
     */
    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;
    }
}