]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/DeveloperController.php
Update deps
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / DeveloperController.php
CommitLineData
24152cdb
NL
1<?php
2
ee32248f 3namespace Wallabag\ApiBundle\Controller;
24152cdb 4
24152cdb 5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
f808b016 6use Symfony\Component\HttpFoundation\Request;
115de64e 7use Symfony\Component\Routing\Annotation\Route;
abc32945 8use Wallabag\ApiBundle\Entity\Client;
ee32248f 9use Wallabag\ApiBundle\Form\Type\ClientType;
24152cdb
NL
10
11class DeveloperController extends Controller
12{
13 /**
1256f6fe
JB
14 * List all clients and link to create a new one.
15 *
24152cdb 16 * @Route("/developer", name="developer")
b6321bed
NL
17 *
18 * @return \Symfony\Component\HttpFoundation\Response
24152cdb 19 */
abc32945 20 public function indexAction()
24152cdb 21 {
23406ca3 22 $clients = $this->getDoctrine()->getRepository('WallabagApiBundle:Client')->findByUser($this->getUser()->getId());
9bf15f02 23
2ff9991a 24 return $this->render('@WallabagCore/themes/common/Developer/index.html.twig', [
9bf15f02 25 'clients' => $clients,
4094ea47 26 ]);
24152cdb
NL
27 }
28
29 /**
1256f6fe
JB
30 * Create a client (an app).
31 *
9bf15f02 32 * @Route("/developer/client/create", name="developer_create_client")
b6321bed
NL
33 *
34 * @return \Symfony\Component\HttpFoundation\Response
24152cdb
NL
35 */
36 public function createClientAction(Request $request)
37 {
abc32945 38 $em = $this->getDoctrine()->getManager();
23406ca3 39 $client = new Client($this->getUser());
abc32945
NL
40 $clientForm = $this->createForm(ClientType::class, $client);
41 $clientForm->handleRequest($request);
42
21e7ccef 43 if ($clientForm->isSubmitted() && $clientForm->isValid()) {
2e5b2fa8 44 $client->setAllowedGrantTypes(['token', 'authorization_code', 'password', 'refresh_token']);
abc32945
NL
45 $em->persist($client);
46 $em->flush();
47
48 $this->get('session')->getFlashBag()->add(
49 'notice',
543da3e0 50 $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
abc32945
NL
51 );
52
2ff9991a 53 return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [
abc32945
NL
54 'client_id' => $client->getPublicId(),
55 'client_secret' => $client->getSecret(),
9c545fe0 56 'client_name' => $client->getName(),
4094ea47 57 ]);
abc32945 58 }
24152cdb 59
2ff9991a 60 return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [
abc32945 61 'form' => $clientForm->createView(),
4094ea47 62 ]);
24152cdb 63 }
b6321bed
NL
64
65 /**
9bf15f02
JB
66 * Remove a client.
67 *
9bf15f02
JB
68 * @Route("/developer/client/delete/{id}", requirements={"id" = "\d+"}, name="developer_delete_client")
69 *
70 * @return \Symfony\Component\HttpFoundation\RedirectResponse
71 */
1256f6fe 72 public function deleteClientAction(Client $client)
9bf15f02 73 {
f808b016 74 if (null === $this->getUser() || $client->getUser()->getId() !== $this->getUser()->getId()) {
23406ca3
NL
75 throw $this->createAccessDeniedException('You can not access this client.');
76 }
77
9bf15f02
JB
78 $em = $this->getDoctrine()->getManager();
79 $em->remove($client);
80 $em->flush();
81
82 $this->get('session')->getFlashBag()->add(
83 'notice',
543da3e0 84 $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
9bf15f02
JB
85 );
86
87 return $this->redirect($this->generateUrl('developer'));
88 }
89
90 /**
1256f6fe
JB
91 * Display developer how to use an existing app.
92 *
9bf15f02 93 * @Route("/developer/howto/first-app", name="developer_howto_firstapp")
b6321bed
NL
94 *
95 * @return \Symfony\Component\HttpFoundation\Response
96 */
abc32945 97 public function howtoFirstAppAction()
b6321bed 98 {
2ff9991a 99 return $this->render('@WallabagCore/themes/common/Developer/howto_app.html.twig');
b6321bed 100 }
24152cdb 101}