|
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\Block\Customer\Account\Edit; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Customer\Api\Data\CustomerInterface; |
|
25
|
|
|
use Magento\Framework\View\Element\Template; |
|
26
|
|
|
use Magento\Framework\View\Element\Template\Context; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerConfig; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Customer; |
|
30
|
|
|
|
|
31
|
|
|
class UseTwoFactor extends Template |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var IsUsingTwoFactor |
|
35
|
|
|
*/ |
|
36
|
|
|
private $isUsingTwoFactor; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var Customer |
|
39
|
|
|
*/ |
|
40
|
|
|
private $customerGetter; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var CustomerConfig |
|
43
|
|
|
*/ |
|
44
|
|
|
private $customerConfig; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* UseTwoFactor constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param Context $context |
|
50
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
51
|
|
|
* @param Customer $customerGetter |
|
52
|
|
|
* @param CustomerConfig $customerConfig |
|
53
|
|
|
* @param array $data |
|
54
|
|
|
*/ |
|
55
|
4 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
56
|
|
|
Context $context, |
|
57
|
|
|
IsUsingTwoFactor $isUsingTwoFactor, |
|
58
|
|
|
Customer $customerGetter, |
|
59
|
|
|
CustomerConfig $customerConfig, |
|
60
|
|
|
array $data = [] |
|
61
|
|
|
) { |
|
62
|
4 |
|
parent::__construct($context, $data); |
|
63
|
4 |
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
64
|
4 |
|
$this->customerGetter = $customerGetter; |
|
65
|
4 |
|
$this->customerConfig = $customerConfig; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* We only want to display the block if two factor has been enabled in the admin |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function shouldBlockBeDisplayed() |
|
74
|
|
|
{ |
|
75
|
4 |
|
return ($this->customerConfig->isTwoFactorEnabled() === true); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Used to get the customer, return false if no customer is present in the session |
|
80
|
|
|
* |
|
81
|
|
|
* @return CustomerInterface|false |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function getCustomer() |
|
84
|
|
|
{ |
|
85
|
3 |
|
return $this->customerGetter->getCustomer(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Checks to see if the customer is using two factor authentication. |
|
90
|
|
|
* |
|
91
|
|
|
* This first checks if the customer has a value set for the attributes, and if so if the value is true. If both of |
|
92
|
|
|
* these checks pass the method returns true, otherwise it returns false |
|
93
|
|
|
* |
|
94
|
|
|
* @param CustomerInterface $customer |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
3 |
View Code Duplication |
public function isUsingTwoFactor(CustomerInterface $customer) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
3 |
|
if ($this->isUsingTwoFactor->hasValue($customer) === false) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
return ($this->isUsingTwoFactor->getValue($customer) === true); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* If the customer is using two factor authentication then this will return the selected snippet for use in the |
|
109
|
|
|
* options tag, if not it will return an empty string |
|
110
|
|
|
* |
|
111
|
|
|
* @param CustomerInterface $customer |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function getSelectedForYes(CustomerInterface $customer) |
|
116
|
|
|
{ |
|
117
|
3 |
|
return $this->getSelectedSnippet($customer, true); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* If the customer is not using two factor authentication then this will return the selected snippet for use in the |
|
122
|
|
|
* options tag, if they are it will return an empty string |
|
123
|
|
|
* |
|
124
|
|
|
* @param CustomerInterface $customer |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
3 |
|
public function getSelectedForNo(CustomerInterface $customer) |
|
129
|
|
|
{ |
|
130
|
3 |
|
return $this->getSelectedSnippet($customer, false); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Generates the snippet for the two getSelected methods |
|
135
|
|
|
* |
|
136
|
|
|
* @param CustomerInterface $customer |
|
137
|
|
|
* @param boolean $condition |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
3 |
|
private function getSelectedSnippet(CustomerInterface $customer, $condition) |
|
142
|
|
|
{ |
|
143
|
3 |
|
$html = ''; |
|
144
|
3 |
|
if ($this->isUsingTwoFactor($customer) === $condition) { |
|
145
|
3 |
|
$html = ' selected="selected"'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
3 |
|
return $html; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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.