Completed
Push — master ( b4ede8...73596f )
by Guilherme
05:13
created

RemoteClaimManager::enforceImplicitAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 1
rs 10
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\RemoteClaimsBundle\Model;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Authorization;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\CoreBundle\Service\AuthorizationManager;
17
use LoginCidadao\OAuthBundle\Model\ClientInterface;
18
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorization;
19
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorizationRepository;
20
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository;
21
22
class RemoteClaimManager implements RemoteClaimManagerInterface
23
{
24
    /** @var EntityManagerInterface */
25
    private $em;
26
27
    /** @var RemoteClaimAuthorizationRepository */
28
    private $remoteClaimAuthorizationRepository;
29
30
    /** @var RemoteClaimRepository */
31
    private $remoteClaimRepository;
32
33
    /** @var AuthorizationManager */
34
    private $authorizationManager;
35
36
    /**
37
     * RemoteClaimManager constructor.
38
     * @param EntityManagerInterface $em
39
     * @param RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository
40
     * @param RemoteClaimRepository $remoteClaimRepository
41
     * @param AuthorizationManager $authorizationManager
42
     */
43 18
    public function __construct(
44
        EntityManagerInterface $em,
45
        RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository,
46
        RemoteClaimRepository $remoteClaimRepository,
47
        AuthorizationManager $authorizationManager
48
    ) {
49 18
        $this->em = $em;
50 18
        $this->remoteClaimAuthorizationRepository = $remoteClaimAuthorizationRepository;
51 18
        $this->remoteClaimRepository = $remoteClaimRepository;
52 18
        $this->authorizationManager = $authorizationManager;
53 18
    }
54
55
    /**
56
     * {@inheritdoc}
57
     * @throws \Doctrine\ORM\NonUniqueResultException
58
     */
59 2
    public function enforceAuthorization(RemoteClaimAuthorizationInterface $authorization)
60
    {
61 2
        $remoteClaim = $this->getExistingRemoteClaim($authorization->getClaimName());
62 2
        $this->enforceImplicitAuthorization($authorization, $remoteClaim);
63
64 2
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
65 2
        if ($existingAuthorization instanceof RemoteClaimAuthorizationInterface) {
66 1
            return $existingAuthorization;
67
        }
68
69 1
        $this->em->persist($authorization);
70
71 1
        return $authorization;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     * @throws \Doctrine\ORM\NonUniqueResultException
77
     */
78 3
    public function isAuthorized($claimName, PersonInterface $person, ClientInterface $client)
79
    {
80 3
        if (!$claimName instanceof TagUri) {
81 1
            $claimName = TagUri::createFromString($claimName);
82
        }
83 3
        $authorization = (new RemoteClaimAuthorization())
84 3
            ->setClaimName($claimName)
85 3
            ->setPerson($person)
86 3
            ->setClient($client);
87 3
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
88
89 3
        return $existingAuthorization instanceof RemoteClaimAuthorizationInterface;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function revokeAllAuthorizations(Authorization $authorization)
96
    {
97 1
        $remoteClaimAuthorizations = $this->remoteClaimAuthorizationRepository
98 1
            ->findAllByClientAndPerson($authorization->getClient(), $authorization->getPerson());
99
100 1
        foreach ($remoteClaimAuthorizations as $remoteClaimAuthorization) {
101 1
            $this->em->remove($remoteClaimAuthorization);
102
        }
103 1
        $this->em->flush();
104
105 1
        return true;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 5
    public function filterRemoteClaims($scopes)
112
    {
113 5
        $returnString = is_string($scopes);
114 5
        if ($returnString) {
115 3
            $scopes = explode(' ', $scopes);
116
        }
117
118 5
        $response = [];
119 5
        foreach ($scopes as $scope) {
120
            try {
121 4
                TagUri::createFromString($scope);
122 4
            } catch (\InvalidArgumentException $e) {
123 4
                $response[] = $scope;
124
            }
125
        }
126
127 5
        if ($returnString) {
128 3
            return implode(' ', $response);
129
        }
130
131 2
        return $response;
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137 5
    public function getExistingRemoteClaim(TagUri $claimName)
138
    {
139 5
        return $this->remoteClaimRepository->findOneBy([
140 5
            'name' => $claimName,
141
        ]);
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147 1
    public function getRemoteClaimsFromAuthorization(Authorization $authorization)
148
    {
149 1
        return $this->remoteClaimRepository
150 1
            ->findByClientAndPerson($authorization->getClient(), $authorization->getPerson());
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156 1
    public function getRemoteClaimsAuthorizationsFromAuthorization(Authorization $authorization)
157
    {
158 1
        return $this->remoteClaimAuthorizationRepository->findAllByClientAndPerson(
159 1
            $authorization->getClient(), $authorization->getPerson()
160
        );
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166 1
    public function getRemoteClaimsWithTokens(ClientInterface $client, PersonInterface $person)
167
    {
168
        /** @var RemoteClaimAuthorizationInterface[] $remoteClaimAuthorizations */
169 1
        $remoteClaimAuthorizations = $this->remoteClaimAuthorizationRepository
170 1
            ->findAllByClientAndPerson($client, $person);
171
172
        /** @var RemoteClaimInterface[] $remoteClaims */
173 1
        $remoteClaims = $this->remoteClaimRepository->findByClientAndPerson($client, $person);
174
175 1
        $response = [];
176 1
        foreach ($remoteClaimAuthorizations as $authorization) {
177 1
            $tag = $this->getTagString($authorization->getClaimName());
178 1
            $response[$tag]['authorization'] = $authorization;
179
        }
180 1
        foreach ($remoteClaims as $remoteClaim) {
181 1
            $tag = $this->getTagString($remoteClaim->getName());
182 1
            $response[$tag]['remoteClaim'] = $remoteClaim;
183
        }
184
185 1
        return $response;
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191 1
    public function getRemoteClaimAuthorizationByAccessToken(ClaimProviderInterface $claimProvider, $accessToken)
192
    {
193 1
        return $this->remoteClaimAuthorizationRepository->findOneBy([
194 1
            'claimProvider' => $claimProvider,
195 1
            'accessToken' => $accessToken,
196
        ]);
197
    }
198
199
    /**
200
     * @inheritDoc
201
     */
202 2
    public function updateRemoteClaimUri(TagUri $claimName, $uri)
203
    {
204 2
        $remoteClaim = $this->getExistingRemoteClaim($claimName);
205
206 2
        if (!$remoteClaim instanceof RemoteClaimInterface) {
207
            // TODO: log
208
            // TODO: throw exception?
209 1
            return null;
210
        }
211
212 1
        $remoteClaim->setUri($uri);
213 1
        $this->em->flush();
214
215 1
        return $remoteClaim;
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221 2
    public function enforceImplicitAuthorization(
222
        RemoteClaimAuthorizationInterface $claimAuthorization,
223
        RemoteClaimInterface $remoteClaim
224
    ): Authorization {
225
        /** @var ClaimProviderInterface|ClientInterface $provider */
226 2
        $provider = $claimAuthorization->getClaimProvider();
227
228 2
        return $this->authorizationManager->enforceAuthorization(
229 2
            $claimAuthorization->getPerson(),
230 2
            $provider,
231 2
            array_merge($remoteClaim->getEssentialScope(), $remoteClaim->getRecommendedScope()),
232 2
            AuthorizationManager::SCOPE_MERGE
233
        );
234
    }
235
236
    /**
237
     * @param string|TagUri $tag
238
     * @return string
239
     */
240 1
    private function getTagString($tag)
241
    {
242 1
        return is_string($tag) ? $tag : $tag->__toString();
243
    }
244
}
245