1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Platine\App\Http\Action\User; |
6
|
|
|
|
7
|
|
|
use Platine\App\Param\AuthParam; |
8
|
|
|
use Platine\App\Validator\AuthValidator; |
9
|
|
|
use Platine\Framework\Auth\AuthenticationInterface; |
10
|
|
|
use Platine\Framework\Auth\Exception\AuthenticationException; |
11
|
|
|
use Platine\Framework\Helper\Flash; |
12
|
|
|
use Platine\Framework\Http\RequestData; |
13
|
|
|
use Platine\Framework\Http\Response\RedirectResponse; |
14
|
|
|
use Platine\Framework\Http\Response\TemplateResponse; |
15
|
|
|
use Platine\Framework\Http\RouteHelper; |
16
|
|
|
use Platine\Http\Handler\RequestHandlerInterface; |
17
|
|
|
use Platine\Http\ResponseInterface; |
18
|
|
|
use Platine\Http\ServerRequestInterface; |
19
|
|
|
use Platine\Lang\Lang; |
20
|
|
|
use Platine\Logger\LoggerInterface; |
21
|
|
|
use Platine\Template\Template; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @class AuthAction |
25
|
|
|
* @package Platine\App\Http\Action\User |
26
|
|
|
*/ |
27
|
|
|
class AuthAction implements RequestHandlerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The AuthenticationInterface instance |
31
|
|
|
* @var AuthenticationInterface |
32
|
|
|
*/ |
33
|
|
|
protected AuthenticationInterface $authentication; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The Flash instance |
37
|
|
|
* @var Flash |
38
|
|
|
*/ |
39
|
|
|
protected Flash $flash; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The RouteHelper instance |
43
|
|
|
* @var RouteHelper |
44
|
|
|
*/ |
45
|
|
|
protected RouteHelper $routeHelper; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The Lang instance |
49
|
|
|
* @var Lang |
50
|
|
|
*/ |
51
|
|
|
protected Lang $lang; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The LoggerInterface instance |
55
|
|
|
* @var LoggerInterface |
56
|
|
|
*/ |
57
|
|
|
protected LoggerInterface $logger; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The Template instance |
61
|
|
|
* @var Template |
62
|
|
|
*/ |
63
|
|
|
protected Template $template; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create new instance |
69
|
|
|
* @param AuthenticationInterface $authentication |
70
|
|
|
* @param Flash $flash |
71
|
|
|
* @param RouteHelper $routeHelper |
72
|
|
|
* @param Lang $lang |
73
|
|
|
* @param LoggerInterface $logger |
74
|
|
|
* @param Template $template |
75
|
|
|
*/ |
76
|
|
|
public function __construct( |
77
|
|
|
AuthenticationInterface $authentication, |
78
|
|
|
Flash $flash, |
79
|
|
|
RouteHelper $routeHelper, |
80
|
|
|
Lang $lang, |
81
|
|
|
LoggerInterface $logger, |
82
|
|
|
Template $template |
83
|
|
|
) { |
84
|
|
|
$this->authentication = $authentication; |
85
|
|
|
$this->flash = $flash; |
86
|
|
|
$this->routeHelper = $routeHelper; |
87
|
|
|
$this->lang = $lang; |
88
|
|
|
$this->logger = $logger; |
89
|
|
|
$this->template = $template; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
96
|
|
|
{ |
97
|
|
|
$context = []; |
98
|
|
|
$param = new RequestData($request); |
99
|
|
|
|
100
|
|
|
$formParam = new AuthParam($param->posts()); |
101
|
|
|
$context['param'] = $formParam; |
102
|
|
|
|
103
|
|
|
if ($request->getMethod() === 'GET') { |
104
|
|
|
return new TemplateResponse( |
105
|
|
|
$this->template, |
106
|
|
|
'user/login', |
107
|
|
|
$context |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$validator = new AuthValidator($formParam, $this->lang); |
112
|
|
|
if ($validator->validate() === false) { |
113
|
|
|
$context['errors'] = $validator->getErrors(); |
114
|
|
|
|
115
|
|
|
return new TemplateResponse( |
116
|
|
|
$this->template, |
117
|
|
|
'user/login', |
118
|
|
|
$context |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$username = $formParam->getUsername(); |
123
|
|
|
$password = $formParam->getPassword(); |
124
|
|
|
|
125
|
|
|
$credentials = [ |
126
|
|
|
'username' => $username, |
127
|
|
|
'password' => $password, |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$this->authentication->login($credentials); |
132
|
|
|
} catch (AuthenticationException $ex) { |
133
|
|
|
$this->logger->error('Authentication error: {error}', [ |
134
|
|
|
'error' => $ex->getMessage() |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
$this->flash->setError('Authentication error. Please check your login and/or password.'); |
138
|
|
|
|
139
|
|
|
return new TemplateResponse( |
140
|
|
|
$this->template, |
141
|
|
|
'user/login', |
142
|
|
|
$context |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$returnUrl = $this->routeHelper->generateUrl('home'); |
147
|
|
|
if ($param->get('next')) { |
148
|
|
|
$returnUrl = $param->get('next'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new RedirectResponse($returnUrl); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|