]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/WallabagRestController.php
fix tests for GET /entries/tags
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / WallabagRestController.php
index b895b67cd913b881bbd765f06918d54972576c64..e25ac6db8a545fd69bd96b7a0c46dc6e9319378c 100644 (file)
@@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Controller;
 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Response;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
@@ -12,14 +13,35 @@ use Wallabag\CoreBundle\Service\Extractor;
 
 class WallabagRestController extends Controller
 {
+    /**
+     * @param Entry  $entry
+     * @param string $tags
+     */
+    private function assignTagsToEntry(Entry $entry, $tags)
+    {
+        foreach (explode(',', $tags) as $label) {
+            $label = trim($label);
+            $tagEntity = $this
+                ->getDoctrine()
+                ->getRepository('WallabagCoreBundle:Tag')
+                ->findOneByLabel($label);
+
+            if (is_null($tagEntity)) {
+                $tagEntity = new Tag($this->getUser());
+                $tagEntity->setLabel($label);
+            }
+
+            // only add the tag on the entry if the relation doesn't exist
+            if (!$entry->getTags()->contains($tagEntity)) {
+                $entry->addTag($tagEntity);
+            }
+        }
+    }
+
     /**
      * Retrieve salt for a giver user.
      *
-     * @ApiDoc(
-     *       parameters={
-     *          {"name"="username", "dataType"="string", "required"=true, "description"="username"}
-     *       }
-     * )
+     * @ApiDoc()
      * @return array
      */
     public function getSaltAction($username)
@@ -72,7 +94,7 @@ class WallabagRestController extends Controller
 
         $json = $this->get('serializer')->serialize($entries, 'json');
 
-        return new Response($json, 200, array('application/json'));
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -87,9 +109,13 @@ class WallabagRestController extends Controller
      */
     public function getEntryAction(Entry $entry)
     {
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return new Response($json, 200, array('application/json'));
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -106,7 +132,6 @@ class WallabagRestController extends Controller
      */
     public function postEntriesAction(Request $request)
     {
-        //TODO gĂ©rer si on passe les tags
         $url = $request->request->get('url');
 
         $content = Extractor::extract($url);
@@ -114,13 +139,19 @@ class WallabagRestController extends Controller
         $entry->setUrl($url);
         $entry->setTitle($request->request->get('title') ?: $content->getTitle());
         $entry->setContent($content->getBody());
+
+        $tags = $request->request->get('tags', '');
+        if (!empty($tags)) {
+            $this->assignTagsToEntry($entry, $tags);
+        }
+
         $em = $this->getDoctrine()->getManager();
         $em->persist($entry);
         $em->flush();
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return new Response($json, 200, array('application/json'));
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -141,8 +172,11 @@ class WallabagRestController extends Controller
      */
     public function patchEntriesAction(Entry $entry, Request $request)
     {
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
         $title      = $request->request->get("title");
-        $tags       = $request->request->get("tags", array());
         $isArchived = $request->request->get("archive");
         $isStarred  = $request->request->get("star");
 
@@ -158,10 +192,17 @@ class WallabagRestController extends Controller
             $entry->setStarred($isStarred);
         }
 
+        $tags = $request->request->get('tags', '');
+        if (!empty($tags)) {
+            $this->assignTagsToEntry($entry, $tags);
+        }
+
         $em = $this->getDoctrine()->getManager();
         $em->flush();
 
-        return $entry;
+        $json = $this->get('serializer')->serialize($entry, 'json');
+
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -176,13 +217,17 @@ class WallabagRestController extends Controller
      */
     public function deleteEntriesAction(Entry $entry)
     {
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
         $em = $this->getDoctrine()->getManager();
         $em->remove($entry);
         $em->flush();
 
         $json = $this->get('serializer')->serialize($entry, 'json');
 
-        return new Response($json, 200, array('application/json'));
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -196,7 +241,11 @@ class WallabagRestController extends Controller
      */
     public function getEntriesTagsAction(Entry $entry)
     {
-        $json = $this->get('serializer')->serialize($entry, 'json');
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
+        $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
 
         return new Response($json, 200, array('application/json'));
     }
@@ -213,8 +262,24 @@ class WallabagRestController extends Controller
      *       }
      * )
      */
-    public function postEntriesTagsAction(Entry $entry)
+    public function postEntriesTagsAction(Request $request, Entry $entry)
     {
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
+        $tags = $request->request->get('tags', '');
+        if (!empty($tags)) {
+            $this->assignTagsToEntry($entry, $tags);
+        }
+
+        $em = $this->getDoctrine()->getManager();
+        $em->persist($entry);
+        $em->flush();
+
+        $json = $this->get('serializer')->serialize($entry, 'json');
+
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -229,17 +294,30 @@ class WallabagRestController extends Controller
      */
     public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
     {
+        if ($entry->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
+        $entry->removeTag($tag);
+        $em = $this->getDoctrine()->getManager();
+        $em->persist($entry);
+        $em->flush();
+
+        $json = $this->get('serializer')->serialize($entry, 'json');
+
+        return new JsonResponse($json, 200);
     }
 
     /**
      * Retrieve all tags
      *
-     * @ApiDoc(
-     *          {"name"="user", "dataType"="integer", "requirement"="\w+", "description"="The user ID"}
-     * )
+     * @ApiDoc()
      */
-    public function getTagsUserAction()
+    public function getTagsAction()
     {
+        $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
+
+        return new JsonResponse($json, 200);
     }
 
     /**
@@ -253,5 +331,16 @@ class WallabagRestController extends Controller
      */
     public function deleteTagAction(Tag $tag)
     {
+        if ($tag->getUser()->getId() != $this->getUser()->getId()) {
+            throw $this->createAccessDeniedException();
+        }
+
+        $em = $this->getDoctrine()->getManager();
+        $em->remove($tag);
+        $em->flush();
+
+        $json = $this->get('serializer')->serialize($tag, 'json');
+
+        return new JsonResponse($json, 200);
     }
 }