]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/DeveloperController.php
Update deps
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / DeveloperController.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\Routing\Annotation\Route;
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')->findByUser($this->getUser()->getId());
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 * @Route("/developer/client/create", name="developer_create_client")
33 *
34 * @return \Symfony\Component\HttpFoundation\Response
35 */
36 public function createClientAction(Request $request)
37 {
38 $em = $this->getDoctrine()->getManager();
39 $client = new Client($this->getUser());
40 $clientForm = $this->createForm(ClientType::class, $client);
41 $clientForm->handleRequest($request);
42
43 if ($clientForm->isSubmitted() && $clientForm->isValid()) {
44 $client->setAllowedGrantTypes(['token', 'authorization_code', 'password', 'refresh_token']);
45 $em->persist($client);
46 $em->flush();
47
48 $this->get('session')->getFlashBag()->add(
49 'notice',
50 $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
51 );
52
53 return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [
54 'client_id' => $client->getPublicId(),
55 'client_secret' => $client->getSecret(),
56 'client_name' => $client->getName(),
57 ]);
58 }
59
60 return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [
61 'form' => $clientForm->createView(),
62 ]);
63 }
64
65 /**
66 * Remove a client.
67 *
68 * @Route("/developer/client/delete/{id}", requirements={"id" = "\d+"}, name="developer_delete_client")
69 *
70 * @return \Symfony\Component\HttpFoundation\RedirectResponse
71 */
72 public function deleteClientAction(Client $client)
73 {
74 if (null === $this->getUser() || $client->getUser()->getId() !== $this->getUser()->getId()) {
75 throw $this->createAccessDeniedException('You can not access this client.');
76 }
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 }