Uh oh!
There was an error while loading. Please reload this page.
forked from humhub/twofa
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
Latest commit
178 lines (156 loc) · 4.68 KB
/
Copy pathModule.php
File metadata and controls
178 lines (156 loc) · 4.68 KB
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2020 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespacehumhub\modules\twofa;
usehumhub\components\ModuleasBaseModule;
usehumhub\libs\Html;
usehumhub\modules\admin\models\forms\UserEditForm;
usehumhub\modules\twofa\drivers\EmailDriver;
usehumhub\modules\twofa\drivers\GoogleAuthenticatorDriver;
usehumhub\modules\twofa\helpers\TwofaHelper;
usehumhub\modules\twofa\helpers\TwofaUrl;
usehumhub\modules\user\models\Group;
useYii;
/**
* @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
*/
publicfunctiongetConfigUrl()
{
return TwofaUrl::toConfig();
}
/**
* @return bool Check if current page is already URL of 2fa
*/
publicfunctionisTwofaCheckUrl()
{
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 boolean true - to load only enabled drivers, false - to load all implemented drivers for the module
* @return array
*/
publicfunctiongetDriversOptions($driversOptions = [], $onlyEnabled = false)
{
$drivers = $onlyEnabled ? $this->getEnabledDrivers() : $this->drivers;
foreach ($driversas$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
*/
publicfunctionrenderDriverCheckboxItem($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
*/
publicfunctiongetEnabledDrivers(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 ($enabledDriversas$d => $enabledDriverClassName) {
$enabledDriver = TwofaHelper::getDriverByClassName($enabledDriverClassName);
if (!$enabledDriver->isInstalled() || ($checkActive && !$enabledDriver->isActive())) {
unset($enabledDrivers[$d]);
}
}
return$enabledDrivers;
}
/**
* Get length of verifying code
*
* @return integer
*/
publicfunctiongetCodeLength()
{
returnintval($this->settings->get('codeLength', 6));
}
/**
* Get length in days of remember me option
*
* @return integer
*/
publicfunctiongetRememberMeDays()
{
return$this->settings->get('rememberMeDays', 7);
}
/**
* Get groups options for the 2fa module settings
*
* @return array
*/
publicfunctiongetGroupsOptions()
{
$groups = Group::find()->all();
return UserEditForm::getGroupItems($groups);
}
/**
* Get enforced groups to 2fa E-mail driver
*
* @return array
*/
publicfunctiongetEnforcedGroups()
{
$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();
}
returnempty($enforcedGroups) ? [] : explode(',', $enforcedGroups);
}
/**
* @return mixed
*/
publicfunctiongetTrustedNetworks()
{
returnjson_decode($this->settings->get('trustedNetworks', '[]'));
}
}