1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\UI; |
5
|
|
|
|
6
|
|
|
use Lookyman\NetteOAuth2Server\Psr7\ApplicationPsr7ResponseInterface; |
7
|
|
|
use Lookyman\NetteOAuth2Server\RedirectConfig; |
8
|
|
|
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer; |
9
|
|
|
use Lookyman\NetteOAuth2Server\User\UserEntity; |
10
|
|
|
use Nette\Application\AbortException; |
11
|
|
|
use Nette\Application\IResponse; |
12
|
|
|
use Nette\Http\Session; |
13
|
|
|
use Nette\Http\SessionSection; |
14
|
|
|
use Nette\Security\User; |
15
|
|
|
use Nextras\Application\UI\SecuredLinksPresenterTrait; |
16
|
|
|
|
17
|
|
|
trait ApprovePresenterTrait |
18
|
|
|
{ |
19
|
|
|
use SecuredLinksPresenterTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var IApproveControlFactory |
23
|
|
|
* @inject |
24
|
|
|
*/ |
25
|
|
|
public $approveControlFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var IAuthorizationRequestSerializer |
29
|
|
|
* @inject |
30
|
|
|
*/ |
31
|
|
|
public $authorizationRequestSerializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RedirectConfig |
35
|
|
|
* @inject |
36
|
|
|
*/ |
37
|
|
|
public $redirectConfig; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ApproveControl |
41
|
|
|
*/ |
42
|
|
|
protected function createComponentApprove(): ApproveControl |
43
|
|
|
{ |
44
|
|
|
$control = $this->approveControlFactory->create(); |
45
|
|
|
|
46
|
|
|
$control->onAuthorizationComplete[] = function () { |
47
|
|
|
$this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->remove(); |
|
|
|
|
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
$control->onResponse[] = function (ApplicationPsr7ResponseInterface $response) { |
51
|
|
|
$this->sendResponse($response); |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
$control->onAnchor[] = function (ApproveControl $control) { |
55
|
|
|
if (!$this->getUser()->isLoggedIn()) { |
56
|
|
|
$this->redirect(...$this->redirectConfig->getLoginDestination()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$data = $this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest; |
60
|
|
|
$authorizationRequest = $data ? $this->authorizationRequestSerializer->unserialize($data) : null; |
61
|
|
|
if (!$authorizationRequest) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!$authorizationRequest->getUser()) { |
66
|
|
|
$authorizationRequest->setUser(new UserEntity($this->getUser()->getId())); |
67
|
|
|
} |
68
|
|
|
$control->setAuthorizationRequest($authorizationRequest); |
69
|
|
|
|
70
|
|
|
if ($authorizationRequest->isAuthorizationApproved()) { |
71
|
|
|
$control->completeAuthorizationRequest(); |
72
|
|
|
} |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
return $control; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|null $namespace |
80
|
|
|
* @return Session|SessionSection |
81
|
|
|
*/ |
82
|
|
|
abstract public function getSession($namespace = null); |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return User |
86
|
|
|
*/ |
87
|
|
|
abstract public function getUser(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $code [optional] |
91
|
|
|
* @param string|null $destination |
92
|
|
|
* @param array|mixed $args |
93
|
|
|
* @throws AbortException |
94
|
|
|
*/ |
95
|
|
|
abstract public function redirect($code, $destination = null, $args = []); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param IResponse $response |
99
|
|
|
* @throws AbortException |
100
|
|
|
*/ |
101
|
|
|
abstract public function sendResponse(IResponse $response); |
102
|
|
|
} |
103
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: