use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
+use Wallabag\UserBundle\Entity\User;
class GrabySiteConfigBuilder implements SiteConfigBuilder
{
private $logger;
/**
- * @var Wallabag\UserBundle\Entity\User|null
+ * @var User|null
*/
private $currentUser;
/**
* GrabySiteConfigBuilder constructor.
- *
- * @param ConfigBuilder $grabyConfigBuilder
- * @param TokenStorage $token
- * @param SiteCredentialRepository $credentialRepository
- * @param LoggerInterface $logger
*/
public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
{
{
// required by credentials below
$host = strtolower($host);
- if (substr($host, 0, 4) === 'www.') {
+ if ('www.' === substr($host, 0, 4)) {
$host = substr($host, 4);
}
- $credentials = null;
- if ($this->currentUser) {
- $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId());
+ if (!$this->currentUser) {
+ $this->logger->debug('Auth: no current user defined.');
+
+ return false;
}
+ $hosts = [$host];
+ // will try to see for a host without the first subdomain (fr.example.org & .example.org)
+ $split = explode('.', $host);
+
+ if (\count($split) > 1) {
+ // remove first subdomain
+ array_shift($split);
+ $hosts[] = '.' . implode('.', $split);
+ }
+
+ $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
+
if (null === $credentials) {
$this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
*/
protected function processExtraFields($extraFieldsStrings)
{
- if (!is_array($extraFieldsStrings)) {
+ if (!\is_array($extraFieldsStrings)) {
return [];
}
$extraFields = [];
foreach ($extraFieldsStrings as $extraField) {
- if (strpos($extraField, '=') === false) {
+ if (false === strpos($extraField, '=')) {
continue;
}