wallabag/src/Repository/SiteCredentialRepository.php

55 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2016-12-04 12:51:58 +00:00
<?php
2024-02-19 00:30:12 +00:00
namespace Wallabag\Repository;
2016-12-04 12:51:58 +00:00
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\SiteCredential;
use Wallabag\Helper\CryptoProxy;
2017-06-11 21:05:19 +00:00
2016-12-04 12:51:58 +00:00
/**
* SiteCredentialRepository.
2022-11-23 14:51:33 +00:00
*
* @method SiteCredential[] findByUser(int $userId)
2016-12-04 12:51:58 +00:00
*/
class SiteCredentialRepository extends ServiceEntityRepository
2016-12-04 12:51:58 +00:00
{
2017-06-11 21:05:19 +00:00
private $cryptoProxy;
public function __construct(ManagerRegistry $registry, CryptoProxy $cryptoProxy)
2017-06-11 21:05:19 +00:00
{
parent::__construct($registry, SiteCredential::class);
2017-06-11 21:05:19 +00:00
$this->cryptoProxy = $cryptoProxy;
}
/**
* Retrieve one username/password for the given host and userId.
*
* @param array $hosts An array of host to look for
* @param int $userId
*
2019-02-11 10:50:24 +00:00
* @return array|null
*/
public function findOneByHostsAndUser($hosts, $userId)
{
2017-06-11 21:05:19 +00:00
$res = $this->createQueryBuilder('s')
->select('s.username', 's.password')
->where('s.host IN (:hosts)')->setParameter('hosts', $hosts)
->andWhere('s.user = :userId')->setParameter('userId', $userId)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
2017-06-11 21:05:19 +00:00
if (null === $res) {
2023-08-08 01:27:21 +00:00
return null;
2017-06-11 21:05:19 +00:00
}
// decrypt user & password before returning them
$res['username'] = $this->cryptoProxy->decrypt($res['username']);
2017-06-11 21:05:19 +00:00
$res['password'] = $this->cryptoProxy->decrypt($res['password']);
return $res;
}
2016-12-04 12:51:58 +00:00
}