Repository navigation
Expand file tree
/
Copy pathEvents.php
More file actions
111 lines (97 loc) · 3.49 KB
/
Copy pathEvents.php
File metadata and controls
111 lines (97 loc) · 3.49 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
<?php
/**
* HumHub DAV Access
*
* @package humhub.modules.humdav
* @author KeudellCoding
*/
namespace humhub\modules\humdav;
use Yii;
use yii\helpers\Url;
use yii\helpers\StringHelper;
use humhub\modules\humdav\definitions\RouteDefinitions;
use humhub\modules\humdav\models\UserToken;
use humhub\modules\ui\menu\MenuLink;
use humhub\modules\user\models\User;
class Events
{
public static function onBeforeRequest($event)
{
if (!StringHelper::startsWith(Yii::$app->request->pathInfo, 'humdav/')
&& Yii::$app->request->pathInfo !== '.well-known/carddav') {
return;
}
Yii::$app->urlManager->addRules(RouteDefinitions::getDefinitions(), true);
}
public static function onTopMenuInit($event)
{
try {
$settings = Yii::$app->getModule('humdav')->settings;
if ((bool)$settings->get('active', false) !== true) {
return;
}
if ($settings->get('instruction_location') !== 'top_menu') {
return;
}
$currentIdentity = Yii::$app->user->identity;
if ($currentIdentity === null || Yii::$app->user->isGuest) {
return;
}
$allowedUsers = array_filter((array)$settings->getSerialized('enabled_users'));
if (!in_array($currentIdentity->guid, $allowedUsers) && !empty($allowedUsers)) {
return;
}
$event->sender->addEntry(new MenuLink([
'label' => 'HumDAV',
'url' => Url::to(['/humdav/accessinfo/index']),
'htmlOptions' => [],
'icon' => 'address-card',
'isActive' => (Yii::$app->controller->module
&& Yii::$app->controller->module->id === 'humdav'
&& Yii::$app->controller->id === 'accessinfo'),
'sortOrder' => $settings->get('instruction_location_sort_order', 400),
]));
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onIntegrityCheck($event)
{
try {
$integrityController = $event->sender;
$integrityController->showTestHeadline('HumHub DAV Access Module - Token (' . UserToken::find()->count() . ' entries)');
foreach (UserToken::find()->joinWith(['user'])->each() as $userToken) {
if ($userToken->user === null) {
if ($integrityController->showFix('Deleting token ' . $userToken->id . ' without existing user!')) {
$userToken->delete();
}
continue;
}
if ($userToken->user->status !== User::STATUS_ENABLED) {
if ($integrityController->showFix('Deleting token ' . $userToken->id . ' with disabled user!')) {
$userToken->delete();
continue;
}
}
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onUserDelete($event)
{
try {
foreach (UserToken::findAll(['user_id' => $event->sender->id]) as $userToken) {
$userToken->delete();
}
} catch (\Throwable $e) {
Yii::error($e);
}
}
public static function onUserUpdate($event)
{
if ($event->sender->status !== User::STATUS_ENABLED) {
self::onUserDelete($event);
}
}
}