2015-01-29 15:56:58 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Controller\Api;
|
2015-01-29 15:56:58 +00:00
|
|
|
|
2023-02-17 09:46:07 +00:00
|
|
|
use Craue\ConfigBundle\Util\Config;
|
2022-12-19 09:37:22 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-05-29 10:00:23 +00:00
|
|
|
use FOS\RestBundle\Controller\AbstractFOSRestController;
|
2018-10-24 18:11:45 +00:00
|
|
|
use JMS\Serializer\SerializationContext;
|
2022-08-28 00:01:46 +00:00
|
|
|
use JMS\Serializer\SerializerInterface;
|
2022-11-26 15:48:30 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model;
|
2022-11-06 12:00:41 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
2022-12-23 13:40:42 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2016-11-03 16:29:16 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2022-11-23 11:44:55 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
2016-11-03 17:01:25 +00:00
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
2022-12-19 09:37:22 +00:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\Api\ApplicationInfo;
|
|
|
|
use Wallabag\Entity\User;
|
2015-01-29 15:56:58 +00:00
|
|
|
|
2019-05-29 10:00:23 +00:00
|
|
|
class WallabagRestController extends AbstractFOSRestController
|
2015-01-29 15:56:58 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
protected EntityManagerInterface $entityManager;
|
|
|
|
protected SerializerInterface $serializer;
|
|
|
|
protected AuthorizationCheckerInterface $authorizationChecker;
|
|
|
|
protected TokenStorageInterface $tokenStorage;
|
|
|
|
protected TranslatorInterface $translator;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, SerializerInterface $serializer, AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->serializer = $serializer;
|
|
|
|
$this->authorizationChecker = $authorizationChecker;
|
|
|
|
$this->tokenStorage = $tokenStorage;
|
|
|
|
$this->translator = $translator;
|
|
|
|
}
|
|
|
|
|
2016-03-07 14:00:03 +00:00
|
|
|
/**
|
2016-03-08 08:22:25 +00:00
|
|
|
* Retrieve version number.
|
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
2022-11-26 15:48:30 +00:00
|
|
|
* tags={"Information"},
|
2022-11-06 12:00:41 +00:00
|
|
|
* summary="Retrieve version number.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
2022-11-26 15:48:30 +00:00
|
|
|
* description="Returned when successful",
|
|
|
|
* @OA\JsonContent(
|
|
|
|
* description="Version number of the application.",
|
|
|
|
* type="string",
|
|
|
|
* example="2.5.2",
|
|
|
|
* )
|
2022-11-06 12:00:41 +00:00
|
|
|
* )
|
|
|
|
* )
|
2016-03-07 14:00:03 +00:00
|
|
|
*
|
2019-01-15 09:17:11 +00:00
|
|
|
* @deprecated Should use info endpoint instead
|
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/version.{_format}", methods={"GET"}, name="api_get_version", defaults={"_format": "json"})
|
|
|
|
*
|
2016-09-08 10:03:09 +00:00
|
|
|
* @return JsonResponse
|
2016-03-07 14:00:03 +00:00
|
|
|
*/
|
|
|
|
public function getVersionAction()
|
|
|
|
{
|
2024-02-19 23:47:53 +00:00
|
|
|
$version = $this->getParameter('wallabag.version');
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($version, 'json');
|
2016-11-03 16:29:16 +00:00
|
|
|
|
2016-09-08 10:03:09 +00:00
|
|
|
return (new JsonResponse())->setJson($json);
|
2016-03-07 14:00:03 +00:00
|
|
|
}
|
2015-03-29 08:53:10 +00:00
|
|
|
|
2019-01-15 09:17:11 +00:00
|
|
|
/**
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
2022-11-26 15:48:30 +00:00
|
|
|
* tags={"Information"},
|
|
|
|
* summary="Retrieve information about the running wallabag application.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
2022-11-26 15:48:30 +00:00
|
|
|
* description="Returned when successful",
|
|
|
|
* @Model(type=ApplicationInfo::class),
|
2022-11-06 12:00:41 +00:00
|
|
|
* )
|
|
|
|
* )
|
2019-01-15 09:17:11 +00:00
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/info.{_format}", methods={"GET"}, name="api_get_info", defaults={"_format": "json"})
|
|
|
|
*
|
2019-01-15 09:17:11 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2023-02-17 09:46:07 +00:00
|
|
|
public function getInfoAction(Config $craueConfig)
|
2019-01-15 09:17:11 +00:00
|
|
|
{
|
2022-11-26 15:48:30 +00:00
|
|
|
$info = new ApplicationInfo(
|
2024-02-19 23:47:53 +00:00
|
|
|
$this->getParameter('wallabag.version'),
|
2022-11-26 15:48:30 +00:00
|
|
|
$this->getParameter('fosuser_registration') && $craueConfig->get('api_user_registration'),
|
|
|
|
);
|
2019-01-15 09:17:11 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
return (new JsonResponse())->setJson($this->serializer->serialize($info, 'json'));
|
2019-01-15 09:17:11 +00:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:46:30 +00:00
|
|
|
protected function validateAuthentication()
|
2016-10-07 21:31:53 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
if (false === $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
|
2015-09-29 12:57:46 +00:00
|
|
|
throw new AccessDeniedException();
|
2016-10-07 21:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 08:53:10 +00:00
|
|
|
/**
|
|
|
|
* Validate that the first id is equal to the second one.
|
2015-05-30 11:52:26 +00:00
|
|
|
* If not, throw exception. It means a user try to access information from an other user.
|
2015-03-29 08:53:10 +00:00
|
|
|
*
|
2015-05-30 11:52:26 +00:00
|
|
|
* @param int $requestUserId User id from the requested source
|
2015-03-29 08:53:10 +00:00
|
|
|
*/
|
2016-10-28 12:46:30 +00:00
|
|
|
protected function validateUserAccess($requestUserId)
|
2015-03-29 08:53:10 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
$user = $this->tokenStorage->getToken()->getUser();
|
2022-11-23 14:51:33 +00:00
|
|
|
\assert($user instanceof User);
|
2022-12-19 09:37:22 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
if ($requestUserId !== $user->getId()) {
|
|
|
|
throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId());
|
2015-03-29 08:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 18:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut to send data serialized in json.
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
protected function sendResponse($data)
|
|
|
|
{
|
|
|
|
// https://github.com/schmittjoh/JMSSerializerBundle/issues/293
|
|
|
|
$context = new SerializationContext();
|
|
|
|
$context->setSerializeNull(true);
|
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize($data, 'json', $context);
|
2018-10-24 18:11:45 +00:00
|
|
|
|
|
|
|
return (new JsonResponse())->setJson($json);
|
|
|
|
}
|
2024-01-01 18:51:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
protected function getUser()
|
|
|
|
{
|
|
|
|
$user = parent::getUser();
|
|
|
|
\assert(null === $user || $user instanceof User);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
2015-01-31 18:09:34 +00:00
|
|
|
}
|