Completed
Pull Request — master (#181)
by Beñat
13:17
created

Controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Infrastructure\Ui\Web\Api\Rest;
16
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
20
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
21
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
22
23
class Controller
24
{
25
    private $authorizationChecker;
26
    private $tokenStorage;
27
28
    public function __construct(
29
        AuthorizationCheckerInterface $authorizationChecker,
30
        TokenStorageInterface $tokenStorage
31
    ) {
32
        $this->authorizationChecker = $authorizationChecker;
33
        $this->tokenStorage = $tokenStorage;
34
    }
35
36
    public function userAction() : JsonResponse
37
    {
38
        if (!$this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)) {
39
            throw new AccessDeniedException('This request requires authentication');
40
        }
41
42
        return new JsonResponse(['userId' => $this->tokenStorage->getToken()->getUser()->id]);
43
    }
44
}
45