-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEvents.php
More file actions
204 lines (176 loc) · 6.15 KB
/
Copy pathEvents.php
File metadata and controls
204 lines (176 loc) · 6.15 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?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\helpers\ControllerHelper;
use humhub\modules\admin\controllers\UserController as AdminUserController;
use humhub\modules\admin\grid\UserActionColumn;
use humhub\modules\admin\permissions\ManageUsers;
use humhub\modules\twofa\events\BeforeCheck;
use humhub\modules\twofa\helpers\TwofaHelper;
use humhub\modules\twofa\helpers\TwofaUrl;
use humhub\modules\ui\menu\MenuLink;
use humhub\modules\user\controllers\AuthController;
use humhub\modules\user\events\UserEvent;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\AccountMenu;
use humhub\modules\user\widgets\AccountSettingsMenu;
use Yii;
use yii\web\Controller;
class Events
{
/**
* @inheritdoc
*/
public static function onBeforeRequest()
{
try {
static::registerAutoloader();
} catch (\Throwable $e) {
Yii::error($e);
}
}
/**
* Register composer autoloader
*/
public static function registerAutoloader()
{
$autoloaderFilePath = Yii::getAlias('@twofa/vendor/autoload.php');
if (file_exists($autoloaderFilePath)) {
require $autoloaderFilePath;
}
}
/**
* Check if current User has been verified by 2fa if it is required
*
* @param $event
* @return false|\yii\console\Response|\yii\web\Response
*/
public static function onBeforeAction($event)
{
if (Yii::$app->user->mustChangePassword()) {
return false;
}
/** @var Controller $controller */
$controller = $event->sender;
if (self::isImpersonateAction($controller)) {
Yii::$app->session->set('twofa.switchedUserId', Yii::$app->user->id);
}
if (
$controller->module->id === 'fcm-push'
&& $controller->id === 'token'
&& $controller->action->id === 'update'
) {
return false;
}
$beforeVerifying = new BeforeCheck();
Yii::$app->trigger($beforeVerifying->name, $beforeVerifying);
if (!$beforeVerifying->handled && TwofaHelper::isVerifyingRequired() && !Yii::$app->getModule('twofa')->isTwofaCheckUrl()) {
return Yii::$app->response->redirect(TwofaUrl::toCheck());
}
}
/**
* Check if currently action "Impersonate" is called
*
* @param $controller Controller
* @return bool
*/
protected static function isImpersonateAction($controller): bool
{
return ($controller instanceof AdminUserController)
&& isset($controller->action)
&& $controller->action->id === 'impersonate'
&& Yii::$app->user->can(ManageUsers::class);
}
/**
* Clear temp user ID which was used for administration action "Impersonate"
*
* @param $event
*/
public static function onAfterAction($event)
{
if ($event->sender instanceof AuthController && $event->sender->action->id == 'logout') {
TwofaHelper::resetSessionStatus();
Yii::$app->session->remove('twofa.switchedUserId');
}
}
/**
* Set flag after login to user who need 2fa
*
* @param $event
* @throws \Throwable
*/
public static function onAfterLogin($event)
{
TwofaHelper::resetSessionStatus();
TwofaHelper::enableVerifying();
}
/**
* Add menu to edit module setting per current User
*
* @param UserEvent $event
*/
public static function onProfileSettingMenuInit($event)
{
if (Yii::$app->user->isGuest) {
return;
}
$menuRoute = explode('/', trim(TwofaUrl::ROUTE_USER_SETTINGS, '/'));
$isActiveMenu = ControllerHelper::isActivePath($menuRoute[0], $menuRoute[1]);
/* @var AccountSettingsMenu $menu */
$menu = $event->sender;
$menu->addEntry(new MenuLink([
'label' => Yii::t('TwofaModule.base', 'Two-Factor Authentication'),
'url' => Yii::$app->user->identity->createUrl(TwofaUrl::ROUTE_USER_SETTINGS),
'isActive' => $isActiveMenu,
'sortOrder' => 300,
]));
if ($isActiveMenu) {
AccountMenu::markAsActive('account-settings-settings');
}
}
public static function onUserActionColumnAfterInitActions($event): void
{
if (!Yii::$app->user->can(ManageUsers::class)) {
return;
}
/** @var UserActionColumn $actionColumn */
$actionColumn = $event->sender;
if (empty($actionColumn->actions) || !is_array($actionColumn->actions)) {
return;
}
$resetLabel = Yii::t('TwofaModule.base', 'Reset two-factor authentication');
if (isset($actionColumn->actions[$resetLabel])) {
return;
}
$actions = [];
$inserted = false;
foreach ($actionColumn->actions as $actionTitle => $action) {
$actions[$actionTitle] = $action;
if (!$inserted && $actionTitle === Yii::t('base', 'Edit')) {
$actions[$resetLabel] = [
'/twofa/admin/reset-user',
'returnUrl' => Yii::$app->request->url,
'linkOptions' => [
'data-action-method' => 'post',
'data-action-confirm-header' => Yii::t('TwofaModule.base', '<strong>Confirm</strong> two-factor authentication reset'),
'data-action-confirm' => Yii::t('TwofaModule.base', 'This will remove the current two-factor authentication setup for this user. They will need to configure it again on the next login.'),
'data-action-confirm-text' => $resetLabel,
],
];
$inserted = true;
}
}
if ($inserted) {
$actionColumn->actions = $actions;
}
}
public static function canResetUser(User $user): bool
{
return Yii::$app->user->can(ManageUsers::class)
&& (Yii::$app->user->isAdmin() || !$user->isSystemAdmin());
}
}