|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A two factor authentication module that protects both the admin and customer logins |
|
4
|
|
|
* Copyright (C) 2017 Ross Mitchell |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of Rossmitchell/Twofactor. |
|
7
|
|
|
* |
|
8
|
|
|
* Rossmitchell/Twofactor is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU General Public License as published by |
|
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU General Public License |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Rossmitchell\Twofactor\Controller\Customerlogin; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Customer\Api\Data\CustomerInterface; |
|
25
|
|
|
use Magento\Framework\App\Action\Action; |
|
26
|
|
|
use Magento\Framework\App\Action\Context; |
|
27
|
|
|
use Magento\Framework\Controller\Result\Redirect; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Customer; |
|
31
|
|
|
use Rossmitchell\Twofactor\Model\Urls\Fetcher; |
|
32
|
|
|
|
|
33
|
|
|
abstract class AbstractController extends Action |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CustomerAdmin |
|
38
|
|
|
*/ |
|
39
|
|
|
private $customerAdmin; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Customer |
|
42
|
|
|
*/ |
|
43
|
|
|
private $customerGetter; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var Redirect |
|
46
|
|
|
*/ |
|
47
|
|
|
private $redirectAction; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var CustomerInterface|false |
|
50
|
|
|
*/ |
|
51
|
|
|
private $customerModel; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var IsUsingTwoFactor |
|
54
|
|
|
*/ |
|
55
|
|
|
private $isUsingTwoFactor; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var Fetcher |
|
58
|
|
|
*/ |
|
59
|
|
|
private $fetcher; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* AbstractController constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Context $context |
|
65
|
|
|
* @param CustomerAdmin $customerAdmin |
|
66
|
|
|
* @param Customer $customerGetter |
|
67
|
|
|
* @param Fetcher $fetcher |
|
68
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
69
|
|
|
*/ |
|
70
|
6 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
71
|
|
|
Context $context, |
|
72
|
|
|
CustomerAdmin $customerAdmin, |
|
73
|
|
|
Customer $customerGetter, |
|
74
|
|
|
Fetcher $fetcher, |
|
75
|
|
|
IsUsingTwoFactor $isUsingTwoFactor |
|
76
|
|
|
) { |
|
77
|
6 |
|
parent::__construct($context); |
|
78
|
6 |
|
$this->customerAdmin = $customerAdmin; |
|
79
|
6 |
|
$this->customerGetter = $customerGetter; |
|
80
|
6 |
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
81
|
6 |
|
$this->fetcher = $fetcher; |
|
82
|
6 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* The controllers should only be run if the following conditions are met: |
|
86
|
|
|
* |
|
87
|
|
|
* - Two Factor Authentication is enabled for the store |
|
88
|
|
|
* - There is a customer |
|
89
|
|
|
* - That customer is using Two Factor Authentication |
|
90
|
|
|
* |
|
91
|
|
|
* If all of these conditions are met, then the method will return true, otherwise a redirect will be created and |
|
92
|
|
|
* the method will return false |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function shouldActionBeRun() |
|
97
|
|
|
{ |
|
98
|
6 |
|
if ($this->isEnabled() === false) { |
|
99
|
1 |
|
$this->redirectAction = $this->handleDisabled(); |
|
100
|
|
|
|
|
101
|
1 |
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
5 |
|
if ($this->getCustomer() === false) { |
|
105
|
1 |
|
$this->redirectAction = $this->handleMissingCustomer(); |
|
106
|
|
|
|
|
107
|
1 |
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
4 |
|
if ($this->isCustomerUsingTwoFactor() === false) { |
|
111
|
1 |
|
$this->redirectAction = $this->handleNonOptInCustomer(); |
|
112
|
|
|
|
|
113
|
1 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns the redirect action generated by the shouldActionBeRun method |
|
121
|
|
|
* |
|
122
|
|
|
* @return Redirect |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function getRedirectAction() |
|
125
|
|
|
{ |
|
126
|
3 |
|
return $this->redirectAction; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Used to create a redirect action to a specific page |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $path - The path the the customer should be redirected to |
|
133
|
|
|
* |
|
134
|
|
|
* @return Redirect |
|
135
|
|
|
*/ |
|
136
|
5 |
|
public function redirect($path) |
|
137
|
|
|
{ |
|
138
|
5 |
|
$redirect = $this->resultRedirectFactory->create(); |
|
139
|
5 |
|
$redirect->setPath($path); |
|
140
|
|
|
|
|
141
|
5 |
|
return $redirect; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Used to fetch the customer from the session |
|
146
|
|
|
* |
|
147
|
|
|
* @return CustomerInterface|false |
|
148
|
|
|
*/ |
|
149
|
5 |
|
public function getCustomer() |
|
150
|
|
|
{ |
|
151
|
5 |
|
if (null === $this->customerModel) { |
|
152
|
5 |
|
$this->customerModel = $this->customerGetter->getCustomer(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
5 |
|
return $this->customerModel; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return Fetcher |
|
160
|
|
|
*/ |
|
161
|
3 |
|
public function getUrlFetcher() |
|
162
|
|
|
{ |
|
163
|
3 |
|
return $this->fetcher; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* A wrapper method around the Customer::isTwoFactorEnabled method |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
6 |
|
private function isEnabled() |
|
172
|
|
|
{ |
|
173
|
6 |
|
return ($this->customerAdmin->isTwoFactorEnabled() === true); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* A wrapper method around the IsUsingTwoFactor::getValue method |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
4 |
|
private function isCustomerUsingTwoFactor() |
|
182
|
|
|
{ |
|
183
|
4 |
|
$customer = $this->getCustomer(); |
|
184
|
|
|
|
|
185
|
4 |
|
return $this->isUsingTwoFactor->getValue($customer); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* If Two Factor Authentication is disabled redirect the customer to the home page |
|
190
|
|
|
* |
|
191
|
|
|
* @return Redirect |
|
192
|
|
|
*/ |
|
193
|
1 |
|
private function handleDisabled() |
|
194
|
|
|
{ |
|
195
|
1 |
|
return $this->redirect('/'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* If there isn't a customer in the session then redirect the user to the login page |
|
200
|
|
|
* |
|
201
|
|
|
* @return Redirect |
|
202
|
|
|
*/ |
|
203
|
1 |
|
private function handleMissingCustomer() |
|
204
|
|
|
{ |
|
205
|
1 |
|
$loginUrl = $this->getUrlFetcher()->getCustomerLogInUrl(); |
|
206
|
|
|
|
|
207
|
1 |
|
return $this->redirect($loginUrl); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Redirect customers that are not using Two Factor Authentication to the home page |
|
212
|
|
|
* |
|
213
|
|
|
* @return Redirect |
|
214
|
|
|
*/ |
|
215
|
1 |
|
private function handleNonOptInCustomer() |
|
216
|
|
|
{ |
|
217
|
1 |
|
return $this->redirect('/'); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
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.