]>
Commit | Line | Data |
---|---|---|
24152cdb NL |
1 | <?php |
2 | ||
ee32248f | 3 | namespace Wallabag\ApiBundle\Controller; |
24152cdb NL |
4 | |
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
6 | use Symfony\Component\HttpFoundation\Request; | |
7 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
abc32945 | 8 | use Wallabag\ApiBundle\Entity\Client; |
ee32248f | 9 | use Wallabag\ApiBundle\Form\Type\ClientType; |
24152cdb NL |
10 | |
11 | class 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, |
1abd428b | 26 | 'wallabag_url' => $this->get('craue_config')->get('wallabag_url'), |
4094ea47 | 27 | ]); |
24152cdb NL |
28 | } |
29 | ||
30 | /** | |
1256f6fe JB |
31 | * Create a client (an app). |
32 | * | |
24152cdb NL |
33 | * @param Request $request |
34 | * | |
9bf15f02 | 35 | * @Route("/developer/client/create", name="developer_create_client") |
b6321bed NL |
36 | * |
37 | * @return \Symfony\Component\HttpFoundation\Response | |
24152cdb NL |
38 | */ |
39 | public function createClientAction(Request $request) | |
40 | { | |
abc32945 | 41 | $em = $this->getDoctrine()->getManager(); |
23406ca3 | 42 | $client = new Client($this->getUser()); |
abc32945 NL |
43 | $clientForm = $this->createForm(ClientType::class, $client); |
44 | $clientForm->handleRequest($request); | |
45 | ||
21e7ccef | 46 | if ($clientForm->isSubmitted() && $clientForm->isValid()) { |
4094ea47 | 47 | $client->setAllowedGrantTypes(['token', 'authorization_code', 'password', 'refresh_token']); |
abc32945 NL |
48 | $em->persist($client); |
49 | $em->flush(); | |
50 | ||
51 | $this->get('session')->getFlashBag()->add( | |
52 | 'notice', | |
543da3e0 | 53 | $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()]) |
abc32945 NL |
54 | ); |
55 | ||
2ff9991a | 56 | return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [ |
abc32945 NL |
57 | 'client_id' => $client->getPublicId(), |
58 | 'client_secret' => $client->getSecret(), | |
9c545fe0 | 59 | 'client_name' => $client->getName(), |
4094ea47 | 60 | ]); |
abc32945 | 61 | } |
24152cdb | 62 | |
2ff9991a | 63 | return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [ |
abc32945 | 64 | 'form' => $clientForm->createView(), |
4094ea47 | 65 | ]); |
24152cdb | 66 | } |
b6321bed NL |
67 | |
68 | /** | |
9bf15f02 JB |
69 | * Remove a client. |
70 | * | |
1256f6fe | 71 | * @param Client $client |
9bf15f02 JB |
72 | * |
73 | * @Route("/developer/client/delete/{id}", requirements={"id" = "\d+"}, name="developer_delete_client") | |
74 | * | |
75 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | |
76 | */ | |
1256f6fe | 77 | public function deleteClientAction(Client $client) |
9bf15f02 | 78 | { |
23406ca3 NL |
79 | if (null === $this->getUser() || $client->getUser()->getId() != $this->getUser()->getId()) { |
80 | throw $this->createAccessDeniedException('You can not access this client.'); | |
81 | } | |
82 | ||
9bf15f02 JB |
83 | $em = $this->getDoctrine()->getManager(); |
84 | $em->remove($client); | |
85 | $em->flush(); | |
86 | ||
87 | $this->get('session')->getFlashBag()->add( | |
88 | 'notice', | |
543da3e0 | 89 | $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()]) |
9bf15f02 JB |
90 | ); |
91 | ||
92 | return $this->redirect($this->generateUrl('developer')); | |
93 | } | |
94 | ||
95 | /** | |
1256f6fe JB |
96 | * Display developer how to use an existing app. |
97 | * | |
9bf15f02 | 98 | * @Route("/developer/howto/first-app", name="developer_howto_firstapp") |
b6321bed NL |
99 | * |
100 | * @return \Symfony\Component\HttpFoundation\Response | |
101 | */ | |
abc32945 | 102 | public function howtoFirstAppAction() |
b6321bed | 103 | { |
2ff9991a | 104 | return $this->render('@WallabagCore/themes/common/Developer/howto_app.html.twig'); |
b6321bed | 105 | } |
24152cdb | 106 | } |