X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FController%2FWallabagRestController.php;h=81bfbe12a58bf40a0facf908f6f711a36a6b834a;hb=092ca70725b0263390e45c46f93828c613eca3f0;hp=27d11da5ec158c931f614186d51777a75f73fa3b;hpb=f59f45d74093e92656f9717c8c5f4e37c56d2173;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index 27d11da5..81bfbe12 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php @@ -5,13 +5,39 @@ namespace Wallabag\CoreBundle\Controller; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpFoundation\Response; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Service\Extractor; +use Symfony\Component\Security\Core\Exception\AccessDeniedException; 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. * @@ -20,7 +46,7 @@ class WallabagRestController extends Controller * {"name"="username", "dataType"="string", "required"=true, "description"="username"} * } * ) - * @return string + * @return array */ public function getSaltAction($username) { @@ -33,7 +59,7 @@ class WallabagRestController extends Controller throw $this->createNotFoundException(); } - return $user->getSalt(); + return array($user->getSalt() ?: null); } /** * Retrieve all entries. It could be filtered by many options. @@ -42,7 +68,6 @@ class WallabagRestController extends Controller * parameters={ * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."}, * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."}, - * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false, default '0'", "description"="filter by deleted status."}, * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, @@ -56,7 +81,6 @@ class WallabagRestController extends Controller { $isArchived = $request->query->get('archive'); $isStarred = $request->query->get('star'); - $isDeleted = $request->query->get('delete', 0); $sort = $request->query->get('sort', 'created'); $order = $request->query->get('order', 'desc'); $page = $request->query->get('page', 1); @@ -66,13 +90,15 @@ class WallabagRestController extends Controller $entries = $this ->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') - ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $isDeleted, $sort, $order); + ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); if (!($entries)) { throw $this->createNotFoundException(); } - return $entries; + $json = $this->get('serializer')->serialize($entries, 'json'); + + return new Response($json, 200, array('application/json')); } /** @@ -87,7 +113,13 @@ class WallabagRestController extends Controller */ public function getEntryAction(Entry $entry) { - return $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')); } /** @@ -104,7 +136,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); @@ -112,11 +143,16 @@ class WallabagRestController extends Controller $entry->setUrl($url); $entry->setTitle($request->request->get('title') ?: $content->getTitle()); $entry->setContent($content->getBody()); + + $this->assignTagsToEntry($entry, $request->request->get('tags', array())); + $em = $this->getDoctrine()->getManager(); $em->persist($entry); $em->flush(); - return $entry; + $json = $this->get('serializer')->serialize($entry, 'json'); + + return new Response($json, 200, array('application/json')); } /** @@ -131,17 +167,18 @@ class WallabagRestController extends Controller * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."}, * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."}, - * {"name"="delete", "dataType"="boolean", "required"=false, "format"="true or false", "description"="flag as deleted. Default false. In case that you don't want to *really* remove it.."}, - * } + * } * ) * @return Entry */ 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"); - $isDeleted = $request->request->get("delete"); $isStarred = $request->request->get("star"); if (!is_null($title)) { @@ -152,14 +189,12 @@ class WallabagRestController extends Controller $entry->setArchived($isArchived); } - if (!is_null($isDeleted)) { - $entry->setDeleted($isDeleted); - } - if (!is_null($isStarred)) { $entry->setStarred($isStarred); } + $this->assignTagsToEntry($entry, $request->request->get('tags', array())); + $em = $this->getDoctrine()->getManager(); $em->flush(); @@ -178,15 +213,17 @@ class WallabagRestController extends Controller */ public function deleteEntriesAction(Entry $entry) { - if ($entry->isDeleted()) { - throw new NotFoundHttpException('This entry is already deleted'); + if ($entry->getUser()->getId() != $this->getUser()->getId()) { + throw $this->createAccessDeniedException(); } $em = $this->getDoctrine()->getManager(); - $entry->setDeleted(1); + $em->remove($entry); $em->flush(); - return $entry; + $json = $this->get('serializer')->serialize($entry, 'json'); + + return new Response($json, 200, array('application/json')); } /** @@ -200,6 +237,15 @@ class WallabagRestController extends Controller */ public function getEntriesTagsAction(Entry $entry) { + var_dump($entry->getUser()->getId()); + var_dump($this->getUser()->getId()); + 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')); } /** @@ -214,8 +260,21 @@ 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(); + } + + $this->assignTagsToEntry($entry, $request->request->get('tags', array())); + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $json = $this->get('serializer')->serialize($entry, 'json'); + + return new Response($json, 200, array('application/json')); } /** @@ -230,29 +289,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 Response($json, 200, array('application/json')); } /** * Retrieve all tags * - * @ApiDoc( - * ) + * @ApiDoc() */ public function getTagsAction() { - } + $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json'); - /** - * Retrieve a single tag - * - * @ApiDoc( - * requirements={ - * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"} - * } - * ) - */ - public function getTagAction(Tag $tag) - { + return new Response($json, 200, array('application/json')); } /** @@ -266,5 +326,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 Response($json, 200, array('application/json')); } }