3 namespace Wallabag\ApiBundle\Controller
;
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method
;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
7 use Symfony\Component\Finder\Exception\AccessDeniedException
;
8 use Symfony\Component\HttpFoundation\JsonResponse
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
11 use Wallabag\ApiBundle\Entity\AccessToken
;
12 use Wallabag\ApiBundle\Entity\Client
;
13 use Wallabag\ApiBundle\Form\Type\ClientType
;
15 class AppsController
extends Controller
18 * List all clients and link to create a new one.
20 * @Route("/apps", name="apps")
22 * @return \Symfony\Component\HttpFoundation\Response
24 public function indexAction()
26 $clients = $this->getDoctrine()->getRepository('WallabagApiBundle:Client')->findByUser($this->getUser()->getId());
28 $apps = $this->getDoctrine()->getRepository('WallabagApiBundle:AccessToken')->findAppsByUser($this->getUser()->getId());
30 return $this->render('@WallabagCore/themes/common/Developer/index.html.twig', [
31 'clients' => $clients,
39 * @param Request $request
41 * @Route("/api/apps", name="apps_create")
44 * @return \Symfony\Component\HttpFoundation\Response
46 public function createAppAction(Request
$request)
48 $em = $this->getDoctrine()->getManager();
50 $clientName = $request->request
->get('client_name');
51 $redirectURIs = $request->request
->get('redirect_uris');
52 $logoURI = $request->request
->get('logo_uri');
53 $description = $request->request
->get('description');
54 $appURI = $request->request
->get('app_uri');
55 $nextRedirect = $request->request
->get('uri_redirect_after_creation');
58 return new JsonResponse([
59 'error' => 'invalid_client_name',
60 'error_description' => 'The client name cannot be empty',
65 return new JsonResponse([
66 'error' => 'invalid_redirect_uri',
67 'error_description' => 'One or more redirect_uri values are invalid',
71 $redirectURIs = (array) $redirectURIs;
73 $client = new Client();
75 $client->setName($clientName);
77 $client->setDescription($description);
79 $client->setRedirectUris($redirectURIs);
81 $client->setImage($logoURI);
82 $client->setAppUrl($appURI);
84 $client->setAllowedGrantTypes(['token', 'refresh_token', 'authorization_code']);
85 $em->persist($client);
88 return new JsonResponse([
89 'client_id' => $client->getPublicId(),
90 'client_secret' => $client->getSecret(),
91 'client_name' => $client->getName(),
92 'redirect_uri' => $client->getRedirectUris(),
93 'description' => $client->getDescription(),
94 'logo_uri' => $client->getImage(),
95 'app_uri' => $client->getAppUrl(),
100 * Create a client (an app).
102 * @param Request $request
104 * @Route("/apps/client/create", name="apps_create_client")
106 * @return \Symfony\Component\HttpFoundation\Response
108 public function createClientAction(Request
$request)
110 $em = $this->getDoctrine()->getManager();
111 $client = new Client($this->getUser());
112 $clientForm = $this->createForm(ClientType
::class, $client);
113 $clientForm->handleRequest($request);
115 if ($clientForm->isSubmitted() && $clientForm->isValid()) {
116 $client->setAllowedGrantTypes(['password', 'token', 'refresh_token', 'client_credentials']); // Password is depreciated
117 $em->persist($client);
120 $this->get('session')->getFlashBag()->add(
122 $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
125 return $this->render('@WallabagCore/themes/common/Developer/client_parameters.html.twig', [
126 'client_id' => $client->getPublicId(),
127 'client_secret' => $client->getSecret(),
128 'client_name' => $client->getName(),
132 return $this->render('@WallabagCore/themes/common/Developer/client.html.twig', [
133 'form' => $clientForm->createView(),
138 * Revoke an access token
140 * @Route("/api/revoke/{token}", name="apps_revoke_access_token")
141 * @return JsonResponse
143 public function removeAccessTokenAction($token)
145 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
146 throw new AccessDeniedException();
149 $em = $this->getDoctrine()->getManager();
150 $accessToken = $em->getRepository('WallabagApiBundle:AccessToken')->findOneBy([
151 'user' => $this->getUser()->getId(),
155 $em->remove($accessToken);
158 return new JsonResponse([], 204);
160 return new JsonResponse([], 404);
166 * @param Client $client
168 * @Route("/apps/client/delete/{id}", requirements={"id" = "\d+"}, name="apps_delete_client")
170 * @return \Symfony\Component\HttpFoundation\RedirectResponse
172 public function deleteClientAction(Client
$client)
174 if (null === $this->getUser() || $client->getUser()->getId() != $this->getUser()->getId()) {
175 throw $this->createAccessDeniedException('You can not access this client.');
178 $em = $this->getDoctrine()->getManager();
179 $em->remove($client);
182 $this->get('session')->getFlashBag()->add(
184 $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
187 return $this->redirect($this->generateUrl('apps'));