1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Yiisoft\Aliases\Aliases; |
10
|
|
|
use Yiisoft\Auth\IdentityRepositoryInterface; |
11
|
|
|
use Yiisoft\Http\Method; |
12
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
13
|
|
|
use Yiisoft\View\WebView; |
14
|
|
|
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface; |
15
|
|
|
use Yiisoft\Yii\Web\User\User; |
16
|
|
|
|
17
|
|
|
class AuthController extends Controller |
18
|
|
|
{ |
19
|
|
|
private LoggerInterface $logger; |
20
|
|
|
private UrlGeneratorInterface $urlGenerator; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
DataResponseFactoryInterface $responseFactory, |
24
|
|
|
Aliases $aliases, |
25
|
|
|
WebView $view, |
26
|
|
|
User $user, |
27
|
|
|
LoggerInterface $logger, |
28
|
|
|
UrlGeneratorInterface $urlGenerator |
29
|
|
|
) { |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
$this->urlGenerator = $urlGenerator; |
32
|
|
|
parent::__construct($responseFactory, $user, $aliases, $view); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function login( |
36
|
|
|
ServerRequestInterface $request, |
37
|
|
|
IdentityRepositoryInterface $identityRepository |
38
|
|
|
): ResponseInterface { |
39
|
|
|
$body = $request->getParsedBody(); |
40
|
|
|
$error = null; |
41
|
|
|
|
42
|
|
|
if ($request->getMethod() === Method::POST) { |
43
|
|
|
try { |
44
|
|
|
foreach (['login', 'password'] as $name) { |
45
|
|
|
if (empty($body[$name])) { |
46
|
|
|
throw new \InvalidArgumentException(ucfirst($name) . ' is required'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @var \App\Entity\User $identity */ |
51
|
|
|
$identity = $identityRepository->findByLogin($body['login']); |
52
|
|
|
if ($identity === null) { |
53
|
|
|
throw new \InvalidArgumentException('No such user'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$identity->validatePassword($body['password'])) { |
57
|
|
|
throw new \InvalidArgumentException('Invalid password'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($this->user->login($identity)) { |
61
|
|
|
return $this->responseFactory |
62
|
|
|
->createResponse(302) |
63
|
|
|
->withHeader( |
64
|
|
|
'Location', |
65
|
|
|
$this->urlGenerator->generate('site/index') |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \InvalidArgumentException('Unable to login'); |
70
|
|
|
} catch (\Throwable $e) { |
71
|
|
|
$this->logger->error($e); |
72
|
|
|
$error = $e->getMessage(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->render( |
77
|
|
|
'login', |
78
|
|
|
[ |
79
|
|
|
'csrf' => $request->getAttribute('csrf_token'), |
80
|
|
|
'body' => $body, |
81
|
|
|
'error' => $error, |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function logout(): ResponseInterface |
87
|
|
|
{ |
88
|
|
|
$this->user->logout(); |
89
|
|
|
|
90
|
|
|
return $this->responseFactory |
91
|
|
|
->createResponse(302) |
92
|
|
|
->withHeader( |
93
|
|
|
'Location', |
94
|
|
|
$this->urlGenerator->generate('site/index') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|