Failed Conditions
Pull Request — master (#262)
by Guilherme
10:25 queued 04:26
created

BaseController::convertContextToJMSContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\APIBundle\Controller;
12
13
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
14
use FOS\RestBundle\Context\Context;
15
use FOS\RestBundle\Controller\FOSRestController;
16
use JMS\Serializer\SerializationContext;
17
use LoginCidadao\APIBundle\Service\VersionService;
18
use LoginCidadao\CoreBundle\Entity\Authorization;
19
use LoginCidadao\CoreBundle\Model\PersonInterface;
20
use LoginCidadao\OAuthBundle\Entity\AccessToken;
21
use LoginCidadao\OAuthBundle\Model\ClientInterface;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
24
25
class BaseController extends FOSRestController
26
{
27
    protected function renderWithContext($content, $context = null)
28
    {
29
        $person = $this->getUser();
30
31
        if (null === $context) {
32
            $scope = $this->getClientScope($person);
33
            $context = $this->getSerializationContext($scope);
34
        }
35
36
        $view = $this->view($content)->setContext($context);
37
38
        return $this->handleView($view);
39
    }
40
41
    protected function getClientScope(
42
        PersonInterface $user,
43
        ClientInterface $client = null
44
    ) {
45
        if ($client === null) {
46
            $client = $this->getClient();
47
        }
48
49
        $authorization = $this->getDoctrine()
50
            ->getRepository('LoginCidadaoCoreBundle:Authorization')
51
            ->findOneBy(['person' => $user, 'client' => $client]);
52
        if (!($authorization instanceof Authorization)) {
53
            throw new AccessDeniedException("Access denied");
54
        }
55
56
        $scopes = $authorization->getScope();
57
        if (array_search('public', $scopes) === false) {
58
            $scopes[] = 'public';
59
        }
60
61
        return $scopes;
62
    }
63
64
    protected function getSerializationContext($scope)
65
    {
66
        /** @var VersionService $versionService */
67
        $versionService = $this->get('lc.api.version');
68
        $version = $versionService->getString($versionService->getVersionFromRequest());
69
70
        $context = new Context();
71
        $context->setGroups($scope);
72
        $context->setVersion(/** @scrutinizer ignore-type */
73
            $version);
74
75
        return $context;
76
    }
77
78
    /**
79
     * Gets the authenticated Client.
80
     *
81
     * @return ClientInterface
82
     */
83
    protected function getClient()
84
    {
85
        /** @var TokenStorageInterface $tokenStorage */
86
        $tokenStorage = $this->get('security.token_storage');
87
88
        $token = $tokenStorage->getToken();
89
90
        if (!$token instanceof OAuthToken) {
91
            return null;
92
        }
93
94
        $accessToken = $this->getDoctrine()
95
            ->getRepository('LoginCidadaoOAuthBundle:AccessToken')
96
            ->findOneBy(['token' => $token->getToken()]);
97
98
        if (!$accessToken instanceof AccessToken) {
99
            return null;
100
        }
101
102
        return $accessToken->getClient();
103
    }
104
105
    protected function getJMSSerializationContext($scope)
106
    {
107
        $context = $this->getSerializationContext($scope);
108
109
        return $this->convertContextToJMSContext($context);
110
    }
111
112
    protected function convertContextToJMSContext(Context $context): SerializationContext
113
    {
114
        $jmsContext = new SerializationContext();
115
116
        $jmsContext->setGroups($context->getGroups());
117
        $jmsContext->setSerializeNull($context->getSerializeNull());
118
        $jmsContext->setVersion($context->getVersion());
119
120
        return $jmsContext;
121
    }
122
}
123