-
Notifications
You must be signed in to change notification settings - Fork 5
/
Module.php
188 lines (165 loc) · 4.91 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\twofa;
use humhub\components\Module as BaseModule;
use humhub\libs\Html;
use humhub\modules\admin\models\forms\UserEditForm;
use humhub\modules\twofa\drivers\EmailDriver;
use humhub\modules\twofa\drivers\GoogleAuthenticatorDriver;
use humhub\modules\twofa\helpers\TwofaHelper;
use humhub\modules\twofa\helpers\TwofaUrl;
use humhub\modules\user\models\Group;
use Yii;
/**
* @inheritdoc
*/
class Module extends BaseModule
{
/**
* @inheritdoc
*/
public $resourcesPath = 'resources';
/**
* @var string Default Driver, used for Users from enforced Groups by default
*/
public $defaultDriver = EmailDriver::class;
/**
* @var array Drivers
*/
public $drivers = [
EmailDriver::class,
GoogleAuthenticatorDriver::class,
];
/**
* @inheritdoc
*/
public function getConfigUrl()
{
return TwofaUrl::toConfig();
}
/**
* @return bool Check if current page is already URL of 2fa
*/
public function isTwofaCheckUrl()
{
return Yii::$app->getRequest()->getUrl() === TwofaUrl::toCheck();
}
/**
* Get available drivers options for the 2fa module settings
*
* @param array|null Init options(Key - Driver class name, Value - Drive name), used to init None option and/or forced/default Driver
* @param bool true - to load only enabled drivers, false - to load all implemented drivers for the module
* @return array
*/
public function getDriversOptions($driversOptions = [], $onlyEnabled = false)
{
$drivers = $onlyEnabled ? $this->getEnabledDrivers() : $this->drivers;
foreach ($drivers as $driverClassName) {
$driversOptions[$driverClassName] = TwofaHelper::getDriverByClassName($driverClassName)->name;
}
return $driversOptions;
}
/**
* Callback function to render checkbox element of Driver on backoffice module config form
*
* @param $index
* @param $label
* @param $name
* @param $checked
* @param $value
* @return string
*/
public function renderDriverCheckboxItem($index, $label, $name, $checked, $value)
{
$options = [
'label' => Html::encode($label),
'value' => $value,
'disabled' => !TwofaHelper::getDriverByClassName($value)->isInstalled(),
];
return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
}
/**
* Get enabled drivers
*
* @param bool $checkActive
* @return array
*/
public function getEnabledDrivers(bool $checkActive = true): array
{
$enabledDrivers = $this->settings->get('enabledDrivers', implode(',', $this->drivers));
if (empty($enabledDrivers)) {
return [];
}
// Check if each enabled Driver is properly installed:
$enabledDrivers = explode(',', $enabledDrivers);
foreach ($enabledDrivers as $d => $enabledDriverClassName) {
$enabledDriver = TwofaHelper::getDriverByClassName($enabledDriverClassName);
if (!$enabledDriver->isInstalled() || ($checkActive && !$enabledDriver->isActive())) {
unset($enabledDrivers[$d]);
}
}
return $enabledDrivers;
}
/**
* Get length of verifying code
*
* @return int
*/
public function getCodeLength()
{
return intval($this->settings->get('codeLength', 6));
}
/**
* Get length in days of remember me option
*
* @return int
*/
public function getRememberMeDays()
{
return $this->settings->get('rememberMeDays', 7);
}
/**
* Get groups options for the 2fa module settings
*
* @return array
*/
public function getGroupsOptions()
{
$groups = Group::find()->all();
return UserEditForm::getGroupItems($groups);
}
/**
* Get enforced groups to 2fa E-mail driver
*
* @return array
*/
public function getEnforcedGroups()
{
$enforcedGroups = $this->settings->get('enforcedGroups');
if ($enforcedGroups === null) {
// Enforce all Administrative Groups by default
return Group::find()->select('id')->where(['is_admin_group' => '1'])->column();
}
return empty($enforcedGroups) ? [] : explode(',', $enforcedGroups);
}
/**
* Get default method for the mandatory/enforced groups
*
* @return string
*/
public function getEnforcedMethod(): string
{
return $this->settings->get('enforcedMethod', $this->defaultDriver);
}
/**
* @return mixed
*/
public function getTrustedNetworks()
{
return json_decode($this->settings->get('trustedNetworks', '[]'));
}
}