|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use App\Form\LoginForm; |
|
8
|
|
|
use App\User\User; |
|
9
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Yiisoft\Auth\IdentityRepositoryInterface; |
|
13
|
|
|
use Yiisoft\Http\Method; |
|
14
|
|
|
use Yiisoft\Http\Status; |
|
15
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
16
|
|
|
use Yiisoft\Translator\TranslatorInterface; |
|
17
|
|
|
use Yiisoft\User\CurrentUser; |
|
18
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
|
19
|
|
|
use Yiisoft\Yii\View\ViewRenderer; |
|
20
|
|
|
|
|
21
|
|
|
class AuthController |
|
22
|
|
|
{ |
|
23
|
|
|
private ResponseFactoryInterface $responseFactory; |
|
24
|
|
|
private UrlGeneratorInterface $urlGenerator; |
|
25
|
|
|
private ViewRenderer $viewRenderer; |
|
26
|
|
|
private CurrentUser $currentUser; |
|
27
|
|
|
|
|
28
|
5 |
|
public function __construct( |
|
29
|
|
|
ResponseFactoryInterface $responseFactory, |
|
30
|
|
|
ViewRenderer $viewRenderer, |
|
31
|
|
|
UrlGeneratorInterface $urlGenerator, |
|
32
|
|
|
CurrentUser $currentUser |
|
33
|
|
|
) { |
|
34
|
5 |
|
$this->responseFactory = $responseFactory; |
|
35
|
5 |
|
$this->urlGenerator = $urlGenerator; |
|
36
|
5 |
|
$this->viewRenderer = $viewRenderer->withControllerName('auth'); |
|
37
|
5 |
|
$this->currentUser = $currentUser; |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
public function login( |
|
41
|
|
|
IdentityRepositoryInterface $identityRepository, |
|
42
|
|
|
LoginForm $loginForm, |
|
43
|
|
|
ServerRequestInterface $request, |
|
44
|
|
|
TranslatorInterface $translator, |
|
45
|
|
|
ValidatorInterface $validator |
|
46
|
|
|
): ResponseInterface { |
|
47
|
5 |
|
if (!$this->currentUser->isGuest()) { |
|
48
|
|
|
return $this->redirectToMain(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @var array */ |
|
52
|
5 |
|
$body = $request->getParsedBody(); |
|
53
|
5 |
|
$error = null; |
|
54
|
|
|
|
|
55
|
|
|
if ( |
|
56
|
5 |
|
$request->getMethod() === Method::POST |
|
57
|
4 |
|
&& $loginForm->load($body) |
|
|
|
|
|
|
58
|
4 |
|
&& $validator->validate($loginForm)->isValid() |
|
59
|
|
|
) { |
|
60
|
|
|
/** @var User $identity */ |
|
61
|
3 |
|
$identity = $identityRepository->findByLogin($loginForm->getAttributeValue('login')); |
|
62
|
|
|
|
|
63
|
3 |
|
if ($identity === null || !$identity->validatePassword($loginForm->getAttributeValue('password'))) { |
|
|
|
|
|
|
64
|
2 |
|
$loginForm->getFormErrors()->addError('password', $translator->translate('Invalid login or password')); |
|
65
|
1 |
|
} elseif ($this->currentUser->login($identity)) { |
|
66
|
1 |
|
return $this->redirectToMain(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
5 |
|
return $this->viewRenderer->render( |
|
71
|
5 |
|
'login', |
|
72
|
|
|
[ |
|
73
|
5 |
|
'body' => $body, |
|
74
|
5 |
|
'formModel' => $loginForm, |
|
75
|
5 |
|
'error' => $error, |
|
76
|
|
|
] |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function logout(): ResponseInterface |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->currentUser->logout(); |
|
83
|
|
|
|
|
84
|
1 |
|
return $this->redirectToMain(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
private function redirectToMain(): ResponseInterface |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->responseFactory->createResponse(Status::FOUND) |
|
90
|
1 |
|
->withHeader( |
|
91
|
1 |
|
'Location', |
|
92
|
1 |
|
$this->urlGenerator->generate('site/index') |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|