aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-10-29 13:20:01 +0200
committerGitHub <noreply@github.com>2016-10-29 13:20:01 +0200
commit267087d969e68e2f5abcb18f526120d835d9a686 (patch)
tree49d4f5b01f168591bc19180506289da5f076fa87 /src/Wallabag/ApiBundle/Controller/WallabagRestController.php
parenteca4d030bf90cdf6ca0b6a86b18bd8f66a36db08 (diff)
parentbc4564a709ccae949999af77aa456287a72f1591 (diff)
downloadwallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.gz
wallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.zst
wallabag-267087d969e68e2f5abcb18f526120d835d9a686.zip
Merge pull request #2509 from wallabag/explode-api-controller
Exploded WallabagRestController into many controllers
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/WallabagRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php522
1 files changed, 2 insertions, 520 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 9997913d..e927a890 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -3,19 +3,12 @@
3namespace Wallabag\ApiBundle\Controller; 3namespace Wallabag\ApiBundle\Controller;
4 4
5use FOS\RestBundle\Controller\FOSRestController; 5use FOS\RestBundle\Controller\FOSRestController;
6use Hateoas\Configuration\Route;
7use Hateoas\Representation\Factory\PagerfantaFactory;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12use Symfony\Component\Security\Core\Exception\AccessDeniedException; 6use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
14use Wallabag\CoreBundle\Entity\Tag;
15 8
16class WallabagRestController extends FOSRestController 9class WallabagRestController extends FOSRestController
17{ 10{
18 private function validateAuthentication() 11 protected function validateAuthentication()
19 { 12 {
20 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 13 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
21 throw new AccessDeniedException(); 14 throw new AccessDeniedException();
@@ -23,523 +16,12 @@ class WallabagRestController extends FOSRestController
23 } 16 }
24 17
25 /** 18 /**
26 * Check if an entry exist by url.
27 *
28 * @ApiDoc(
29 * parameters={
30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
31 * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"}
32 * }
33 * )
34 *
35 * @return JsonResponse
36 */
37 public function getEntriesExistsAction(Request $request)
38 {
39 $this->validateAuthentication();
40
41 $urls = $request->query->get('urls', []);
42
43 // handle multiple urls first
44 if (!empty($urls)) {
45 $results = [];
46 foreach ($urls as $url) {
47 $res = $this->getDoctrine()
48 ->getRepository('WallabagCoreBundle:Entry')
49 ->findByUrlAndUserId($url, $this->getUser()->getId());
50
51 $results[$url] = false === $res ? false : true;
52 }
53
54 $json = $this->get('serializer')->serialize($results, 'json');
55
56 return (new JsonResponse())->setJson($json);
57 }
58
59 // let's see if it is a simple url?
60 $url = $request->query->get('url', '');
61
62 if (empty($url)) {
63 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
64 }
65
66 $res = $this->getDoctrine()
67 ->getRepository('WallabagCoreBundle:Entry')
68 ->findByUrlAndUserId($url, $this->getUser()->getId());
69
70 $exists = false === $res ? false : true;
71
72 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
73
74 return (new JsonResponse())->setJson($json);
75 }
76
77 /**
78 * Retrieve all entries. It could be filtered by many options.
79 *
80 * @ApiDoc(
81 * parameters={
82 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
83 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
84 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
85 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
86 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
87 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
88 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
89 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
90 * }
91 * )
92 *
93 * @return JsonResponse
94 */
95 public function getEntriesAction(Request $request)
96 {
97 $this->validateAuthentication();
98
99 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
100 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
101 $sort = $request->query->get('sort', 'created');
102 $order = $request->query->get('order', 'desc');
103 $page = (int) $request->query->get('page', 1);
104 $perPage = (int) $request->query->get('perPage', 30);
105 $tags = $request->query->get('tags', '');
106 $since = $request->query->get('since', 0);
107
108 $pager = $this->getDoctrine()
109 ->getRepository('WallabagCoreBundle:Entry')
110 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
111
112 $pager->setCurrentPage($page);
113 $pager->setMaxPerPage($perPage);
114
115 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
116 $paginatedCollection = $pagerfantaFactory->createRepresentation(
117 $pager,
118 new Route(
119 'api_get_entries',
120 [
121 'archive' => $isArchived,
122 'starred' => $isStarred,
123 'sort' => $sort,
124 'order' => $order,
125 'page' => $page,
126 'perPage' => $perPage,
127 'tags' => $tags,
128 'since' => $since,
129 ],
130 UrlGeneratorInterface::ABSOLUTE_URL
131 )
132 );
133
134 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
135
136 return (new JsonResponse())->setJson($json);
137 }
138
139 /**
140 * Retrieve a single entry.
141 *
142 * @ApiDoc(
143 * requirements={
144 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
145 * }
146 * )
147 *
148 * @return JsonResponse
149 */
150 public function getEntryAction(Entry $entry)
151 {
152 $this->validateAuthentication();
153 $this->validateUserAccess($entry->getUser()->getId());
154
155 $json = $this->get('serializer')->serialize($entry, 'json');
156
157 return (new JsonResponse())->setJson($json);
158 }
159
160 /**
161 * Create an entry.
162 *
163 * @ApiDoc(
164 * parameters={
165 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
166 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
167 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
168 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
169 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
170 * }
171 * )
172 *
173 * @return JsonResponse
174 */
175 public function postEntriesAction(Request $request)
176 {
177 $this->validateAuthentication();
178
179 $url = $request->request->get('url');
180 $title = $request->request->get('title');
181 $isArchived = $request->request->get('archive');
182 $isStarred = $request->request->get('starred');
183
184 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
185
186 if (false === $entry) {
187 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
188 new Entry($this->getUser()),
189 $url
190 );
191 }
192
193 if (!is_null($title)) {
194 $entry->setTitle($title);
195 }
196
197 $tags = $request->request->get('tags', '');
198 if (!empty($tags)) {
199 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
200 }
201
202 if (!is_null($isStarred)) {
203 $entry->setStarred((bool) $isStarred);
204 }
205
206 if (!is_null($isArchived)) {
207 $entry->setArchived((bool) $isArchived);
208 }
209
210 $em = $this->getDoctrine()->getManager();
211 $em->persist($entry);
212
213 $em->flush();
214
215 $json = $this->get('serializer')->serialize($entry, 'json');
216
217 return (new JsonResponse())->setJson($json);
218 }
219
220 /**
221 * Change several properties of an entry.
222 *
223 * @ApiDoc(
224 * requirements={
225 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
226 * },
227 * parameters={
228 * {"name"="title", "dataType"="string", "required"=false},
229 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
230 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
231 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
232 * }
233 * )
234 *
235 * @return JsonResponse
236 */
237 public function patchEntriesAction(Entry $entry, Request $request)
238 {
239 $this->validateAuthentication();
240 $this->validateUserAccess($entry->getUser()->getId());
241
242 $title = $request->request->get('title');
243 $isArchived = $request->request->get('archive');
244 $isStarred = $request->request->get('starred');
245
246 if (!is_null($title)) {
247 $entry->setTitle($title);
248 }
249
250 if (!is_null($isArchived)) {
251 $entry->setArchived((bool) $isArchived);
252 }
253
254 if (!is_null($isStarred)) {
255 $entry->setStarred((bool) $isStarred);
256 }
257
258 $tags = $request->request->get('tags', '');
259 if (!empty($tags)) {
260 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
261 }
262
263 $em = $this->getDoctrine()->getManager();
264 $em->flush();
265
266 $json = $this->get('serializer')->serialize($entry, 'json');
267
268 return (new JsonResponse())->setJson($json);
269 }
270
271 /**
272 * Delete **permanently** an entry.
273 *
274 * @ApiDoc(
275 * requirements={
276 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
277 * }
278 * )
279 *
280 * @return JsonResponse
281 */
282 public function deleteEntriesAction(Entry $entry)
283 {
284 $this->validateAuthentication();
285 $this->validateUserAccess($entry->getUser()->getId());
286
287 $em = $this->getDoctrine()->getManager();
288 $em->remove($entry);
289 $em->flush();
290
291 $json = $this->get('serializer')->serialize($entry, 'json');
292
293 return (new JsonResponse())->setJson($json);
294 }
295
296 /**
297 * Retrieve all tags for an entry.
298 *
299 * @ApiDoc(
300 * requirements={
301 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
302 * }
303 * )
304 *
305 * @return JsonResponse
306 */
307 public function getEntriesTagsAction(Entry $entry)
308 {
309 $this->validateAuthentication();
310 $this->validateUserAccess($entry->getUser()->getId());
311
312 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
313
314 return (new JsonResponse())->setJson($json);
315 }
316
317 /**
318 * Add one or more tags to an entry.
319 *
320 * @ApiDoc(
321 * requirements={
322 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
323 * },
324 * parameters={
325 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
326 * }
327 * )
328 *
329 * @return JsonResponse
330 */
331 public function postEntriesTagsAction(Request $request, Entry $entry)
332 {
333 $this->validateAuthentication();
334 $this->validateUserAccess($entry->getUser()->getId());
335
336 $tags = $request->request->get('tags', '');
337 if (!empty($tags)) {
338 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
339 }
340
341 $em = $this->getDoctrine()->getManager();
342 $em->persist($entry);
343 $em->flush();
344
345 $json = $this->get('serializer')->serialize($entry, 'json');
346
347 return (new JsonResponse())->setJson($json);
348 }
349
350 /**
351 * Permanently remove one tag for an entry.
352 *
353 * @ApiDoc(
354 * requirements={
355 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
356 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
357 * }
358 * )
359 *
360 * @return JsonResponse
361 */
362 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
363 {
364 $this->validateAuthentication();
365 $this->validateUserAccess($entry->getUser()->getId());
366
367 $entry->removeTag($tag);
368 $em = $this->getDoctrine()->getManager();
369 $em->persist($entry);
370 $em->flush();
371
372 $json = $this->get('serializer')->serialize($entry, 'json');
373
374 return (new JsonResponse())->setJson($json);
375 }
376
377 /**
378 * Retrieve all tags.
379 *
380 * @ApiDoc()
381 *
382 * @return JsonResponse
383 */
384 public function getTagsAction()
385 {
386 $this->validateAuthentication();
387
388 $tags = $this->getDoctrine()
389 ->getRepository('WallabagCoreBundle:Tag')
390 ->findAllTags($this->getUser()->getId());
391
392 $json = $this->get('serializer')->serialize($tags, 'json');
393
394 return (new JsonResponse())->setJson($json);
395 }
396
397 /**
398 * Permanently remove one tag from **every** entry.
399 *
400 * @ApiDoc(
401 * requirements={
402 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
403 * }
404 * )
405 *
406 * @return JsonResponse
407 */
408 public function deleteTagLabelAction(Request $request)
409 {
410 $this->validateAuthentication();
411 $label = $request->request->get('tag', '');
412
413 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
414
415 if (empty($tag)) {
416 throw $this->createNotFoundException('Tag not found');
417 }
418
419 $this->getDoctrine()
420 ->getRepository('WallabagCoreBundle:Entry')
421 ->removeTag($this->getUser()->getId(), $tag);
422
423 $this->cleanOrphanTag($tag);
424
425 $json = $this->get('serializer')->serialize($tag, 'json');
426
427 return (new JsonResponse())->setJson($json);
428 }
429
430 /**
431 * Permanently remove some tags from **every** entry.
432 *
433 * @ApiDoc(
434 * requirements={
435 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
436 * }
437 * )
438 *
439 * @return JsonResponse
440 */
441 public function deleteTagsLabelAction(Request $request)
442 {
443 $this->validateAuthentication();
444
445 $tagsLabels = $request->request->get('tags', '');
446
447 $tags = [];
448
449 foreach (explode(',', $tagsLabels) as $tagLabel) {
450 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
451
452 if (!empty($tagEntity)) {
453 $tags[] = $tagEntity;
454 }
455 }
456
457 if (empty($tags)) {
458 throw $this->createNotFoundException('Tags not found');
459 }
460
461 $this->getDoctrine()
462 ->getRepository('WallabagCoreBundle:Entry')
463 ->removeTags($this->getUser()->getId(), $tags);
464
465 $this->cleanOrphanTag($tags);
466
467 $json = $this->get('serializer')->serialize($tags, 'json');
468
469 return (new JsonResponse())->setJson($json);
470 }
471
472 /**
473 * Permanently remove one tag from **every** entry.
474 *
475 * @ApiDoc(
476 * requirements={
477 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
478 * }
479 * )
480 *
481 * @return JsonResponse
482 */
483 public function deleteTagAction(Tag $tag)
484 {
485 $this->validateAuthentication();
486
487 $this->getDoctrine()
488 ->getRepository('WallabagCoreBundle:Entry')
489 ->removeTag($this->getUser()->getId(), $tag);
490
491 $this->cleanOrphanTag($tag);
492
493 $json = $this->get('serializer')->serialize($tag, 'json');
494
495 return (new JsonResponse())->setJson($json);
496 }
497
498 /**
499 * Retrieve version number.
500 *
501 * @ApiDoc()
502 *
503 * @return JsonResponse
504 */
505 public function getVersionAction()
506 {
507 $version = $this->container->getParameter('wallabag_core.version');
508
509 $json = $this->get('serializer')->serialize($version, 'json');
510
511 return (new JsonResponse())->setJson($json);
512 }
513
514 /**
515 * Remove orphan tag in case no entries are associated to it.
516 *
517 * @param Tag|array $tags
518 */
519 private function cleanOrphanTag($tags)
520 {
521 if (!is_array($tags)) {
522 $tags = [$tags];
523 }
524
525 $em = $this->getDoctrine()->getManager();
526
527 foreach ($tags as $tag) {
528 if (count($tag->getEntries()) === 0) {
529 $em->remove($tag);
530 }
531 }
532
533 $em->flush();
534 }
535
536 /**
537 * Validate that the first id is equal to the second one. 19 * Validate that the first id is equal to the second one.
538 * If not, throw exception. It means a user try to access information from an other user. 20 * If not, throw exception. It means a user try to access information from an other user.
539 * 21 *
540 * @param int $requestUserId User id from the requested source 22 * @param int $requestUserId User id from the requested source
541 */ 23 */
542 private function validateUserAccess($requestUserId) 24 protected function validateUserAccess($requestUserId)
543 { 25 {
544 $user = $this->get('security.token_storage')->getToken()->getUser(); 26 $user = $this->get('security.token_storage')->getToken()->getUser();
545 if ($requestUserId != $user->getId()) { 27 if ($requestUserId != $user->getId()) {