1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Authentication\UI; |
6
|
|
|
|
7
|
|
|
use Nette\Security\User; |
8
|
|
|
use Nette\Application\ForbiddenRequestException; |
9
|
|
|
use SixtyEightPublishers\User\Common\Exception\RuntimeException; |
10
|
|
|
use SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactoryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Use in Presenter! |
14
|
|
|
* |
15
|
|
|
* For link creation use: |
16
|
|
|
* |
17
|
|
|
* <code> |
18
|
|
|
* $this->link(':My:Logout:Presenter', [ |
19
|
|
|
* '_sec' => $csrfTokenFactory->create(My\Logout\Presenter::class), |
20
|
|
|
* ]); |
21
|
|
|
* </code> |
22
|
|
|
* |
23
|
|
|
* |
24
|
|
|
* @method User getUser() |
25
|
|
|
* @method mixed getParameter($key) |
26
|
|
|
* @method |
27
|
|
|
*/ |
28
|
|
|
trait LogoutPresenterTrait |
29
|
|
|
{ |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $tokenName = '_sec'; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $tokenComponent = __CLASS__; |
35
|
|
|
|
36
|
|
|
/** @var NULL|\SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactoryInterface */ |
37
|
|
|
private $csrfTokenFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactoryInterface $csrfTokenFactory |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*@internal |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public function injectCsrfTokenFactory(CsrfTokenFactoryInterface $csrfTokenFactory): void |
47
|
|
|
{ |
48
|
|
|
$this->csrfTokenFactory = $csrfTokenFactory; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
* |
54
|
|
|
* @throws \Nette\Application\ForbiddenRequestException |
55
|
|
|
*/ |
56
|
|
|
public function startup(): void |
57
|
|
|
{ |
58
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
59
|
|
|
parent::startup(); |
60
|
|
|
|
61
|
|
|
$user = $this->getUser(); |
62
|
|
|
|
63
|
|
|
if (!$user->isLoggedIn()) { |
64
|
|
|
$this->triggerUserNotLoggedIn(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->getParameter($this->tokenName) !== $this->csrfTokenFactory->create($this->tokenComponent)) { |
68
|
|
|
$this->triggerInvalidToken(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$user->logout(); |
72
|
|
|
$this->triggerLoggedOut(); |
73
|
|
|
|
74
|
|
|
throw new RuntimeException(sprintf( |
75
|
|
|
'Method %s::triggerLoggedOut() must redirects when user is logged in.', |
76
|
|
|
__CLASS__ |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Do redirect in this method, you can also add flash messages etc. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
* @throws \Nette\Application\AbortException |
85
|
|
|
*/ |
86
|
|
|
abstract protected function triggerLoggedOut(): void; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Use can override the default behavior |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
* @throws \Nette\Application\ForbiddenRequestException |
93
|
|
|
*/ |
94
|
|
|
protected function triggerUserNotLoggedIn(): void |
95
|
|
|
{ |
96
|
|
|
throw new ForbiddenRequestException(''); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Use can override the default behavior |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
* @throws \Nette\Application\ForbiddenRequestException |
104
|
|
|
*/ |
105
|
|
|
protected function triggerInvalidToken(): void |
106
|
|
|
{ |
107
|
|
|
throw new ForbiddenRequestException(''); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|