forked from humhub/twofa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
158 lines (138 loc) · 4.16 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
<?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 boolean 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
*
* @return array
*/
public function getEnabledDrivers()
{
$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) {
if (!TwofaHelper::getDriverByClassName($enabledDriverClassName)->isInstalled()) {
unset($enabledDrivers[$d]);
}
}
return $enabledDrivers;
}
/**
* Get length of verifying code
*
* @return integer
*/
public function getCodeLength()
{
return intval($this->settings->get('codeLength', 6));
}
/**
* 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);
}
}