|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Admin\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Admin\Service\AdminUserService; |
|
8
|
|
|
use Std\AbstractController; |
|
9
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
10
|
|
|
use Zend\Expressive\Router\RouterInterface as Router; |
|
11
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface as Template; |
|
12
|
|
|
use Zend\Session\SessionManager; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AuthController. |
|
16
|
|
|
* Deals with handlers which work with admin user authentication. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class AuthController extends AbstractController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Router |
|
22
|
|
|
*/ |
|
23
|
|
|
private $router; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Template |
|
27
|
|
|
*/ |
|
28
|
|
|
private $template; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SessionManager |
|
32
|
|
|
*/ |
|
33
|
|
|
private $session; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var AdminUserService |
|
37
|
|
|
*/ |
|
38
|
|
|
private $adminUserService; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* AuthController constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Router $router router |
|
44
|
|
|
* @param Template $template template engine |
|
45
|
|
|
* @param SessionManager $session session manager |
|
46
|
|
|
* @param AdminUserService $adminUserService admin user service |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
49
|
|
|
Router $router, |
|
50
|
|
|
Template $template, |
|
51
|
|
|
SessionManager $session, |
|
52
|
|
|
AdminUserService $adminUserService |
|
53
|
|
|
) { |
|
54
|
|
|
$this->router = $router; |
|
55
|
|
|
$this->template = $template; |
|
56
|
|
|
$this->session = $session; |
|
57
|
|
|
$this->adminUserService = $adminUserService; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Performs session check and redirects user to appropriate page or displays login page. |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function login($error = false): \Psr\Http\Message\ResponseInterface |
|
66
|
|
|
{ |
|
67
|
|
View Code Duplication |
if ($this->session->getStorage()->user) { |
|
|
|
|
|
|
68
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return new HtmlResponse($this->template->render('admin::login', ['layout' => false, 'error' => $error])); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Performs user credentials check and registers admin session if valid. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
public function loginHandle(): \Psr\Http\Message\ResponseInterface |
|
80
|
|
|
{ |
|
81
|
|
View Code Duplication |
if ($this->session->getStorage()->user) { |
|
|
|
|
|
|
82
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$data = $this->request->getParsedBody(); |
|
86
|
|
|
$email = isset($data['email']) ? $data['email'] : null; |
|
87
|
|
|
$password = isset($data['password']) ? $data['password'] : null; |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
$this->session->getStorage()->user = $this->adminUserService->loginUser($email, $password); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin')); |
|
93
|
|
|
} catch (\Exception $e) { |
|
94
|
|
|
return $this->login($e->getMessage()); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
//@todo set $e->getMessage() to flash messanger and print messages in next page |
|
97
|
|
|
//return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin')); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function logout(): \Psr\Http\Message\ResponseInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$this->session->getStorage()->clear('user'); |
|
107
|
|
|
|
|
108
|
|
|
return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin')); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.