|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\User; |
|
5
|
|
|
|
|
6
|
|
|
use Kdyby\Events\Subscriber; |
|
7
|
|
|
use Lookyman\NetteOAuth2Server\RedirectConfig; |
|
8
|
|
|
use Lookyman\NetteOAuth2Server\UI\OAuth2Presenter; |
|
9
|
|
|
use Nette\Application\AbortException; |
|
10
|
|
|
use Nette\Application\Application; |
|
11
|
|
|
use Nette\Application\IPresenter; |
|
12
|
|
|
use Nette\Application\UI\Presenter; |
|
13
|
|
|
use Nette\InvalidStateException; |
|
14
|
|
|
use Nette\Security\User; |
|
15
|
|
|
|
|
16
|
|
|
class LoginSubscriber implements Subscriber |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var IPresenter|null |
|
20
|
|
|
*/ |
|
21
|
|
|
private $presenter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $priority; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RedirectConfig |
|
30
|
|
|
*/ |
|
31
|
|
|
private $redirectConfig; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param RedirectConfig $redirectConfig |
|
35
|
|
|
* @param int $priority |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(RedirectConfig $redirectConfig, int $priority = 0) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->redirectConfig = $redirectConfig; |
|
40
|
|
|
$this->priority = $priority; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Application $application |
|
45
|
|
|
* @param IPresenter $presenter |
|
46
|
|
|
*/ |
|
47
|
|
|
public function onPresenter(Application $application, IPresenter $presenter) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->presenter = $presenter; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param User $user |
|
54
|
|
|
* @throws InvalidStateException |
|
55
|
|
|
* @throws AbortException |
|
56
|
|
|
*/ |
|
57
|
|
|
public function onLoggedIn(User $user) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->presenter) { |
|
60
|
|
|
throw new InvalidStateException('Presenter not set'); |
|
61
|
|
|
} |
|
62
|
|
|
if ($this->presenter instanceof Presenter && $this->presenter->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest) { |
|
63
|
|
|
$this->presenter->redirect(...$this->redirectConfig->getApproveDestination()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getSubscribedEvents() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
Application::class . '::onPresenter', |
|
74
|
|
|
User::class . '::onLoggedIn' => [ |
|
75
|
|
|
['onLoggedIn', $this->priority], |
|
76
|
|
|
], |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.