2015-03-28 13:27:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\ParamConverter;
|
|
|
|
|
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
|
2015-03-28 13:27:45 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2015-10-02 12:51:41 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2022-11-23 14:51:33 +00:00
|
|
|
use Wallabag\UserBundle\Repository\UserRepository;
|
2015-03-28 13:27:45 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-13 16:48:10 +00:00
|
|
|
* ParamConverter used in the Feed controller to retrieve the right user according to
|
2015-03-28 13:27:45 +00:00
|
|
|
* username & token given in the url.
|
|
|
|
*
|
|
|
|
* @see http://stfalcon.com/en/blog/post/symfony2-custom-paramconverter
|
|
|
|
*/
|
2017-06-13 16:48:10 +00:00
|
|
|
class UsernameFeedTokenConverter implements ParamConverterInterface
|
2015-03-28 13:27:45 +00:00
|
|
|
{
|
|
|
|
private $registry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ManagerRegistry $registry Manager registry
|
|
|
|
*/
|
|
|
|
public function __construct(ManagerRegistry $registry = null)
|
|
|
|
{
|
|
|
|
$this->registry = $registry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* Check, if object supported by our converter
|
|
|
|
*/
|
|
|
|
public function supports(ParamConverter $configuration)
|
|
|
|
{
|
|
|
|
// If there is no manager, this means that only Doctrine DBAL is configured
|
|
|
|
// In this case we can do nothing and just return
|
2018-09-05 12:25:32 +00:00
|
|
|
if (null === $this->registry || !\count($this->registry->getManagers())) {
|
2015-03-28 13:27:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check, if option class was set in configuration
|
|
|
|
if (null === $configuration->getClass()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get actual entity manager for class
|
|
|
|
$em = $this->registry->getManagerForClass($configuration->getClass());
|
|
|
|
|
|
|
|
// Check, if class name is what we need
|
2016-10-01 12:51:54 +00:00
|
|
|
if (null !== $em && 'Wallabag\UserBundle\Entity\User' !== $em->getClassMetadata($configuration->getClass())->getName()) {
|
2015-03-28 13:27:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* Applies converting
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When route attributes are missing
|
|
|
|
* @throws NotFoundHttpException When object not found
|
|
|
|
*/
|
|
|
|
public function apply(Request $request, ParamConverter $configuration)
|
|
|
|
{
|
|
|
|
$username = $request->attributes->get('username');
|
2017-06-13 16:48:10 +00:00
|
|
|
$feedToken = $request->attributes->get('token');
|
2015-03-28 13:27:45 +00:00
|
|
|
|
2016-10-01 12:51:54 +00:00
|
|
|
if (!$request->attributes->has('username') || !$request->attributes->has('token')) {
|
|
|
|
return false;
|
2015-03-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get actual entity manager for class
|
|
|
|
$em = $this->registry->getManagerForClass($configuration->getClass());
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
/** @var UserRepository $userRepository */
|
2015-03-28 13:27:45 +00:00
|
|
|
$userRepository = $em->getRepository($configuration->getClass());
|
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
// Try to find user by its username and config feed_token
|
|
|
|
$user = $userRepository->findOneByUsernameAndFeedtoken($username, $feedToken);
|
2015-03-28 13:27:45 +00:00
|
|
|
|
|
|
|
if (null === $user || !($user instanceof User)) {
|
|
|
|
throw new NotFoundHttpException(sprintf('%s not found.', $configuration->getClass()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map found user to the route's parameter
|
|
|
|
$request->attributes->set($configuration->getName(), $user);
|
2021-08-05 20:33:04 +00:00
|
|
|
|
|
|
|
return true;
|
2015-03-28 13:27:45 +00:00
|
|
|
}
|
|
|
|
}
|