1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Lookyman\Nette\OAuth2\Server; |
6
|
|
|
|
7
|
|
|
use Nette\Application\UI\Component; |
8
|
|
|
use Nette\InvalidStateException; |
9
|
|
|
use Nette\SmartObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method void onBeforeApproveRedirect(RedirectConfig $redirectConfig) |
13
|
|
|
* @method void onBeforeLoginRedirect(RedirectConfig $redirectConfig) |
14
|
|
|
*/ |
15
|
|
|
final class RedirectConfig |
16
|
|
|
{ |
17
|
|
|
use SmartObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable[] |
21
|
|
|
*/ |
22
|
|
|
public $onBeforeApproveRedirect = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var callable[] |
26
|
|
|
*/ |
27
|
|
|
public $onBeforeLoginRedirect = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $approveDestination; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $loginDestination; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|array|null $approveDestination |
41
|
|
|
* @param string|array|null $loginDestination |
42
|
|
|
*/ |
43
|
|
|
public function __construct($approveDestination, $loginDestination) |
44
|
|
|
{ |
45
|
|
|
$this->setApproveDestination($approveDestination); |
46
|
|
|
$this->setLoginDestination($loginDestination); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string|array|null $approveDestination |
51
|
|
|
*/ |
52
|
|
|
public function setApproveDestination($approveDestination) |
53
|
|
|
{ |
54
|
|
|
$this->approveDestination = (array) $approveDestination; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function redirectToApproveDestination(Component $component) |
58
|
|
|
{ |
59
|
|
|
if (empty($this->approveDestination)) { |
60
|
|
|
throw new InvalidStateException('Approve destination not set'); |
61
|
|
|
} |
62
|
|
|
$this->onBeforeApproveRedirect($this); |
63
|
|
|
$component->redirect(...$this->approveDestination); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string|array|null $loginDestination |
68
|
|
|
*/ |
69
|
|
|
public function setLoginDestination($loginDestination) |
70
|
|
|
{ |
71
|
|
|
$this->loginDestination = (array) $loginDestination; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function redirectToLoginDestination(Component $component) |
75
|
|
|
{ |
76
|
|
|
if (empty($this->loginDestination)) { |
77
|
|
|
throw new InvalidStateException('Login destination not set'); |
78
|
|
|
} |
79
|
|
|
$this->onBeforeLoginRedirect($this); |
80
|
|
|
$component->redirect(...$this->loginDestination); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|