-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvents.php
183 lines (156 loc) · 5.08 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
<?php
/**
* Transition Movement
* @link https://github.com/transitionnetwork/Humhub-Transition
* @license https://github.com/transitionnetwork/Humhub-Transition/blob/main/docs/LICENCE.md
* @author [Marc FARRE](https://marc.fun) for [Transition Movement](https://transitionnetwork.org/)
*/
namespace humhub\modules\transition;
use humhub\modules\admin\permissions\ManageUsers;
use humhub\modules\admin\widgets\UserMenu;
use humhub\modules\legal\Module;
use humhub\modules\space\models\Membership;
use humhub\modules\space\models\Space;
use humhub\modules\transition\helpers\MembershipHelper;
use humhub\modules\transition\jobs\SyncAllSpaceHosts;
use humhub\modules\ui\menu\MenuLink;
use humhub\modules\user\models\ProfileField;
use humhub\modules\user\models\User;
use Throwable;
use Yii;
use yii\base\Event;
use yii\base\InvalidConfigException;
use yii\helpers\BaseInflector;
class Events
{
/**
* @param Event $event
* @throws Throwable
* @throws InvalidConfigException
*/
public static function onAdminUserMenuInit($event)
{
/** @var UserMenu $menu */
$menu = $event->sender;
if (
Yii::$app->user->can(ManageUsers::class) // Don't move in 'isVisible' as it doesn't work in all cases and because the "if" costs less
&& ProfileField::findOne(['internal_name' => 'region'])
) {
$menu->addEntry(new MenuLink([
'label' => Yii::t('TransitionModule.config', 'Default spaces'),
'url' => ['/transition/admin/default-spaces'],
'sortOrder' => 2000,
'isActive' => MenuLink::isActiveState('transition', 'admin', 'default-spaces'),
'isVisible' => true,
]));
}
}
/**
* Add user to space depending on the Region field selected on the registration form
* @param Yii\web\UserEvent $event
* @return void
* @throws InvalidConfigException
* @throws Throwable
*/
public static function onFormAfterRegistration(yii\web\UserEvent $event)
{
// Do not store on console request
if (Yii::$app->request->isConsoleRequest) {
return;
}
if (!isset($event->identity)) {
return;
}
/** @var Module $module */
$module = Yii::$app->getModule('transition');
$settings = $module->settings;
$defaultSpaces = (array)$settings->getSerialized('defaultSpaces');
// Get user from event because Yii::$app->user->id doesnt work here
$user = User::findOne(['id' => $event->identity->id]);
if (!$user->profile->hasAttribute('region')) {
return;
}
$defaultSpaceIds = $defaultSpaces[BaseInflector::slug($user->profile->region)] ?? null;
if (!$defaultSpaceIds) {
return;
}
foreach (Space::findAll($defaultSpaceIds) as $space) {
$space->addMember($user->id);
}
}
/**
* @param $event
*/
public static function onModelSpaceMembershipMemberRemoved($event)
{
if (!isset($event)) {
return;
}
/** @var Membership $membership */
$membership = $event; // not $event->sender as it is executed by queue/run
$user = $membership->user;
MembershipHelper::updateUserTagsAndMembershipToSpaceHostsGroup($user);
}
/**
* @param $event
*/
public static function onModelSpaceMembershipMemberAdded($event)
{
if (!isset($event)) {
return;
}
/** @var Membership $membership */
$membership = $event; // not $event->sender as it is executed by queue/run
$user = $membership->user;
MembershipHelper::updateUserTagsAndMembershipToSpaceHostsGroup($user);
}
/**
* @param $event
*/
public static function onModelSpaceMembershipUpdate($event)
{
if (
!isset($event->sender, $event->changedAttributes)
|| !array_key_exists('group_id', $event->changedAttributes)
) {
return;
}
/** @var Membership $membership */
$membership = $event->sender;
$user = $membership->user;
MembershipHelper::updateUserTagsAndMembershipToSpaceHostsGroup($user);
}
/**
* @param $event
* @return void
*/
public static function onModelSpaceBeforeDelete($event)
{
if (empty($event->sender)) {
return;
}
/** @var Space $space */
$space = $event->sender;
Yii::$app->queue->push(new SyncAllSpaceHosts([
'tagFieldToRemove' => $space->name,
]));
}
/**
* @param $event
* @return void
*/
public static function onModelSpaceAfterUpdate($event)
{
if (
!isset($event->sender, $event->changedAttributes)
|| !array_key_exists('name', $event->changedAttributes)
) {
return;
}
/** @var Space $space */
$space = $event->sender;
Yii::$app->queue->push(new SyncAllSpaceHosts([
'tagFieldToRemove' => $space->name,
]));
}
}