Changed authentication order in GrabySiteConfigBuilder

This commit is contained in:
Nicolas Lœuillet 2020-04-07 10:29:47 +02:00
parent b283ee0d6d
commit de9b5b5f4c
No known key found for this signature in database
GPG key ID: 3A8718BE51C3BA8E

View file

@ -8,7 +8,6 @@ use Graby\SiteConfig\ConfigBuilder;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Wallabag\CoreBundle\Repository\SiteCredentialRepository; use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
use Wallabag\UserBundle\Entity\User;
class GrabySiteConfigBuilder implements SiteConfigBuilder class GrabySiteConfigBuilder implements SiteConfigBuilder
{ {
@ -28,9 +27,9 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
private $logger; private $logger;
/** /**
* @var User|null * @var TokenStorage
*/ */
private $currentUser; private $token;
/** /**
* GrabySiteConfigBuilder constructor. * GrabySiteConfigBuilder constructor.
@ -40,10 +39,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
$this->grabyConfigBuilder = $grabyConfigBuilder; $this->grabyConfigBuilder = $grabyConfigBuilder;
$this->credentialRepository = $credentialRepository; $this->credentialRepository = $credentialRepository;
$this->logger = $logger; $this->logger = $logger;
$this->token = $token;
if ($token->getToken()) {
$this->currentUser = $token->getToken()->getUser();
}
} }
/** /**
@ -51,13 +47,15 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
*/ */
public function buildForHost($host) public function buildForHost($host)
{ {
$user = $this->getUser();
// required by credentials below // required by credentials below
$host = strtolower($host); $host = strtolower($host);
if ('www.' === substr($host, 0, 4)) { if ('www.' === substr($host, 0, 4)) {
$host = substr($host, 4); $host = substr($host, 4);
} }
if (!$this->currentUser) { if (!$user) {
$this->logger->debug('Auth: no current user defined.'); $this->logger->debug('Auth: no current user defined.');
return false; return false;
@ -73,7 +71,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
$hosts[] = '.' . implode('.', $split); $hosts[] = '.' . implode('.', $split);
} }
$credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $user->getId());
if (null === $credentials) { if (null === $credentials) {
$this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
@ -131,4 +129,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
return $extraFields; return $extraFields;
} }
private function getUser()
{
if ($this->token->getToken() && null !== $this->token->getToken()->getUser()) {
return $this->token->getToken()->getUser();
}
return null;
}
} }