-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUserGroup.class.php
More file actions
650 lines (567 loc) · 16.8 KB
/
UserGroup.class.php
File metadata and controls
650 lines (567 loc) · 16.8 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
namespace wcf\data\user\group;
use wcf\data\DatabaseObject;
use wcf\data\ITitledObject;
use wcf\data\user\User;
use wcf\system\cache\builder\UserGroupCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\WCF;
use wcf\util\ArrayUtil;
/**
* Represents a user group.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @property-read int $groupID unique id of the user group
* @property-read string $groupName name of the user group or name of language
* item which contains the name
* @property-read string $groupDescription description of the user group or name of
* language item which contains the description
* @property-read int $groupType identifier of the type of user group
* @property-read int $priority priority of the user group used to determine
* member's user rank and online marking
* @property-read string $userOnlineMarking HTML code used to print the formatted name of
* a user group member
* @property-read int $showOnTeamPage is `1` if the user group and its members
* should be shown on the team page, otherwise `0`
* @property-read int $allowMention is `1` if the user group can be mentioned in messages,
* otherwise `0`
* @property-read int $requireMultifactor is `1` if group members need to set up multi-factor
* authentcation, otherwise `0`
*/
class UserGroup extends DatabaseObject implements ITitledObject
{
/**
* group type everyone user group
* @var int
*/
const EVERYONE = 1;
/**
* group type guests user group
* @var int
*/
const GUESTS = 2;
/**
* group type registered users user group
* @var int
*/
const USERS = 3;
/**
* group type of other user groups
* @var int
*/
const OTHER = 4;
/**
* the owner group is always an administrator group
* @var int
*/
const OWNER = 9;
/**
* group cache
* @var array{groups: array<int, UserGroup>, types: array<int, int[]>}
*/
protected static $cache;
/**
* list of accessible groups for active user
* @var int[]
*/
protected static $accessibleGroups;
/**
* @var UserGroup|false
*/
protected static $ownerGroup = false;
/**
* group options of this group
* @var array<string, mixed>
*/
protected $groupOptions;
/**
* Returns group ids by given type.
*
* @param int[] $types
* @return int[]
*/
public static function getGroupIDsByType(array $types)
{
self::getCache();
$groupIDs = [];
foreach ($types as $type) {
if (isset(self::$cache['types'][$type])) {
$groupIDs = \array_merge($groupIDs, self::$cache['types'][$type]);
}
}
return \array_unique($groupIDs);
}
/**
* Returns groups by given type. Returns all groups if no types given.
*
* @param int[] $types
* @param int[] $invalidGroupTypes
* @return array<int, UserGroup>
*/
public static function getGroupsByType(array $types = [], array $invalidGroupTypes = [])
{
self::getCache();
$groups = [];
foreach (self::$cache['groups'] as $group) {
if (
(empty($types) || \in_array($group->groupType, $types)) && !\in_array(
$group->groupType,
$invalidGroupTypes
)
) {
$groups[$group->groupID] = $group;
}
}
return $groups;
}
/**
* Returns a sorted list of groups filtered by given type.
*
* @param int[] $types
* @param int[] $invalidGroupTypes
* @return UserGroup[]
* @since 5.3
*/
public static function getSortedGroupsByType(array $types = [], array $invalidGroupTypes = [])
{
$userGroups = self::getGroupsByType($types, $invalidGroupTypes);
self::sortGroups($userGroups);
return $userGroups;
}
/**
* Returns unique group by given type. Only works for the default user groups.
*
* @param int $type
* @return ?UserGroup
* @throws SystemException
*/
public static function getGroupByType($type)
{
if ($type != self::EVERYONE && $type != self::GUESTS && $type != self::USERS && $type != self::OWNER) {
throw new SystemException('invalid value for type argument');
}
$groups = self::getGroupsByType([$type]);
return \array_shift($groups);
}
/**
* Returns the user group with the given id or null if no such user group
* exists.
*
* @param int $groupID
* @return UserGroup|null
*/
public static function getGroupByID($groupID)
{
self::getCache();
return self::$cache['groups'][$groupID] ?? null;
}
/**
* Returns a list of groups by group id.
*
* @param int[] $groupIDs list of group ids
* @return UserGroup[]
*/
public static function getGroupsByIDs(array $groupIDs)
{
$groups = [];
foreach ($groupIDs as $groupID) {
$group = self::getGroupByID($groupID);
if ($group !== null) {
$groups[$groupID] = $group;
}
}
return $groups;
}
/**
* Returns true if the given user is member of the group. If no user is
* given, the active user is used.
*
* @param User $user user object or current user if null
* @return bool
*/
public function isMember(?User $user = null)
{
if ($user === null) {
$user = WCF::getUser();
}
if (\in_array($this->groupID, $user->getGroupIDs())) {
return true;
}
return false;
}
/**
* Returns true if this is the 'Everyone' group.
*
* @return bool
* @since 3.0
*/
public function isEveryone()
{
return $this->groupType == self::EVERYONE;
}
/**
* Returns true if this is the 'Users' group.
*
* @return bool
* @since 3.1
*/
public function isUsers()
{
return $this->groupType == self::USERS;
}
/**
* Returns true if this is the 'Owner' group.
*
* @return bool
* @since 5.2
*/
public function isOwner()
{
return $this->groupType == self::OWNER;
}
/**
* Returns `true` if the active user can copy this user group.
*
* @return bool
* @since 5.3
*/
public function canCopy()
{
return WCF::getSession()->getPermission('admin.user.canAddGroup') && $this->isAccessible();
}
/**
* Returns true if the given groups are accessible for the active user.
*
* @param int[] $groupIDs
* @return bool
*/
public static function isAccessibleGroup(array $groupIDs = [])
{
if (self::$accessibleGroups === null) {
self::$accessibleGroups = ArrayUtil::toIntegerArray(\explode(
',',
WCF::getSession()->getPermission('admin.user.accessibleGroups') ?: ''
));
}
if ($groupIDs === []) {
return false;
}
foreach ($groupIDs as $groupID) {
if (!\in_array($groupID, self::$accessibleGroups)) {
return false;
}
}
return true;
}
/**
* Returns a list of accessible groups.
*
* @param int[] $groupTypes
* @param int[] $invalidGroupTypes
* @return UserGroup[]
*/
public static function getAccessibleGroups(array $groupTypes = [], array $invalidGroupTypes = [])
{
$groups = self::getGroupsByType($groupTypes, $invalidGroupTypes);
foreach ($groups as $key => $value) {
if (!self::isAccessibleGroup([$key])) {
unset($groups[$key]);
}
}
return $groups;
}
/**
* Returns a sorted list of accessible groups.
*
* @param int[] $groupTypes
* @param int[] $invalidGroupTypes
* @return UserGroup[]
* @since 5.2
*/
public static function getSortedAccessibleGroups(array $groupTypes = [], array $invalidGroupTypes = [])
{
$userGroups = self::getAccessibleGroups($groupTypes, $invalidGroupTypes);
self::sortGroups($userGroups);
return $userGroups;
}
/**
* Returns true if the current group is an admin-group, which requires it to fulfill
* one of these conditions:
* a) The WCFSetup is running and the group id is 4.
* b) This is the 'Owner' group.
* c) The group can access all groups (the 'Owner' group does not count).
*
* @return bool
*/
public function isAdminGroup()
{
// WCFSetup
if (!PACKAGE_ID && $this->groupID == 4) {
return true;
}
if ($this->groupType === self::OWNER) {
return true;
}
$groupIDs = \array_keys(self::getGroupsByType([], [self::OWNER]));
$accessibleGroupIDs = \explode(',', (string)$this->getGroupOption('admin.user.accessibleGroups'));
// no differences -> all groups are included
return \count(\array_diff($groupIDs, $accessibleGroupIDs)) == 0 ? true : false;
}
/**
* Returns true if the current group is a moderator-group.
*
* @return bool
*/
public function isModGroup()
{
// workaround for WCF-Setup
if (!PACKAGE_ID && ($this->groupID == 5 || $this->groupID == 4)) {
return true;
}
return $this->getGroupOption('mod.general.canUseModeration');
}
/**
* Loads the group cache.
*
* @return void
*/
protected static function getCache()
{
if (self::$cache === null) {
self::$cache = UserGroupCacheBuilder::getInstance()->getData();
}
}
/**
* Returns true if this group is accessible for the active user.
*
* @return bool
*/
public function isAccessible()
{
return self::isAccessibleGroup([$this->groupID]);
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return $this->getName();
}
/**
* Returns the name of this user group.
*
* @return string
*/
public function getName()
{
return WCF::getLanguage()->get($this->groupName);
}
/**
* Sets the name of this user group.
*
* This method is only needed to set the current name if it has been changed
* in the same request.
*
* @param string $name
* @return void
*/
public function setName($name)
{
$this->data['groupName'] = $name;
}
/**
* Returns true if current user may delete this group.
*
* @return bool
*/
public function isDeletable()
{
// insufficient permissions
if (!WCF::getSession()->getPermission('admin.user.canDeleteGroup')) {
return false;
}
// cannot delete own groups
if ($this->isMember()) {
return false;
}
// user cannot delete this group
if (!$this->isAccessible()) {
return false;
}
// cannot delete static groups
if ($this->groupType == self::EVERYONE || $this->groupType == self::GUESTS || $this->groupType == self::USERS || $this->groupType == self::OWNER) {
return false;
}
return true;
}
/**
* Returns true if current user may edit this group.
*
* @return bool
*/
public function isEditable()
{
// insufficient permissions
if (!WCF::getSession()->getPermission('admin.user.canEditGroup')) {
return false;
}
// user cannot edit this group
if (!$this->isAccessible()) {
return false;
}
return true;
}
/**
* Returns the value of the group option with the given name.
*
* @param string $name
* @return mixed|null
*/
public function getGroupOption($name)
{
if ($this->groupOptions === null) {
// get all options and filter options with low priority
$this->groupOptions = [];
$sql = "SELECT optionName, optionID
FROM wcf1_user_group_option";
$statement = WCF::getDB()->prepare($sql);
$statement->execute();
$groupOptionIDs = $statement->fetchMap('optionName', 'optionID');
if (!empty($groupOptionIDs)) {
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("option_value.groupID = ?", [$this->groupID]);
$conditions->add("option_value.optionID IN (?)", [$groupOptionIDs]);
$sql = "SELECT group_option.optionName, option_value.optionValue
FROM wcf1_user_group_option_value option_value
LEFT JOIN wcf1_user_group_option group_option
ON group_option.optionID = option_value.optionID
" . $conditions;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
$this->groupOptions = $statement->fetchMap('optionName', 'optionValue');
}
}
return $this->groupOptions[$name] ?? null;
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return WCF::getLanguage()->get($this->groupName);
}
/**
* Returns the user group description in the active user's language.
*
* @return string
* @since 5.2
*/
public function getDescription()
{
return WCF::getLanguage()->get($this->groupDescription ?: '');
}
/**
* The `Everyone`, `Guests` and `Users` group can never be mentioned.
*
* @return bool
* @since 5.2
*/
public function isUnmentionableGroup()
{
return \in_array($this->groupType, [self::EVERYONE, self::GUESTS, self::USERS]);
}
/**
* Returns true if this group can be mentioned, is always false for the
* `Everyone`, `Guests` and `Users` group.
*
* @return bool
* @since 5.2
*/
public function canBeMentioned()
{
if ($this->isUnmentionableGroup()) {
return false;
}
return !!$this->allowMention;
}
/**
* @return UserGroup[]
* @since 5.2
*/
public static function getMentionableGroups()
{
if (!WCF::getSession()->getPermission('user.message.canMentionGroups')) {
return [];
}
self::getCache();
$groups = [];
/** @var UserGroup $group */
foreach (self::$cache['groups'] as $group) {
if ($group->canBeMentioned()) {
$groups[] = $group;
}
}
return $groups;
}
/**
* @return UserGroup[]
* @since 5.2
*/
public static function getAllGroups()
{
self::getCache();
return self::$cache['groups'];
}
/**
* Returns the list of irrevocable permissions of the owner group.
*
* @return string[]
* @since 5.2
*/
public static function getOwnerPermissions()
{
return [
'admin.configuration.canEditOption',
'admin.configuration.canManageApplication',
'admin.configuration.package.canInstallPackage',
'admin.configuration.package.canUpdatePackage',
'admin.general.canUseAcp',
'admin.general.canViewPageDuringOfflineMode',
'admin.user.canEditGroup',
'admin.user.canEditUser',
'admin.user.canSearchUser',
];
}
/**
* Returns the owner group's id unless no group was promoted yet due to backwards compatibility.
*
* @return ?int
* @since 5.2
*/
public static function getOwnerGroupID()
{
if (self::$ownerGroup === false) {
self::$ownerGroup = self::getGroupByType(self::OWNER);
}
return self::$ownerGroup ? self::$ownerGroup->groupID : null;
}
/**
* Sorts the given user groups alphabetically.
*
* @param UserGroup[] $userGroups
* @return void
* @since 5.3
*/
public static function sortGroups(array &$userGroups)
{
$collator = new \Collator(WCF::getLanguage()->getLocale());
\uasort(
$userGroups,
static fn(self $groupA, self $groupB) => $collator->compare($groupA->getName(), $groupB->getName())
);
}
}