3 namespace Wallabag\ApiBundle\Controller
;
5 use FOS\RestBundle\Controller\AbstractFOSRestController
;
6 use JMS\Serializer\SerializationContext
;
7 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
8 use Symfony\Component\HttpFoundation\JsonResponse
;
9 use Symfony\Component\Security\Core\Exception\AccessDeniedException
;
11 class WallabagRestController
extends AbstractFOSRestController
14 * Retrieve version number.
18 * @deprecated Should use info endpoint instead
20 * @return JsonResponse
22 public function getVersionAction()
24 $version = $this->container
->getParameter('wallabag_core.version');
25 $json = $this->get('jms_serializer')->serialize($version, 'json');
27 return (new JsonResponse())->setJson($json);
31 * Retrieve information about the wallabag instance.
35 * @return JsonResponse
37 public function getInfoAction()
40 'appname' => 'wallabag',
41 'version' => $this->container
->getParameter('wallabag_core.version'),
42 'allowed_registration' => $this->container
->getParameter('wallabag_user.registration_enabled'),
45 return (new JsonResponse())->setJson($this->get('jms_serializer')->serialize($info, 'json'));
48 protected function validateAuthentication()
50 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
51 throw new AccessDeniedException();
56 * Validate that the first id is equal to the second one.
57 * If not, throw exception. It means a user try to access information from an other user.
59 * @param int $requestUserId User id from the requested source
61 protected function validateUserAccess($requestUserId)
63 $user = $this->get('security.token_storage')->getToken()->getUser();
64 if ($requestUserId !== $user->getId()) {
65 throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId());
70 * Shortcut to send data serialized in json.
74 * @return JsonResponse
76 protected function sendResponse($data)
78 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
79 $context = new SerializationContext();
80 $context->setSerializeNull(true);
82 $json = $this->get('jms_serializer')->serialize($data, 'json', $context);
84 return (new JsonResponse())->setJson($json);