-
Notifications
You must be signed in to change notification settings - Fork 611
/
UserGroups.php
458 lines (394 loc) · 13.6 KB
/
UserGroups.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\db\Query;
use craft\db\Table;
use craft\elements\User;
use craft\enums\CmsEdition;
use craft\errors\WrongEditionException;
use craft\events\ConfigEvent;
use craft\events\UserGroupEvent;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use craft\models\UserGroup;
use craft\records\UserGroup as UserGroupRecord;
use yii\base\Component;
/**
* User Groups service.
*
* An instance of the service is available via [[\craft\base\ApplicationTrait::getUserGroups()|`Craft::$app->userGroups`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class UserGroups extends Component
{
/**
* @event UserGroupEvent The event that is triggered before a user group is saved.
*/
public const EVENT_BEFORE_SAVE_USER_GROUP = 'beforeSaveUserGroup';
/**
* @event UserGroupEvent The event that is triggered after a user group is saved.
*/
public const EVENT_AFTER_SAVE_USER_GROUP = 'afterSaveUserGroup';
/**
* @event UserGroupEvent The event that is triggered before a user group is deleted.
*/
public const EVENT_BEFORE_DELETE_USER_GROUP = 'beforeDeleteUserGroup';
/**
* @event UserGroupEvent The event that is triggered before a user group delete is applied to the database.
* @since 3.1.0
*/
public const EVENT_BEFORE_APPLY_GROUP_DELETE = 'beforeApplyGroupDelete';
/**
* @event UserGroupEvent The event that is triggered after a user group is saved.
*/
public const EVENT_AFTER_DELETE_USER_GROUP = 'afterDeleteUserGroup';
/**
* The “Team” group’s UUID.
*/
private const TEAM_GROUP_UUID = 'c55ca0a9-4bd6-409b-afd8-c1884dafecd0';
/**
* Returns all user groups.
*
* @return UserGroup[]
*/
public function getAllGroups(): array
{
switch (Craft::$app->edition) {
case CmsEdition::Solo:
return [];
case CmsEdition::Team:
return [$this->getTeamGroup()];
default:
$results = $this->_createUserGroupsQuery()
->orderBy(['name' => SORT_ASC])
->all();
return array_map(fn(array $result) => new UserGroup($result), $results);
}
}
/**
* Returns the user groups that the current user is allowed to assign to another user.
*
* @param User|null $user The recipient of the user groups. If set, their current groups will be included as well.
* @return UserGroup[]
*/
public function getAssignableGroups(?User $user = null): array
{
$currentUser = Craft::$app->getUser()->getIdentity();
if (!$currentUser && !$user) {
return [];
}
// If either user is an admin, all groups are fair game
if (($currentUser !== null && $currentUser->admin) || ($user !== null && $user->admin)) {
return $this->getAllGroups();
}
$assignableGroups = [];
foreach ($this->getAllGroups() as $group) {
if (
($currentUser !== null && $currentUser->can("assignUserGroup:$group->uid")) ||
($user !== null && $user->isInGroup($group))
) {
$assignableGroups[] = $group;
}
}
return $assignableGroups;
}
/**
* Gets a user group by its ID.
*
* @param int $groupId
* @return UserGroup|null
*/
public function getGroupById(int $groupId): ?UserGroup
{
$result = $this->_createUserGroupsQuery()
->where(['id' => $groupId])
->one();
return $result ? new UserGroup($result) : null;
}
/**
* Gets a user group by its UID.
*
* @param string $uid
* @return UserGroup|null
*/
public function getGroupByUid(string $uid): ?UserGroup
{
$result = $this->_createUserGroupsQuery()
->where(['uid' => $uid])
->one();
return $result ? new UserGroup($result) : null;
}
/**
* Gets a user group by its handle.
*
* @param string $groupHandle
* @return UserGroup|null
*/
public function getGroupByHandle(string $groupHandle): ?UserGroup
{
$result = $this->_createUserGroupsQuery()
->where(['handle' => $groupHandle])
->one();
return $result ? new UserGroup($result) : null;
}
/**
* Returns the Craft Team edition’s user group.
*
* @return UserGroup
* @since 5.1.0
*/
public function getTeamGroup(): UserGroup
{
Craft::$app->requireEdition(CmsEdition::Team, false);
$group = $this->getGroupByUid(self::TEAM_GROUP_UUID);
if ($group) {
return $group;
}
/** @var UserGroup $group */
$group = Craft::createObject([
'class' => UserGroup::class,
'uid' => self::TEAM_GROUP_UUID,
]);
// Find a unique name + handle
$i = 1;
do {
$group->name = sprintf('Team%s', $i > 1 ? " $i" : '');
$group->handle = sprintf('team%s', $i > 1 ? $i : '');
if ($group->validate(['name', 'handle'])) {
break;
}
$i++;
} while (true);
$groupRecord = UserGroupRecord::findOne(['uid' => $group->uid]) ?? new UserGroupRecord();
$groupRecord->name = $group->name;
$groupRecord->handle = $group->handle;
$groupRecord->description = null;
$groupRecord->uid = $group->uid;
$groupRecord->save(false);
$group->id = $groupRecord->id;
return $group;
}
/**
* Gets user groups by a user ID.
*
* @param int $userId
* @return UserGroup[]
*/
public function getGroupsByUserId(int $userId): array
{
$groups = (new Query())
->select([
'g.id',
'g.name',
'g.handle',
'g.uid',
])
->from(['g' => Table::USERGROUPS])
->innerJoin(['gu' => Table::USERGROUPS_USERS], '[[gu.groupId]] = [[g.id]]')
->where(['gu.userId' => $userId])
->all();
foreach ($groups as $key => $value) {
$groups[$key] = new UserGroup($value);
}
return $groups;
}
/**
* Eager-loads user groups onto the given users.
*
* @param User[] $users The users to eager-load user groups onto
* @since 3.6.0
*/
public function eagerLoadGroups(array $users): void
{
if (empty($users)) {
return;
}
$assignments = (new Query())
->select(['groupId', 'userId'])
->from([Table::USERGROUPS_USERS])
->where([
'userId' => array_unique(array_map(fn(User $user) => $user->id, $users)),
])
->all();
$groupsByUserId = [];
if (!empty($assignments)) {
// Get the user groups, indexed by their IDs
$groups = [];
$groupResults = $this->_createUserGroupsQuery()
->where([
'id' => array_unique(array_map(fn(array $assignment) => $assignment['groupId'], $assignments)),
])
->all();
foreach ($groupResults as $result) {
$groups[$result['id']] = new UserGroup($result);
}
// Create batches of user groups by user ID
foreach ($assignments as $assignment) {
if (isset($groups[$assignment['groupId']])) {
$groupsByUserId[$assignment['userId']][] = $groups[$assignment['groupId']];
}
}
}
// Assign the user groups
foreach ($users as $user) {
$user->setGroups($groupsByUserId[$user->id] ?? []);
}
}
/**
* Saves a user group.
*
* @param UserGroup $group The user group to be saved
* @param bool $runValidation Whether the user group should be validated
* @return bool
* @throws WrongEditionException if this is called from Craft Solo edition
*/
public function saveGroup(UserGroup $group, bool $runValidation = true): bool
{
if ($group->uid === self::TEAM_GROUP_UUID) {
Craft::$app->requireEdition(CmsEdition::Team);
} else {
Craft::$app->requireEdition(CmsEdition::Pro);
}
$isNewGroup = !$group->id;
// Fire a 'beforeSaveUserGroup' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_USER_GROUP)) {
$this->trigger(self::EVENT_BEFORE_SAVE_USER_GROUP, new UserGroupEvent([
'userGroup' => $group,
'isNew' => $isNewGroup,
]));
}
if ($runValidation && !$group->validate()) {
Craft::info('User group not saved due to validation error.', __METHOD__);
return false;
}
$projectConfig = Craft::$app->getProjectConfig();
if (!$group->uid) {
if ($isNewGroup) {
$group->uid = StringHelper::UUID();
} elseif (!$group->uid) {
$group->uid = Db::uidById(Table::USERGROUPS, $group->id);
}
}
$configPath = ProjectConfig::PATH_USER_GROUPS . '.' . $group->uid;
$configData = $group->getConfig(false);
$projectConfig->set($configPath, $configData, "Save user group “{$group->handle}”");
// Now that we have a group ID, save it on the model
if ($isNewGroup) {
$group->id = Db::idByUid(Table::USERGROUPS, $group->uid);
}
return true;
}
/**
* Handle any changed user groups.
*
* @param ConfigEvent $event
*/
public function handleChangedUserGroup(ConfigEvent $event): void
{
$uid = $event->tokenMatches[0];
$data = $event->newValue;
$groupRecord = UserGroupRecord::findOne(['uid' => $uid]) ?? new UserGroupRecord();
$isNewGroup = $groupRecord->getIsNewRecord();
$groupRecord->name = $data['name'];
$groupRecord->handle = $data['handle'];
$groupRecord->description = $data['description'] ?? null;
$groupRecord->uid = $uid;
$groupRecord->save(false);
// Prevent permission information from being saved. Allowing it would prevent the appropriate event from firing.
$event->newValue['permissions'] = $event->oldValue['permissions'] ?? [];
// Fire an 'afterSaveUserGroup' event
if ($this->hasEventHandlers(self::EVENT_AFTER_SAVE_USER_GROUP)) {
$this->trigger(self::EVENT_AFTER_SAVE_USER_GROUP, new UserGroupEvent([
'userGroup' => $this->getGroupById($groupRecord->id),
'isNew' => $isNewGroup,
]));
}
// Invalidate user caches
Craft::$app->getElements()->invalidateCachesForElementType(User::class);
}
/**
* Handle any deleted user groups.
*
* @param ConfigEvent $event
*/
public function handleDeletedUserGroup(ConfigEvent $event): void
{
$uid = $event->tokenMatches[0];
$group = $this->getGroupByUid($uid);
// Fire a 'beforeApplyGroupDelete' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_APPLY_GROUP_DELETE)) {
$this->trigger(self::EVENT_BEFORE_APPLY_GROUP_DELETE, new UserGroupEvent([
'userGroup' => $group,
]));
}
Db::delete(Table::USERGROUPS, [
'uid' => $uid,
]);
// Fire an 'afterDeleteUserGroup' event
if ($this->hasEventHandlers(self::EVENT_AFTER_DELETE_USER_GROUP)) {
$this->trigger(self::EVENT_AFTER_DELETE_USER_GROUP, new UserGroupEvent([
'userGroup' => $group,
]));
}
// Invalidate user caches
Craft::$app->getElements()->invalidateCachesForElementType(User::class);
}
/**
* Deletes a user group by its ID.
*
* @param int $groupId The user group's ID
* @return bool Whether the user group was deleted successfully
* @throws WrongEditionException if this is called from Craft Solo edition
*/
public function deleteGroupById(int $groupId): bool
{
Craft::$app->requireEdition(CmsEdition::Pro);
$group = $this->getGroupById($groupId);
if (!$group) {
return false;
}
return $this->deleteGroup($group);
}
/**
* Deletes a user group.
*
* @param UserGroup $group The user group
* @return bool Whether the user group was deleted successfully
* @throws WrongEditionException if this is called from Craft Solo edition
* @since 3.0.12
*/
public function deleteGroup(UserGroup $group): bool
{
Craft::$app->requireEdition(CmsEdition::Pro);
// Fire a 'beforeDeleteUserGroup' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_USER_GROUP)) {
$this->trigger(self::EVENT_BEFORE_DELETE_USER_GROUP, new UserGroupEvent([
'userGroup' => $group,
]));
}
Craft::$app->getProjectConfig()->remove(ProjectConfig::PATH_USER_GROUPS . '.' . $group->uid, "Delete the “{$group->handle}” user group");
return true;
}
/**
* @return Query
*/
private function _createUserGroupsQuery(): Query
{
return (new Query())
->select([
'id',
'name',
'handle',
'description',
'uid',
])
->from([Table::USERGROUPS]);
}
}