|
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\OpenIDBundle\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Helper\SecurityHelper; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\Manager\ClientManager; |
|
17
|
|
|
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTaskValidator; |
|
18
|
|
|
use LoginCidadao\TaskStackBundle\Event\GetTasksEvent; |
|
19
|
|
|
use LoginCidadao\TaskStackBundle\Model\TaskInterface; |
|
20
|
|
|
use LoginCidadao\TaskStackBundle\TaskStackEvents; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
|
|
24
|
|
|
class TaskSubscriber implements EventSubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var SecurityHelper */ |
|
27
|
|
|
private $securityHelper; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ClientManager */ |
|
30
|
|
|
private $clientManager; |
|
31
|
|
|
|
|
32
|
|
|
/** @var CompleteUserInfoTaskValidator */ |
|
33
|
|
|
private $taskValidator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* TaskSubscriber constructor. |
|
37
|
|
|
* @param SecurityHelper $securityHelper |
|
38
|
|
|
* @param ClientManager $clientManager |
|
39
|
|
|
* @param CompleteUserInfoTaskValidator $taskValidator |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct( |
|
42
|
|
|
SecurityHelper $securityHelper, |
|
43
|
|
|
ClientManager $clientManager, |
|
44
|
|
|
CompleteUserInfoTaskValidator $taskValidator |
|
45
|
|
|
) { |
|
46
|
|
|
$this->securityHelper = $securityHelper; |
|
47
|
|
|
$this->clientManager = $clientManager; |
|
48
|
|
|
$this->taskValidator = $taskValidator; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function getSubscribedEvents() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
TaskStackEvents::GET_TASKS => ['onGetTasks', 50], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function onGetTasks(GetTasksEvent $event) |
|
59
|
|
|
{ |
|
60
|
|
|
$request = $event->getRequest(); |
|
61
|
|
|
$user = $this->securityHelper->getUser(); |
|
62
|
|
|
$client = $this->getClientFromRequest($request); |
|
63
|
|
|
|
|
64
|
|
|
if (!$user instanceof PersonInterface || !$client instanceof ClientInterface) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$task = $this->taskValidator->getCompleteUserInfoTask($user, $client, $request); |
|
69
|
|
|
if ($task instanceof TaskInterface) { |
|
70
|
|
|
$event->addTask($task); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function getClientFromRequest(Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
$clientId = $request->get('client_id', $request->get('clientId')); |
|
77
|
|
|
|
|
78
|
|
|
return $this->clientManager->getClientById($clientId); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|