diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/DeveloperController.php | 101 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 52 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Entity/AccessToken.php | 2 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Entity/Client.php | 10 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Entity/RefreshToken.php | 2 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Form/Type/ClientType.php | 46 |
6 files changed, 209 insertions, 4 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/DeveloperController.php b/src/Wallabag/ApiBundle/Controller/DeveloperController.php new file mode 100644 index 00000000..5a36a260 --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/DeveloperController.php | |||
@@ -0,0 +1,101 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ApiBundle\Controller; | ||
4 | |||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
8 | use Wallabag\ApiBundle\Entity\Client; | ||
9 | use Wallabag\ApiBundle\Form\Type\ClientType; | ||
10 | |||
11 | class DeveloperController extends Controller | ||
12 | { | ||
13 | /** | ||
14 | * List all clients and link to create a new one. | ||
15 | * | ||
16 | * @Route("/developer", name="developer") | ||
17 | * | ||
18 | * @return \Symfony\Component\HttpFoundation\Response | ||
19 | */ | ||
20 | public function indexAction() | ||
21 | { | ||
22 | $clients = $this->getDoctrine()->getRepository('WallabagApiBundle:Client')->findAll(); | ||
23 | |||
24 | return $this->render('@WallabagCore/themes/common/Developer/index.html.twig', [ | ||
25 | 'clients' => $clients, | ||
26 | ]); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Create a client (an app). | ||
31 | * | ||
32 | * @param Request $request | ||
33 | * | ||
34 | * @Route("/developer/client/create", name="developer_create_client") | ||
35 | * | ||
36 | * @return \Symfony\Component\HttpFoundation\Response | ||
37 | */ | ||
38 | public function createClientAction(Request $request) | ||
39 | { | ||
40 | $em = $this->getDoctrine()->getManager(); | ||
41 | $client = new Client(); | ||
42 | $clientForm = $this->createForm(ClientType::class, $client); | ||
43 | $clientForm->handleRequest($request); | ||
44 | |||
45 | if ($clientForm->isValid()) { | ||
46 | $client->setAllowedGrantTypes(['token', 'authorization_code', 'password', 'refresh_token']); | ||
47 | $em->persist($client); | ||
48 | $em->flush(); | ||
49 | |||
50 | $this->get('session')->getFlashBag()->add( | ||
51 | 'notice', | ||
52 | $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()]) | ||
53 | ); | ||
54 | |||
55 | return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [ | ||
56 | 'client_id' => $client->getPublicId(), | ||
57 | 'client_secret' => $client->getSecret(), | ||
58 | 'client_name' => $client->getName(), | ||
59 | ]); | ||
60 | } | ||
61 | |||
62 | return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [ | ||
63 | 'form' => $clientForm->createView(), | ||
64 | ]); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Remove a client. | ||
69 | * | ||
70 | * @param Client $client | ||
71 | * | ||
72 | * @Route("/developer/client/delete/{id}", requirements={"id" = "\d+"}, name="developer_delete_client") | ||
73 | * | ||
74 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
75 | */ | ||
76 | public function deleteClientAction(Client $client) | ||
77 | { | ||
78 | $em = $this->getDoctrine()->getManager(); | ||
79 | $em->remove($client); | ||
80 | $em->flush(); | ||
81 | |||
82 | $this->get('session')->getFlashBag()->add( | ||
83 | 'notice', | ||
84 | $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()]) | ||
85 | ); | ||
86 | |||
87 | return $this->redirect($this->generateUrl('developer')); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Display developer how to use an existing app. | ||
92 | * | ||
93 | * @Route("/developer/howto/first-app", name="developer_howto_firstapp") | ||
94 | * | ||
95 | * @return \Symfony\Component\HttpFoundation\Response | ||
96 | */ | ||
97 | public function howtoFirstAppAction() | ||
98 | { | ||
99 | return $this->render('@WallabagCore/themes/common/Developer/howto_app.html.twig'); | ||
100 | } | ||
101 | } | ||
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 85875589..fa573988 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -28,7 +28,8 @@ class WallabagRestController extends FOSRestController | |||
28 | * | 28 | * |
29 | * @ApiDoc( | 29 | * @ApiDoc( |
30 | * parameters={ | 30 | * parameters={ |
31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} | 31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, |
32 | * {"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 | * } |
33 | * ) | 34 | * ) |
34 | * | 35 | * |
@@ -38,6 +39,25 @@ class WallabagRestController extends FOSRestController | |||
38 | { | 39 | { |
39 | $this->validateAuthentication(); | 40 | $this->validateAuthentication(); |
40 | 41 | ||
42 | $urls = $request->query->get('urls', []); | ||
43 | |||
44 | // handle multiple urls first | ||
45 | if (!empty($urls)) { | ||
46 | $results = []; | ||
47 | foreach ($urls as $url) { | ||
48 | $res = $this->getDoctrine() | ||
49 | ->getRepository('WallabagCoreBundle:Entry') | ||
50 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
51 | |||
52 | $results[$url] = false === $res ? false : true; | ||
53 | } | ||
54 | |||
55 | $json = $this->get('serializer')->serialize($results, 'json'); | ||
56 | |||
57 | return (new JsonResponse())->setJson($json); | ||
58 | } | ||
59 | |||
60 | // let's see if it is a simple url? | ||
41 | $url = $request->query->get('url', ''); | 61 | $url = $request->query->get('url', ''); |
42 | 62 | ||
43 | if (empty($url)) { | 63 | if (empty($url)) { |
@@ -392,7 +412,7 @@ class WallabagRestController extends FOSRestController | |||
392 | 412 | ||
393 | $tags = $this->getDoctrine() | 413 | $tags = $this->getDoctrine() |
394 | ->getRepository('WallabagCoreBundle:Tag') | 414 | ->getRepository('WallabagCoreBundle:Tag') |
395 | ->findAllTagsWithEntries($this->getUser()->getId()); | 415 | ->findAllTags($this->getUser()->getId()); |
396 | 416 | ||
397 | $json = $this->get('serializer')->serialize($tags, 'json'); | 417 | $json = $this->get('serializer')->serialize($tags, 'json'); |
398 | 418 | ||
@@ -425,6 +445,8 @@ class WallabagRestController extends FOSRestController | |||
425 | ->getRepository('WallabagCoreBundle:Entry') | 445 | ->getRepository('WallabagCoreBundle:Entry') |
426 | ->removeTag($this->getUser()->getId(), $tag); | 446 | ->removeTag($this->getUser()->getId(), $tag); |
427 | 447 | ||
448 | $this->cleanOrphanTag($tag); | ||
449 | |||
428 | $json = $this->get('serializer')->serialize($tag, 'json'); | 450 | $json = $this->get('serializer')->serialize($tag, 'json'); |
429 | 451 | ||
430 | return (new JsonResponse())->setJson($json); | 452 | return (new JsonResponse())->setJson($json); |
@@ -465,6 +487,8 @@ class WallabagRestController extends FOSRestController | |||
465 | ->getRepository('WallabagCoreBundle:Entry') | 487 | ->getRepository('WallabagCoreBundle:Entry') |
466 | ->removeTags($this->getUser()->getId(), $tags); | 488 | ->removeTags($this->getUser()->getId(), $tags); |
467 | 489 | ||
490 | $this->cleanOrphanTag($tags); | ||
491 | |||
468 | $json = $this->get('serializer')->serialize($tags, 'json'); | 492 | $json = $this->get('serializer')->serialize($tags, 'json'); |
469 | 493 | ||
470 | return (new JsonResponse())->setJson($json); | 494 | return (new JsonResponse())->setJson($json); |
@@ -489,6 +513,8 @@ class WallabagRestController extends FOSRestController | |||
489 | ->getRepository('WallabagCoreBundle:Entry') | 513 | ->getRepository('WallabagCoreBundle:Entry') |
490 | ->removeTag($this->getUser()->getId(), $tag); | 514 | ->removeTag($this->getUser()->getId(), $tag); |
491 | 515 | ||
516 | $this->cleanOrphanTag($tag); | ||
517 | |||
492 | $json = $this->get('serializer')->serialize($tag, 'json'); | 518 | $json = $this->get('serializer')->serialize($tag, 'json'); |
493 | 519 | ||
494 | return (new JsonResponse())->setJson($json); | 520 | return (new JsonResponse())->setJson($json); |
@@ -511,6 +537,28 @@ class WallabagRestController extends FOSRestController | |||
511 | } | 537 | } |
512 | 538 | ||
513 | /** | 539 | /** |
540 | * Remove orphan tag in case no entries are associated to it. | ||
541 | * | ||
542 | * @param Tag|array $tags | ||
543 | */ | ||
544 | private function cleanOrphanTag($tags) | ||
545 | { | ||
546 | if (!is_array($tags)) { | ||
547 | $tags = [$tags]; | ||
548 | } | ||
549 | |||
550 | $em = $this->getDoctrine()->getManager(); | ||
551 | |||
552 | foreach ($tags as $tag) { | ||
553 | if (count($tag->getEntries()) === 0) { | ||
554 | $em->remove($tag); | ||
555 | } | ||
556 | } | ||
557 | |||
558 | $em->flush(); | ||
559 | } | ||
560 | |||
561 | /** | ||
514 | * Validate that the first id is equal to the second one. | 562 | * Validate that the first id is equal to the second one. |
515 | * If not, throw exception. It means a user try to access information from an other user. | 563 | * If not, throw exception. It means a user try to access information from an other user. |
516 | * | 564 | * |
diff --git a/src/Wallabag/ApiBundle/Entity/AccessToken.php b/src/Wallabag/ApiBundle/Entity/AccessToken.php index 2ff63a83..c09a0c80 100644 --- a/src/Wallabag/ApiBundle/Entity/AccessToken.php +++ b/src/Wallabag/ApiBundle/Entity/AccessToken.php | |||
@@ -19,7 +19,7 @@ class AccessToken extends BaseAccessToken | |||
19 | protected $id; | 19 | protected $id; |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @ORM\ManyToOne(targetEntity="Client") | 22 | * @ORM\ManyToOne(targetEntity="Client", inversedBy="accessTokens") |
23 | * @ORM\JoinColumn(nullable=false) | 23 | * @ORM\JoinColumn(nullable=false) |
24 | */ | 24 | */ |
25 | protected $client; | 25 | protected $client; |
diff --git a/src/Wallabag/ApiBundle/Entity/Client.php b/src/Wallabag/ApiBundle/Entity/Client.php index 3e2f491c..f7898ac8 100644 --- a/src/Wallabag/ApiBundle/Entity/Client.php +++ b/src/Wallabag/ApiBundle/Entity/Client.php | |||
@@ -25,6 +25,16 @@ class Client extends BaseClient | |||
25 | */ | 25 | */ |
26 | protected $name; | 26 | protected $name; |
27 | 27 | ||
28 | /** | ||
29 | * @ORM\OneToMany(targetEntity="RefreshToken", mappedBy="client", cascade={"remove"}) | ||
30 | */ | ||
31 | protected $refreshTokens; | ||
32 | |||
33 | /** | ||
34 | * @ORM\OneToMany(targetEntity="AccessToken", mappedBy="client", cascade={"remove"}) | ||
35 | */ | ||
36 | protected $accessTokens; | ||
37 | |||
28 | public function __construct() | 38 | public function __construct() |
29 | { | 39 | { |
30 | parent::__construct(); | 40 | parent::__construct(); |
diff --git a/src/Wallabag/ApiBundle/Entity/RefreshToken.php b/src/Wallabag/ApiBundle/Entity/RefreshToken.php index 6d0cab68..822a02d8 100644 --- a/src/Wallabag/ApiBundle/Entity/RefreshToken.php +++ b/src/Wallabag/ApiBundle/Entity/RefreshToken.php | |||
@@ -19,7 +19,7 @@ class RefreshToken extends BaseRefreshToken | |||
19 | protected $id; | 19 | protected $id; |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @ORM\ManyToOne(targetEntity="Client") | 22 | * @ORM\ManyToOne(targetEntity="Client", inversedBy="refreshTokens") |
23 | * @ORM\JoinColumn(nullable=false) | 23 | * @ORM\JoinColumn(nullable=false) |
24 | */ | 24 | */ |
25 | protected $client; | 25 | protected $client; |
diff --git a/src/Wallabag/ApiBundle/Form/Type/ClientType.php b/src/Wallabag/ApiBundle/Form/Type/ClientType.php new file mode 100644 index 00000000..0ea1a9c5 --- /dev/null +++ b/src/Wallabag/ApiBundle/Form/Type/ClientType.php | |||
@@ -0,0 +1,46 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ApiBundle\Form\Type; | ||
4 | |||
5 | use Symfony\Component\Form\AbstractType; | ||
6 | use Symfony\Component\Form\CallbackTransformer; | ||
7 | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
8 | use Symfony\Component\Form\Extension\Core\Type\UrlType; | ||
9 | use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
10 | use Symfony\Component\Form\FormBuilderInterface; | ||
11 | use Symfony\Component\OptionsResolver\OptionsResolver; | ||
12 | |||
13 | class ClientType extends AbstractType | ||
14 | { | ||
15 | public function buildForm(FormBuilderInterface $builder, array $options) | ||
16 | { | ||
17 | $builder | ||
18 | ->add('name', TextType::class, ['label' => 'developer.client.form.name_label']) | ||
19 | ->add('redirect_uris', UrlType::class, ['required' => false, 'label' => 'developer.client.form.redirect_uris_label']) | ||
20 | ->add('save', SubmitType::class, ['label' => 'developer.client.form.save_label']) | ||
21 | ; | ||
22 | |||
23 | $builder->get('redirect_uris') | ||
24 | ->addModelTransformer(new CallbackTransformer( | ||
25 | function ($originalUri) { | ||
26 | return $originalUri; | ||
27 | }, | ||
28 | function ($submittedUri) { | ||
29 | return [$submittedUri]; | ||
30 | } | ||
31 | )) | ||
32 | ; | ||
33 | } | ||
34 | |||
35 | public function configureOptions(OptionsResolver $resolver) | ||
36 | { | ||
37 | $resolver->setDefaults([ | ||
38 | 'data_class' => 'Wallabag\ApiBundle\Entity\Client', | ||
39 | ]); | ||
40 | } | ||
41 | |||
42 | public function getBlockPrefix() | ||
43 | { | ||
44 | return 'client'; | ||
45 | } | ||
46 | } | ||