namespace Wallabag\ApiBundle\Controller;
+use FOS\RestBundle\Controller\FOSRestController;
+use Hateoas\Configuration\Route;
+use Hateoas\Representation\Factory\PagerfantaFactory;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
-use Hateoas\Configuration\Route;
-use Hateoas\Representation\Factory\PagerfantaFactory;
-class WallabagRestController extends Controller
+class WallabagRestController extends FOSRestController
{
/**
* @param Entry $entry
->findOneByLabel($label);
if (is_null($tagEntity)) {
- $tagEntity = new Tag($this->getUser());
+ $tagEntity = new Tag();
$tagEntity->setLabel($label);
}
}
}
- /**
- * Retrieve salt for a giver user.
- *
- * @ApiDoc(
- * parameters={
- * {"name"="username", "dataType"="string", "required"=true, "description"="username"}
- * }
- * )
- *
- * @return array
- */
- public function getSaltAction($username)
+ private function validateAuthentication()
{
- $user = $this
- ->getDoctrine()
- ->getRepository('WallabagCoreBundle:User')
- ->findOneByUsername($username);
-
- if (is_null($user)) {
- throw $this->createNotFoundException();
+ if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
+ throw new AccessDeniedException();
}
-
- return array($user->getSalt() ?: null);
}
/**
* }
* )
*
- * @return Entry
+ * @return Response
*/
public function getEntriesAction(Request $request)
{
+ $this->validateAuthentication();
+
$isArchived = $request->query->get('archive');
$isStarred = $request->query->get('star');
$sort = $request->query->get('sort', 'created');
$perPage = (int) $request->query->get('perPage', 30);
$tags = $request->query->get('tags', []);
- $pager = $this
- ->getDoctrine()
+ $pager = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Entry')
->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
$pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
$paginatedCollection = $pagerfantaFactory->createRepresentation(
$pager,
- new Route('api_get_entries', [], $absolute = true)
+ new Route('api_get_entries', [], UrlGeneratorInterface::ABSOLUTE_URL)
);
$json = $this->get('serializer')->serialize($paginatedCollection, 'json');
* }
* )
*
- * @return Entry
+ * @return Response
*/
public function getEntryAction(Entry $entry)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$json = $this->get('serializer')->serialize($entry, 'json');
* }
* )
*
- * @return Entry
+ * @return Response
*/
public function postEntriesAction(Request $request)
{
- $url = $request->request->get('url');
+ $this->validateAuthentication();
- $content = $this->get('wallabag_core.graby')->fetchContent($url);
+ $url = $request->request->get('url');
- $entry = new Entry($this->getUser());
- $entry->setUrl($url);
- $entry->setTitle($request->request->get('title') ?: $content['title']);
- $entry->setContent($content['html']);
- $entry->setMimetype($content['content_type']);
- if (isset($content['open_graph']['og_image'])) {
- $entry->setPreviewPicture($content['open_graph']['og_image']);
- }
+ $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
+ new Entry($this->getUser()),
+ $url
+ );
$tags = $request->request->get('tags', '');
if (!empty($tags)) {
* }
* )
*
- * @return Entry
+ * @return Response
*/
public function patchEntriesAction(Entry $entry, Request $request)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$title = $request->request->get('title');
- $isArchived = $request->request->get('is_archived');
- $isStarred = $request->request->get('is_starred');
+ $isArchived = $request->request->get('archive');
+ $isStarred = $request->request->get('star');
if (!is_null($title)) {
$entry->setTitle($title);
* }
* )
*
- * @return Entry
+ * @return Response
*/
public function deleteEntriesAction(Entry $entry)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$em = $this->getDoctrine()->getManager();
$em->remove($entry);
* {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
* }
* )
+ *
+ * @return Response
*/
public function getEntriesTagsAction(Entry $entry)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$json = $this->get('serializer')->serialize($entry->getTags(), 'json');
* {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
* }
* )
+ *
+ * @return Response
*/
public function postEntriesTagsAction(Request $request, Entry $entry)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$tags = $request->request->get('tags', '');
if (!empty($tags)) {
* {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
* }
* )
+ *
+ * @return Response
*/
public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
{
- $this->validateUserAccess($entry->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
+ $this->validateUserAccess($entry->getUser()->getId());
$entry->removeTag($tag);
$em = $this->getDoctrine()->getManager();
* Retrieve all tags.
*
* @ApiDoc()
+ *
+ * @return Response
*/
public function getTagsAction()
{
- $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
+ $this->validateAuthentication();
+
+ $tags = $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Tag')
+ ->findAllTags($this->getUser()->getId());
+
+ $json = $this->get('serializer')->serialize($tags, 'json');
return $this->renderJsonResponse($json);
}
* {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
* }
* )
+ *
+ * @return Response
*/
public function deleteTagAction(Tag $tag)
{
- $this->validateUserAccess($tag->getUser()->getId(), $this->getUser()->getId());
+ $this->validateAuthentication();
- $em = $this->getDoctrine()->getManager();
- $em->remove($tag);
- $em->flush();
+ $this->getDoctrine()
+ ->getRepository('WallabagCoreBundle:Entry')
+ ->removeTag($this->getUser()->getId(), $tag);
$json = $this->get('serializer')->serialize($tag, 'json');
* If not, throw exception. It means a user try to access information from an other user.
*
* @param int $requestUserId User id from the requested source
- * @param int $currentUserId User id from the retrieved source
*/
- private function validateUserAccess($requestUserId, $currentUserId)
+ private function validateUserAccess($requestUserId)
{
- if ($requestUserId != $currentUserId) {
- throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$currentUserId);
+ $user = $this->get('security.token_storage')->getToken()->getUser();
+ if ($requestUserId != $user->getId()) {
+ throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
}
}