-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathAdminController.php
117 lines (103 loc) · 4.32 KB
/
AdminController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\InvalidTokenException;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\ExpiredTokenException;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\CookieTokenExtractor;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\PreAuthenticationJWTUserToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Limenius\Liform\Liform;
use App\Entity\Recipe;
use App\Form\Type\RecipeType;
class AdminController extends Controller
{
/**
* @Route("/admin/liform/", name="liform")
*/
public function liformAction(Request $request)
{
try {
$token = $this->getValidToken($request);
$recipe = new Recipe();
$serializer = $this->get('serializer');
$form = $this->createForm(RecipeType::Class, $recipe,
array('csrf_protection' => false)
);
$recipes = $this->getDoctrine()
->getRepository(Recipe::class)
->findAll();
return $this->render('admin/index.html.twig', [
'authToken' => $token,
'recipes' => $serializer->normalize($recipes),
'schema' => $this->get('liform')->transform($form),
'initialValues' => $serializer->normalize($form->createView()),
]);
} catch (\Exception $e) {
return $this->render('admin/index.html.twig', [
'authToken' => null,
'schema' => null,
'recipes' => [],
'initialValues' => null,
'props' => [ ],
]);
}
}
/**
* @Route("/admin/api/form", methods={"GET"}, name="admin_form")
*/
public function getFormAction(Request $request)
{
$recipe = new Recipe();
$serializer = $this->get('serializer');
$form = $this->createForm(RecipeType::Class, $recipe);
return new JsonResponse([
'schema' => $this->get('liform')->transform($form),
'initialValues' => $serializer->normalize($form->createView()),
]);
}
/**
* @Route("/admin/api/recipes", methods={"POST"}, name="liform_post")
*/
public function liformPostAction(Request $request)
{
$serializer = $this->get('serializer');
$recipe = new Recipe();
$data = json_decode($request->getContent(), true);
$form = $this->createForm(RecipeType::Class, $recipe);
$form->submit($data);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($recipe);
$em->flush();
$response = new Response($serializer->serialize($recipe, 'json'), 201);
$response->headers->set('Location', 'We should provide a url here, but this is a dummy example and there is no location where you can retrieve a single recipe, so...');
$response->headers->set('Content-Type', 'application/json');
return $response;
}
return new JsonResponse($serializer->normalize($form), 400);
}
private function getValidToken(Request $request) {
$tokenExtractor = new CookieTokenExtractor('BEARER');
if (false === ($jsonWebToken = $tokenExtractor->extract($request))) {
return;
}
$preAuthToken = new PreAuthenticationJWTUserToken($jsonWebToken);
try {
if (!$payload = $this->get('lexik_jwt_authentication.jwt_manager')->decode($preAuthToken)) {
throw new InvalidTokenException('Invalid JWT Token');
}
$preAuthToken->setPayload($payload);
} catch (JWTDecodeFailureException $e) {
if (JWTDecodeFailureException::EXPIRED_TOKEN === $e->getReason()) {
throw new ExpiredTokenException();
}
throw new InvalidTokenException('Invalid JWT Token', 0, $e);
}
return $preAuthToken;
}
}