1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Model\UserCredentials; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Service\AccessTokenServiceInterface; |
7
|
|
|
use Dontdrinkandroot\RestBundle\Service\Normalizer; |
8
|
|
|
use Dontdrinkandroot\RestBundle\Service\RestRequestParser; |
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
14
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Philip Washington Sorst <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class AccessTokenController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var AccessTokenServiceInterface |
23
|
|
|
*/ |
24
|
|
|
private $accessTokenService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RestRequestParser |
28
|
|
|
*/ |
29
|
|
|
private $restRequestParser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Normalizer |
33
|
|
|
*/ |
34
|
|
|
private $normalizer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var TokenStorageInterface |
38
|
|
|
*/ |
39
|
|
|
private $tokenStorage; |
40
|
|
|
|
41
|
12 |
|
public function __construct( |
42
|
|
|
AccessTokenServiceInterface $accessTokenService, |
43
|
|
|
RestRequestParser $restRequestParser, |
44
|
|
|
Normalizer $normalizer, |
45
|
|
|
TokenStorageInterface $tokenStorage |
46
|
|
|
) { |
47
|
12 |
|
$this->accessTokenService = $accessTokenService; |
48
|
12 |
|
$this->restRequestParser = $restRequestParser; |
49
|
12 |
|
$this->normalizer = $normalizer; |
50
|
12 |
|
$this->tokenStorage = $tokenStorage; |
51
|
12 |
|
} |
52
|
|
|
|
53
|
2 |
|
public function createAction(Request $request) |
54
|
|
|
{ |
55
|
2 |
|
$userCredentials = $this->restRequestParser->parseEntity($request, UserCredentials::class); |
56
|
2 |
|
$accessToken = $this->accessTokenService->createAccessToken( |
57
|
2 |
|
$userCredentials->getUsername(), |
58
|
2 |
|
$userCredentials->getPassword() |
59
|
|
|
); |
60
|
2 |
|
$content = $this->normalizer->normalize($accessToken); |
61
|
|
|
|
62
|
2 |
|
return new JsonResponse($content, Response::HTTP_CREATED); |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
public function listAction(Request $request) |
|
|
|
|
66
|
|
|
{ |
67
|
4 |
|
$user = $this->tokenStorage->getToken()->getUser(); |
68
|
4 |
|
if (!is_object($user)) { |
69
|
2 |
|
throw new AccessDeniedException(); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$tokens = $this->accessTokenService->listByUser($user); |
73
|
2 |
|
$content = $this->normalizer->normalize($tokens); |
74
|
|
|
|
75
|
2 |
|
return new JsonResponse($content); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
public function deleteAction(Request $request, $token) |
|
|
|
|
79
|
|
|
{ |
80
|
6 |
|
$user = $this->tokenStorage->getToken()->getUser(); |
81
|
6 |
|
if (!is_object($user)) { |
82
|
2 |
|
throw new AccessDeniedException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$accessToken = $this->accessTokenService->findByToken($token); |
86
|
4 |
|
if (null === $accessToken) { |
87
|
2 |
|
throw new NotFoundHttpException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$this->accessTokenService->remove($accessToken); |
91
|
|
|
|
92
|
2 |
|
return new JsonResponse(null, Response::HTTP_NO_CONTENT); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.