|
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\Model\Urls; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Framework\UrlInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Fetcher |
|
28
|
|
|
* |
|
29
|
|
|
* This is a simple wrapper class around the URL Interface class to give more meaningful names for fetching URLs |
|
30
|
|
|
*/ |
|
31
|
|
|
class Fetcher |
|
32
|
|
|
{ |
|
33
|
|
|
const CUSTOMER_AUTHENTICATION_URL = 'twofactor/customerlogin/index'; |
|
34
|
|
|
const CUSTOMER_VERIFICATION_URL = 'twofactor/customerlogin/verify'; |
|
35
|
|
|
const CUSTOMER_ACCOUNT_URL = 'customer/account/index'; |
|
36
|
|
|
const CUSTOMER_LOGIN_URL = 'customer/account/login'; |
|
37
|
|
|
|
|
38
|
|
|
const ADMIN_AUTHENTICATION_URL = 'twofactor/adminlogin/index'; |
|
39
|
|
|
const ADMIN_VERIFICATION_URL = 'twofactor/adminlogin/verify'; |
|
40
|
|
|
const ADMIN_DASHBOARD_URL = 'admin/dashboard/index'; |
|
41
|
|
|
const ADMIN_LOGIN_URL = 'admin/index/index'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var UrlInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $url; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Fetcher constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param UrlInterface $url |
|
52
|
|
|
*/ |
|
53
|
45 |
|
public function __construct(UrlInterface $url) |
|
54
|
|
|
{ |
|
55
|
45 |
|
$this->url = $url; |
|
56
|
45 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Used to get either the admin or customer two factor authentication URL |
|
60
|
|
|
* |
|
61
|
|
|
* @param bool $forAdmin - true for the admin URL, false for the customer URL |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
22 |
View Code Duplication |
public function getAuthenticationUrl($forAdmin = false) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
22 |
|
if ($forAdmin === true) { |
|
67
|
5 |
|
return $this->getUrl(self::ADMIN_AUTHENTICATION_URL); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
17 |
|
return $this->getUrl(self::CUSTOMER_AUTHENTICATION_URL); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Used to get either the admin or customer two factor verification URL |
|
75
|
|
|
* |
|
76
|
|
|
* @param bool $forAdmin - true for the admin URL, false for the customer URL |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
19 |
View Code Duplication |
public function getVerificationUrl($forAdmin = false) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
19 |
|
if ($forAdmin === true) { |
|
82
|
4 |
|
return $this->getUrl(self::ADMIN_VERIFICATION_URL); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
15 |
|
return $this->getUrl(self::CUSTOMER_VERIFICATION_URL); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Used to get the URL for the customer account page |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function getCustomerAccountUrl() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->getUrl(self::CUSTOMER_ACCOUNT_URL); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Used to get the URL for the admin dashboard page |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function getAdminDashboardUrl() |
|
104
|
|
|
{ |
|
105
|
3 |
|
return $this->getUrl(self::ADMIN_DASHBOARD_URL); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Used to get the customer login URL |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getCustomerLogInUrl() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->getUrl(self::CUSTOMER_LOGIN_URL); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function getAdminLogInUrl() |
|
119
|
|
|
{ |
|
120
|
2 |
|
return $this->getUrl(self::ADMIN_LOGIN_URL); |
|
121
|
|
|
} |
|
122
|
|
|
/** |
|
123
|
|
|
* This is used to actually get the URL |
|
124
|
|
|
* |
|
125
|
|
|
* @param $path |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
27 |
|
private function getUrl($path) |
|
129
|
|
|
{ |
|
130
|
27 |
|
return $this->url->getUrl($path); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
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.