diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 150 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/UserRestController.php | 23 |
2 files changed, 90 insertions, 83 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 93c8157e..09b73ccb 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -299,65 +299,18 @@ class EntryRestController extends WallabagRestController | |||
299 | $this->validateAuthentication(); | 299 | $this->validateAuthentication(); |
300 | 300 | ||
301 | $url = $request->request->get('url'); | 301 | $url = $request->request->get('url'); |
302 | $title = $request->request->get('title'); | ||
303 | $tags = $request->request->get('tags', []); | ||
304 | $isArchived = $request->request->get('archive'); | ||
305 | $isStarred = $request->request->get('starred'); | ||
306 | $content = $request->request->get('content'); | ||
307 | $language = $request->request->get('language'); | ||
308 | $picture = $request->request->get('preview_picture'); | ||
309 | $publishedAt = $request->request->get('published_at'); | ||
310 | $authors = $request->request->get('authors', ''); | ||
311 | 302 | ||
312 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); | 303 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
304 | $url, | ||
305 | $this->getUser()->getId() | ||
306 | ); | ||
313 | 307 | ||
314 | if (false === $entry) { | 308 | if (false === $entry) { |
315 | $entry = new Entry($this->getUser()); | 309 | $entry = new Entry($this->getUser()); |
316 | } | ||
317 | |||
318 | try { | ||
319 | $this->get('wallabag_core.content_proxy')->updateEntry( | ||
320 | $entry, | ||
321 | $url, | ||
322 | [ | ||
323 | 'title' => $title, | ||
324 | 'html' => $content, | ||
325 | 'url' => $url, | ||
326 | 'language' => $language, | ||
327 | 'date' => $publishedAt, | ||
328 | // faking the preview picture | ||
329 | 'open_graph' => [ | ||
330 | 'og_image' => $picture, | ||
331 | ], | ||
332 | 'authors' => explode(',', $authors), | ||
333 | ] | ||
334 | ); | ||
335 | } catch (\Exception $e) { | ||
336 | $this->get('logger')->error('Error while saving an entry', [ | ||
337 | 'exception' => $e, | ||
338 | 'entry' => $entry, | ||
339 | ]); | ||
340 | $entry->setUrl($url); | 310 | $entry->setUrl($url); |
341 | } | 311 | } |
342 | 312 | ||
343 | if (!empty($tags)) { | 313 | $this->upsertEntry($entry, $request); |
344 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
345 | } | ||
346 | |||
347 | if (!is_null($isStarred)) { | ||
348 | $entry->setStarred((bool) $isStarred); | ||
349 | } | ||
350 | |||
351 | if (!is_null($isArchived)) { | ||
352 | $entry->setArchived((bool) $isArchived); | ||
353 | } | ||
354 | |||
355 | $em = $this->getDoctrine()->getManager(); | ||
356 | $em->persist($entry); | ||
357 | $em->flush(); | ||
358 | |||
359 | // entry saved, dispatch event about it! | ||
360 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
361 | 314 | ||
362 | return $this->sendResponse($entry); | 315 | return $this->sendResponse($entry); |
363 | } | 316 | } |
@@ -374,6 +327,11 @@ class EntryRestController extends WallabagRestController | |||
374 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | 327 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, |
375 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | 328 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, |
376 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | 329 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, |
330 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | ||
331 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | ||
332 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | ||
333 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | ||
334 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | ||
377 | * } | 335 | * } |
378 | * ) | 336 | * ) |
379 | * | 337 | * |
@@ -384,29 +342,7 @@ class EntryRestController extends WallabagRestController | |||
384 | $this->validateAuthentication(); | 342 | $this->validateAuthentication(); |
385 | $this->validateUserAccess($entry->getUser()->getId()); | 343 | $this->validateUserAccess($entry->getUser()->getId()); |
386 | 344 | ||
387 | $title = $request->request->get('title'); | 345 | $this->upsertEntry($entry, $request, true); |
388 | $isArchived = $request->request->get('archive'); | ||
389 | $isStarred = $request->request->get('starred'); | ||
390 | |||
391 | if (!is_null($title)) { | ||
392 | $entry->setTitle($title); | ||
393 | } | ||
394 | |||
395 | if (!is_null($isArchived)) { | ||
396 | $entry->setArchived((bool) $isArchived); | ||
397 | } | ||
398 | |||
399 | if (!is_null($isStarred)) { | ||
400 | $entry->setStarred((bool) $isStarred); | ||
401 | } | ||
402 | |||
403 | $tags = $request->request->get('tags', ''); | ||
404 | if (!empty($tags)) { | ||
405 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
406 | } | ||
407 | |||
408 | $em = $this->getDoctrine()->getManager(); | ||
409 | $em->flush(); | ||
410 | 346 | ||
411 | return $this->sendResponse($entry); | 347 | return $this->sendResponse($entry); |
412 | } | 348 | } |
@@ -673,4 +609,68 @@ class EntryRestController extends WallabagRestController | |||
673 | 609 | ||
674 | return (new JsonResponse())->setJson($json); | 610 | return (new JsonResponse())->setJson($json); |
675 | } | 611 | } |
612 | |||
613 | /** | ||
614 | * Update or Insert a new entry. | ||
615 | * | ||
616 | * @param Entry $entry | ||
617 | * @param Request $request | ||
618 | * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) | ||
619 | */ | ||
620 | private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) | ||
621 | { | ||
622 | $title = $request->request->get('title'); | ||
623 | $tags = $request->request->get('tags', []); | ||
624 | $isArchived = $request->request->get('archive'); | ||
625 | $isStarred = $request->request->get('starred'); | ||
626 | $content = $request->request->get('content'); | ||
627 | $language = $request->request->get('language'); | ||
628 | $picture = $request->request->get('preview_picture'); | ||
629 | $publishedAt = $request->request->get('published_at'); | ||
630 | $authors = $request->request->get('authors', ''); | ||
631 | |||
632 | try { | ||
633 | $this->get('wallabag_core.content_proxy')->updateEntry( | ||
634 | $entry, | ||
635 | $entry->getUrl(), | ||
636 | [ | ||
637 | 'title' => !empty($title) ? $title : $entry->getTitle(), | ||
638 | 'html' => !empty($content) ? $content : $entry->getContent(), | ||
639 | 'url' => $entry->getUrl(), | ||
640 | 'language' => !empty($language) ? $language : $entry->getLanguage(), | ||
641 | 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), | ||
642 | // faking the open graph preview picture | ||
643 | 'open_graph' => [ | ||
644 | 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), | ||
645 | ], | ||
646 | 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), | ||
647 | ], | ||
648 | $disableContentUpdate | ||
649 | ); | ||
650 | } catch (\Exception $e) { | ||
651 | $this->get('logger')->error('Error while saving an entry', [ | ||
652 | 'exception' => $e, | ||
653 | 'entry' => $entry, | ||
654 | ]); | ||
655 | } | ||
656 | |||
657 | if (!is_null($isArchived)) { | ||
658 | $entry->setArchived((bool) $isArchived); | ||
659 | } | ||
660 | |||
661 | if (!is_null($isStarred)) { | ||
662 | $entry->setStarred((bool) $isStarred); | ||
663 | } | ||
664 | |||
665 | if (!empty($tags)) { | ||
666 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | ||
667 | } | ||
668 | |||
669 | $em = $this->getDoctrine()->getManager(); | ||
670 | $em->persist($entry); | ||
671 | $em->flush(); | ||
672 | |||
673 | // entry saved, dispatch event about it! | ||
674 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
675 | } | ||
676 | } | 676 | } |
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php index a1b78e3f..8f675b8d 100644 --- a/src/Wallabag/ApiBundle/Controller/UserRestController.php +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php | |||
@@ -43,16 +43,18 @@ class UserRestController extends WallabagRestController | |||
43 | */ | 43 | */ |
44 | public function putUserAction(Request $request) | 44 | public function putUserAction(Request $request) |
45 | { | 45 | { |
46 | if (!$this->container->getParameter('fosuser_registration')) { | 46 | if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) { |
47 | $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json'); | 47 | $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json'); |
48 | 48 | ||
49 | return (new JsonResponse())->setJson($json)->setStatusCode(403); | 49 | return (new JsonResponse()) |
50 | ->setJson($json) | ||
51 | ->setStatusCode(JsonResponse::HTTP_FORBIDDEN); | ||
50 | } | 52 | } |
51 | 53 | ||
52 | $userManager = $this->get('fos_user.user_manager'); | 54 | $userManager = $this->get('fos_user.user_manager'); |
53 | $user = $userManager->createUser(); | 55 | $user = $userManager->createUser(); |
54 | // enable created user by default | 56 | // user will be disabled BY DEFAULT to avoid spamming account to be enabled |
55 | $user->setEnabled(true); | 57 | $user->setEnabled(false); |
56 | 58 | ||
57 | $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ | 59 | $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ |
58 | 'csrf_protection' => false, | 60 | 'csrf_protection' => false, |
@@ -90,7 +92,9 @@ class UserRestController extends WallabagRestController | |||
90 | 92 | ||
91 | $json = $this->get('serializer')->serialize(['error' => $errors], 'json'); | 93 | $json = $this->get('serializer')->serialize(['error' => $errors], 'json'); |
92 | 94 | ||
93 | return (new JsonResponse())->setJson($json)->setStatusCode(400); | 95 | return (new JsonResponse()) |
96 | ->setJson($json) | ||
97 | ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); | ||
94 | } | 98 | } |
95 | 99 | ||
96 | $userManager->updateUser($user); | 100 | $userManager->updateUser($user); |
@@ -99,17 +103,18 @@ class UserRestController extends WallabagRestController | |||
99 | $event = new UserEvent($user, $request); | 103 | $event = new UserEvent($user, $request); |
100 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | 104 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); |
101 | 105 | ||
102 | return $this->sendUser($user); | 106 | return $this->sendUser($user, JsonResponse::HTTP_CREATED); |
103 | } | 107 | } |
104 | 108 | ||
105 | /** | 109 | /** |
106 | * Send user response. | 110 | * Send user response. |
107 | * | 111 | * |
108 | * @param User $user | 112 | * @param User $user |
113 | * @param int $status HTTP Status code to send | ||
109 | * | 114 | * |
110 | * @return JsonResponse | 115 | * @return JsonResponse |
111 | */ | 116 | */ |
112 | private function sendUser(User $user) | 117 | private function sendUser(User $user, $status = JsonResponse::HTTP_OK) |
113 | { | 118 | { |
114 | $json = $this->get('serializer')->serialize( | 119 | $json = $this->get('serializer')->serialize( |
115 | $user, | 120 | $user, |
@@ -117,7 +122,9 @@ class UserRestController extends WallabagRestController | |||
117 | SerializationContext::create()->setGroups(['user_api']) | 122 | SerializationContext::create()->setGroups(['user_api']) |
118 | ); | 123 | ); |
119 | 124 | ||
120 | return (new JsonResponse())->setJson($json); | 125 | return (new JsonResponse()) |
126 | ->setJson($json) | ||
127 | ->setStatusCode($status); | ||
121 | } | 128 | } |
122 | 129 | ||
123 | /** | 130 | /** |