3 namespace Wallabag\ApiBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Hateoas\Configuration\Route
;
7 use Hateoas\Representation\Factory\PagerfantaFactory
;
8 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Symfony\Component\HttpFoundation\Response
;
11 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
12 use Symfony\Component\Security\Core\Exception\AccessDeniedException
;
13 use Wallabag\CoreBundle\Entity\Entry
;
14 use Wallabag\CoreBundle\Entity\Tag
;
16 class WallabagRestController
extends FOSRestController
18 private function validateAuthentication()
20 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
21 throw new AccessDeniedException();
26 * Retrieve all entries. It could be filtered by many options.
30 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
31 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
32 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
33 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
34 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
35 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
36 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
37 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
43 public function getEntriesAction(Request
$request)
45 $this->validateAuthentication();
47 $isArchived = (null === $request->query
->get('archive')) ? null : (bool) $request->query
->get('archive');
48 $isStarred = (null === $request->query
->get('starred')) ? null : (bool) $request->query
->get('starred');
49 $sort = $request->query
->get('sort', 'created');
50 $order = $request->query
->get('order', 'desc');
51 $page = (int) $request->query
->get('page', 1);
52 $perPage = (int) $request->query
->get('perPage', 30);
53 $since = $request->query
->get('since', 0);
55 $pager = $this->getDoctrine()
56 ->getRepository('WallabagCoreBundle:Entry')
57 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since);
59 $pager->setCurrentPage($page);
60 $pager->setMaxPerPage($perPage);
62 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
63 $paginatedCollection = $pagerfantaFactory->createRepresentation(
65 new Route('api_get_entries', [], UrlGeneratorInterface
::ABSOLUTE_URL
)
68 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
70 return $this->renderJsonResponse($json);
74 * Retrieve a single entry.
78 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
84 public function getEntryAction(Entry
$entry)
86 $this->validateAuthentication();
87 $this->validateUserAccess($entry->getUser()->getId());
89 $json = $this->get('serializer')->serialize($entry, 'json');
91 return $this->renderJsonResponse($json);
99 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
100 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
101 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
102 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
103 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
109 public function postEntriesAction(Request
$request)
111 $this->validateAuthentication();
113 $url = $request->request
->get('url');
114 $title = $request->request
->get('title');
115 $isArchived = $request->request
->get('archive');
116 $isStarred = $request->request
->get('starred');
118 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
120 if (false === $entry) {
121 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
122 new Entry($this->getUser()),
127 if (!is_null($title)) {
128 $entry->setTitle($title);
131 $tags = $request->request
->get('tags', '');
133 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
136 if (!is_null($isStarred)) {
137 $entry->setStarred((bool) $isStarred);
140 if (!is_null($isArchived)) {
141 $entry->setArchived((bool) $isArchived);
144 $em = $this->getDoctrine()->getManager();
145 $em->persist($entry);
149 $json = $this->get('serializer')->serialize($entry, 'json');
151 return $this->renderJsonResponse($json);
155 * Change several properties of an entry.
159 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
162 * {"name"="title", "dataType"="string", "required"=false},
163 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
164 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
165 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
171 public function patchEntriesAction(Entry
$entry, Request
$request)
173 $this->validateAuthentication();
174 $this->validateUserAccess($entry->getUser()->getId());
176 $title = $request->request
->get('title');
177 $isArchived = $request->request
->get('archive');
178 $isStarred = $request->request
->get('starred');
180 if (!is_null($title)) {
181 $entry->setTitle($title);
184 if (!is_null($isArchived)) {
185 $entry->setArchived((bool) $isArchived);
188 if (!is_null($isStarred)) {
189 $entry->setStarred((bool) $isStarred);
192 $tags = $request->request
->get('tags', '');
194 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
197 $em = $this->getDoctrine()->getManager();
200 $json = $this->get('serializer')->serialize($entry, 'json');
202 return $this->renderJsonResponse($json);
206 * Delete **permanently** an entry.
210 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
216 public function deleteEntriesAction(Entry
$entry)
218 $this->validateAuthentication();
219 $this->validateUserAccess($entry->getUser()->getId());
221 $em = $this->getDoctrine()->getManager();
225 $json = $this->get('serializer')->serialize($entry, 'json');
227 return $this->renderJsonResponse($json);
231 * Retrieve all tags for an entry.
235 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
241 public function getEntriesTagsAction(Entry
$entry)
243 $this->validateAuthentication();
244 $this->validateUserAccess($entry->getUser()->getId());
246 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
248 return $this->renderJsonResponse($json);
252 * Add one or more tags to an entry.
256 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
259 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
265 public function postEntriesTagsAction(Request
$request, Entry
$entry)
267 $this->validateAuthentication();
268 $this->validateUserAccess($entry->getUser()->getId());
270 $tags = $request->request
->get('tags', '');
272 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
275 $em = $this->getDoctrine()->getManager();
276 $em->persist($entry);
279 $json = $this->get('serializer')->serialize($entry, 'json');
281 return $this->renderJsonResponse($json);
285 * Permanently remove one tag for an entry.
289 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
290 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
296 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
298 $this->validateAuthentication();
299 $this->validateUserAccess($entry->getUser()->getId());
301 $entry->removeTag($tag);
302 $em = $this->getDoctrine()->getManager();
303 $em->persist($entry);
306 $json = $this->get('serializer')->serialize($entry, 'json');
308 return $this->renderJsonResponse($json);
318 public function getTagsAction()
320 $this->validateAuthentication();
322 $tags = $this->getDoctrine()
323 ->getRepository('WallabagCoreBundle:Tag')
324 ->findAllTags($this->getUser()->getId());
326 $json = $this->get('serializer')->serialize($tags, 'json');
328 return $this->renderJsonResponse($json);
332 * Permanently remove one tag from **every** entry.
336 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
342 public function deleteTagAction(Tag
$tag)
344 $this->validateAuthentication();
347 ->getRepository('WallabagCoreBundle:Entry')
348 ->removeTag($this->getUser()->getId(), $tag);
350 $json = $this->get('serializer')->serialize($tag, 'json');
352 return $this->renderJsonResponse($json);
355 * Retrieve version number.
361 public function getVersionAction()
363 $version = $this->container
->getParameter('wallabag_core.version');
365 $json = $this->get('serializer')->serialize($version, 'json');
367 return $this->renderJsonResponse($json);
371 * Validate that the first id is equal to the second one.
372 * If not, throw exception. It means a user try to access information from an other user.
374 * @param int $requestUserId User id from the requested source
376 private function validateUserAccess($requestUserId)
378 $user = $this->get('security.token_storage')->getToken()->getUser();
379 if ($requestUserId != $user->getId()) {
380 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
385 * Send a JSON Response.
386 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
388 * @param string $json
392 private function renderJsonResponse($json)
394 return new Response($json, 200, ['application/json']);