Failed Conditions
Branch master (116909)
by Guilherme
08:28
created

OAuthEventListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A onPreAuthorizationProcess() 0 18 3
A getUser() 0 3 1
A checkSubjectIdentifierPersisted() 0 13 2
B onPostAuthorizationProcess() 0 41 3
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\OAuthBundle\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use FOS\OAuthServerBundle\Event\OAuthEvent;
15
use LoginCidadao\CoreBundle\Entity\Authorization;
16
use LoginCidadao\CoreBundle\Entity\PersonRepository;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use LoginCidadao\OAuthBundle\Entity\Client;
19
use LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper;
20
use LoginCidadao\OAuthBundle\Model\ClientInterface;
21
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier;
22
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
23
24
class OAuthEventListener
25
{
26
    /** @var EntityManagerInterface */
27
    private $em;
28
29
    /** @var PersonRepository */
30
    private $personRepo;
31
32
    /** @var SubjectIdentifierService */
33
    private $subjectIdentifierService;
34
35
    /** @var ScopeFinderHelper */
36
    private $scopeFinder;
37
38 7
    public function __construct(
39
        EntityManagerInterface $em,
40
        ScopeFinderHelper $scopeFinder,
41
        SubjectIdentifierService $subjectIdentifierService
42
    ) {
43 7
        $this->em = $em;
44 7
        $this->personRepo = $this->em->getRepository('LoginCidadaoCoreBundle:Person');
45 7
        $this->scopeFinder = $scopeFinder;
46 7
        $this->subjectIdentifierService = $subjectIdentifierService;
47 7
    }
48
49 4
    public function onPreAuthorizationProcess(OAuthEvent $event)
50
    {
51 4
        $scope = $this->scopeFinder->getScope();
52
        /** @var PersonInterface $user */
53 4
        $user = $this->getUser($event);
54 4
        if (!$user) {
0 ignored issues
show
introduced by
The condition ! $user can never be false.
Loading history...
55 1
            return;
56
        }
57
58
        /** @var ClientInterface $client */
59 3
        $client = $event->getClient();
60
61 3
        $event->setAuthorizedClient(
62 3
            $user->isAuthorizedClient($client, $scope)
63
        );
64
65 3
        if ($event->isAuthorizedClient()) {
66 2
            $this->checkSubjectIdentifierPersisted($user, $client);
67
        }
68 3
    }
69
70 3
    public function onPostAuthorizationProcess(OAuthEvent $event)
71
    {
72 3
        if (!$event->isAuthorizedClient()) {
73 1
            return;
74
        }
75
76
        /** @var Client $client */
77 2
        $client = $event->getClient();
78
79
        /** @var PersonInterface $user */
80 2
        $user = $this->getUser($event);
81 2
        $scope = $this->scopeFinder->getScope();
82
83 2
        $authRepo = $this->em->getRepository('LoginCidadaoCoreBundle:Authorization');
84 2
        $currentAuth = $authRepo->findOneBy([
85 2
            'person' => $user,
86 2
            'client' => $client,
87
        ]);
88
89
        // if the authorization is already there, update it.
90 2
        if ($currentAuth instanceof Authorization) {
91 1
            $merged = array_merge($currentAuth->getScope(), $scope);
92 1
            $currentAuth->setScope($merged);
93 1
            $this->checkSubjectIdentifierPersisted($user, $client);
94
        } else {
95 1
            $authorization = new Authorization();
96 1
            $authorization->setClient($client);
97 1
            $authorization->setPerson($user);
98 1
            $authorization->setScope($scope);
99
100 1
            $subjectIdentifier = $this->subjectIdentifierService->getSubjectIdentifier($user, $client->getMetadata());
101 1
            $sub = new SubjectIdentifier();
102 1
            $sub->setPerson($user)
103 1
                ->setClient($client)
104 1
                ->setSubjectIdentifier($subjectIdentifier);
105
106 1
            $this->em->persist($authorization);
107 1
            $this->em->persist($sub);
108
        }
109
110 2
        $this->em->flush();
111 2
    }
112
113 6
    public function getUser(OAuthEvent $event)
114
    {
115 6
        return $this->personRepo->findOneBy(['username' => $event->getUser()->getUsername()]);
116
    }
117
118 3
    private function checkSubjectIdentifierPersisted(PersonInterface $person, ClientInterface $client)
119
    {
120 3
        if ($this->subjectIdentifierService->isSubjectIdentifierPersisted($person, $client)) {
121 2
            return;
122
        }
123
124 1
        $subjectIdentifier = $this->subjectIdentifierService->getSubjectIdentifier($person, $client->getMetadata());
125 1
        $sub = new SubjectIdentifier();
126 1
        $sub->setPerson($person)
127 1
            ->setClient($client)
128 1
            ->setSubjectIdentifier($subjectIdentifier);
129 1
        $this->em->persist($sub);
130 1
        $this->em->flush($sub);
131 1
    }
132
}
133