|
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\ActionHelper; |
|
12
|
|
|
use Platine\Framework\Http\Action\BaseAction; |
|
13
|
|
|
use Platine\Framework\Http\Response\RedirectResponse; |
|
14
|
|
|
use Platine\Http\ResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @class AuthAction |
|
18
|
|
|
* @package Platine\App\Http\Action\User |
|
19
|
|
|
* @template T |
|
20
|
|
|
* @extends BaseAction<T> |
|
21
|
|
|
*/ |
|
22
|
|
|
class AuthAction extends BaseAction |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Create new instance |
|
26
|
|
|
* @param AuthenticationInterface $authentication |
|
27
|
|
|
* @param ActionHelper<T> $actionHelper |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
protected AuthenticationInterface $authentication, |
|
31
|
|
|
ActionHelper $actionHelper, |
|
32
|
|
|
) { |
|
33
|
|
|
parent::__construct($actionHelper); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function respond(): ResponseInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$this->setView('user/login'); |
|
42
|
|
|
$request = $this->request; |
|
43
|
|
|
$param = $this->param; |
|
44
|
|
|
|
|
45
|
|
|
$formParam = new AuthParam($param->posts()); |
|
46
|
|
|
$this->addContext('param', $formParam); |
|
47
|
|
|
|
|
48
|
|
|
if ($request->getMethod() === 'GET') { |
|
49
|
|
|
return $this->viewResponse(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$validator = new AuthValidator($formParam, $this->lang); |
|
53
|
|
|
if ($validator->validate() === false) { |
|
54
|
|
|
$this->addContext('errors', $validator->getErrors()); |
|
55
|
|
|
|
|
56
|
|
|
return $this->viewResponse(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$username = $formParam->getUsername(); |
|
60
|
|
|
$password = $formParam->getPassword(); |
|
61
|
|
|
|
|
62
|
|
|
$credentials = [ |
|
63
|
|
|
'username' => $username, |
|
64
|
|
|
'password' => $password, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$this->authentication->login($credentials); |
|
69
|
|
|
} catch (AuthenticationException $ex) { |
|
70
|
|
|
$this->logger->error('Authentication error: {error}', [ |
|
71
|
|
|
'error' => $ex->getMessage() |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
$this->flash->setError('Authentication error. Please check your login and/or password.'); |
|
75
|
|
|
|
|
76
|
|
|
return $this->viewResponse(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$returnUrl = $this->routeHelper->generateUrl('home'); |
|
80
|
|
|
if ($param->get('next')) { |
|
81
|
|
|
$returnUrl = $param->get('next'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new RedirectResponse($returnUrl); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|