-
Notifications
You must be signed in to change notification settings - Fork 8
/
Events.php
293 lines (247 loc) · 9.64 KB
/
Events.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\legal;
use humhub\modules\admin\controllers\UserController;
use humhub\modules\comment\models\Comment;
use humhub\modules\content\widgets\richtext\ProsemirrorRichText;
use humhub\modules\legal\models\Page;
use humhub\modules\legal\models\RegistrationChecks;
use humhub\modules\legal\widgets\Content;
use humhub\modules\legal\widgets\CookieNote;
use humhub\modules\post\models\Post;
use humhub\modules\rest\components\BaseController;
use humhub\modules\ui\menu\MenuLink;
use humhub\modules\user\models\forms\Registration;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\AccountSettingsMenu;
use humhub\widgets\LayoutAddons;
use Yii;
use yii\base\ActionEvent;
use yii\helpers\Url;
use yii\web\UserEvent;
/**
* @author luke
*/
class Events
{
public const SESSION_KEY_LEGAL_CHECK = 'legalModuleChecked';
public const SESSION_KEY_LEGAL_AFTER_REGISTRATION = 'legalModuleAfterRegistration';
public static function onFooterMenuInit($event)
{
/** @var Module $module */
$module = Yii::$app->getModule('legal');
$sortOrder = 100;
foreach (Page::getPages() as $pageKey => $title) {
if (!$module->isPageEnabled($pageKey) || !in_array($pageKey, Page::getFooterMenuPages())) {
// Cookie notice is not a navigation page
continue;
}
$page = Page::getPage($pageKey);
if ($page !== null) {
$sortOrder += 10;
$event->sender->addItem([
'label' => $page->title,
'url' => Url::to(['/legal/page/view', 'pageKey' => $pageKey], true),
'sortOrder' => $sortOrder,
]);
}
}
}
public static function onLayoutAddonInit($event)
{
/** @var Module $module */
$module = Yii::$app->getModule('legal');
/** @var LayoutAddons $layoutAddons */
$layoutAddons = $event->sender;
if ($module->isPageEnabled(Page::PAGE_KEY_COOKIE_NOTICE)) {
$layoutAddons->addWidget(CookieNote::class);
}
}
public static function onBeforeControllerAction(ActionEvent $event)
{
if (Yii::$app->user->isGuest) {
return;
}
// Legal already checked
if (!empty(Yii::$app->session->get(static::SESSION_KEY_LEGAL_CHECK))) {
return;
}
/** @var Module $module */
$module = Yii::$app->getModule('legal');
// Allow user delete action
if ($event->action->controller->module->id === 'user' && $event->action->controller->id === 'account' && $event->action->id === 'delete') {
return;
}
if ($event->action->controller->module->id === 'user' && $event->action->controller->id === 'auth') {
return;
}
if ($event->action->controller->module->id === 'user' && $event->action->controller->id === 'must-change-password') {
return;
}
if ($event->action->controller->module->id === 'mail' && $event->action->controller->id === 'mail') {
return;
}
if ($event->action->controller->id === 'poll') {
return;
}
if ($event->action->controller->module->id === 'file' && $event->action->controller->id === 'file' && $event->action->id === 'download') {
return;
}
if ($event->action->controller instanceof BaseController) { // REST API request
return;
}
if ($event->action->controller->module->id === 'twofa' && $event->action->controller->id === 'check') {
return;
}
if ($event->action->controller->module->id === 'termsbox' && $event->action->controller->id === 'index') {
return;
}
if ($event->action->controller->module->id === 'breakingnews' && $event->action->controller->id === 'index') {
return;
}
$registrationCheck = new RegistrationChecks(['user' => Yii::$app->user->getIdentity()]);
if (!$registrationCheck->hasOpenCheck()) {
Yii::$app->session->set(static::SESSION_KEY_LEGAL_CHECK, 'true');
Yii::$app->session->remove(static::SESSION_KEY_LEGAL_AFTER_REGISTRATION);
return;
}
// Allow legal module usage
if ($event->action->controller->module->id === 'legal') {
if (Yii::$app->controller->id === 'page') {
$event->sender->layout = '@user/views/layouts/main';
$event->sender->subLayout = '@legal/views/page/layout_login';
}
return;
}
// Show legal update?
if (empty(Yii::$app->session->get(static::SESSION_KEY_LEGAL_AFTER_REGISTRATION)) && $module->isPageEnabled(Page::PAGE_KEY_LEGAL_UPDATE) && Page::getPage(Page::PAGE_KEY_LEGAL_UPDATE) !== null) {
$event->isValid = false;
$event->result = Yii::$app->response->redirect(['/legal/page/update']);
}
// Show legal pages in full screen with confirm form, one by one (after account creation)
elseif ($registrationCheck->showTermsCheck() || $registrationCheck->showPrivacyCheck()) {
$event->isValid = false;
$event->result = Yii::$app->response->redirect(['/legal/page/confirm']);
}
}
public static function skipVerifying(): bool
{
// Do not use on console request
if (Yii::$app->request->isConsoleRequest) {
return true;
}
// If, for example, users are automatically registered with LDAP during login, no legal checks should take place.
// Otherwise the auto registration would be broken.
if (Yii::$app->controller instanceof \humhub\modules\user\controllers\AuthController) {
return true;
}
// Don't ask admin on creating of a new user from back-office
if (Yii::$app->user->isAdmin() && (Yii::$app->controller instanceof UserController)) {
return true;
}
return false;
}
public static function onRegistrationFormInit($event)
{
if (static::skipVerifying()) {
return;
}
/** @var Registration $hForm */
$hForm = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('legal');
$hForm->models['RegistrationChecks'] = new RegistrationChecks(['restrictToSettingKey' => $module->showPagesAfterRegistration() ? RegistrationChecks::SETTING_KEY_AGE : false]);
if ($module->showPagesAfterRegistration()) {
Yii::$app->session->set(static::SESSION_KEY_LEGAL_AFTER_REGISTRATION, 'true');
}
}
public static function onRegistrationFormRender($event)
{
if (static::skipVerifying()) {
return;
}
/** @var Registration $hForm */
$hForm = $event->sender;
/** @var RegistrationChecks $model */
$model = $hForm->models['RegistrationChecks'];
/** @var Module $module */
$module = Yii::$app->getModule('legal');
$elements = [];
if ($model->showTermsCheck()) {
$elements['termsCheck'] = [
'type' => 'checkbox',
'class' => 'form-control',
];
}
if ($model->showPrivacyCheck()) {
$elements['dataPrivacyCheck'] = [
'type' => 'checkbox',
'class' => 'form-control',
];
}
if ($module->showAgeCheck()) {
$elements['ageCheck'] = [
'type' => 'checkbox',
'class' => 'form-control',
];
}
$hForm->definition['elements']['RegistrationChecks'] = [
'type' => 'form',
'elements' => $elements,
];
}
/**
* @param UserEvent $event
* @throws \yii\base\Exception
*/
public static function onRegistrationAfterRegistration(UserEvent $event)
{
if (static::skipVerifying()) {
return;
}
/** @var User $user */
$user = $event->identity;
/** @var Module $module */
$module = Yii::$app->getModule('legal');
$model = new RegistrationChecks(['user' => $user, 'restrictToSettingKey' => $module->showPagesAfterRegistration() ? RegistrationChecks::SETTING_KEY_AGE : false]);
$model->load(Yii::$app->request->post());
$model->save();
}
public static function onAfterRunRichText($event)
{
/* @var ProsemirrorRichText $richText */
$richText = $event->sender;
if (!isset($richText->record) || empty($event->result)) {
return;
}
if ($richText->record instanceof Post || $richText->record instanceof Comment) {
$event->result = Content::widget(['content' => $event->result, 'richtext' => false]);
}
}
public static function onAccountSettingsMenuInit($event)
{
/* @var AccountSettingsMenu $menu */
$menu = $event->sender;
/* @var Module $module */
$module = Yii::$app->getModule('legal');
if ($module->isEnabledExportUserData()) {
$menu->addEntry(new MenuLink([
'label' => Yii::t('LegalModule.base', 'Export personal data'),
'url' => ['/legal/export'],
'sortOrder' => 1000,
'isActive' => MenuLink::isActiveState('legal', 'export'),
]));
}
}
/**
* Callback on daily cron job run
*/
public static function onCronDailyRun()
{
Yii::$app->queue->push(new jobs\DeletePackages());
}
}