-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathUserGroupEditor.class.php
More file actions
216 lines (187 loc) · 6.38 KB
/
UserGroupEditor.class.php
File metadata and controls
216 lines (187 loc) · 6.38 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
<?php
namespace wcf\data\user\group;
use wcf\data\DatabaseObjectEditor;
use wcf\data\IEditableCachedObject;
use wcf\system\cache\builder\UserGroupCacheBuilder;
use wcf\system\cache\builder\UserGroupPermissionCacheBuilder;
use wcf\system\cache\eager\UserGroupAssignmentCache;
use wcf\system\exception\SystemException;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\WCF;
/**
* Provides functions to edit user groups.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @mixin UserGroup
* @extends DatabaseObjectEditor<UserGroup>
* @implements IEditableCachedObject<UserGroup>
*/
class UserGroupEditor extends DatabaseObjectEditor implements IEditableCachedObject
{
/**
* @inheritDoc
*/
protected static $baseClass = UserGroup::class;
/**
* @inheritDoc
*/
public static function create(array $parameters = [])
{
$group = parent::create($parameters);
self::updateAccessibleGroups($group->groupID);
return $group;
}
/**
* @inheritDoc
*/
public static function deleteAll(array $objectIDs = [])
{
$returnValue = parent::deleteAll($objectIDs);
// remove user to group assignments
self::removeGroupAssignments($objectIDs);
// remove group option values
self::removeOptionValues($objectIDs);
foreach ($objectIDs as $objectID) {
self::updateAccessibleGroups($objectID, true);
}
return $returnValue;
}
/**
* Removes user to group assignments.
*
* @param int[] $groupIDs
* @return void
*/
protected static function removeGroupAssignments(array $groupIDs)
{
if ($groupIDs === []) {
return;
}
$sql = "DELETE FROM wcf1_user_to_group
WHERE groupID = ?";
$statement = WCF::getDB()->prepare($sql);
foreach ($groupIDs as $groupID) {
$statement->execute([$groupID]);
}
}
/**
* Removes group option values.
*
* @param int[] $groupIDs
* @return void
*/
protected static function removeOptionValues(array $groupIDs)
{
if ($groupIDs === []) {
return;
}
$sql = "DELETE FROM wcf1_user_group_option_value
WHERE groupID = ?";
$statement = WCF::getDB()->prepare($sql);
foreach ($groupIDs as $groupID) {
$statement->execute([$groupID]);
}
}
/**
* Updates group options.
*
* @param array<int, mixed> $groupOptions
* @return void
*/
public function updateGroupOptions(array $groupOptions = [])
{
WCF::getDB()->beginTransaction();
// delete old group options
$sql = "DELETE FROM wcf1_user_group_option_value
WHERE groupID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$this->groupID]);
// insert new options
$sql = "INSERT INTO wcf1_user_group_option_value
(groupID, optionID, optionValue)
VALUES (?, ?, ?)";
$statement = WCF::getDB()->prepare($sql);
foreach ($groupOptions as $id => $value) {
$statement->execute([$this->groupID, $id, $value]);
}
WCF::getDB()->commitTransaction();
}
/**
* Updates the value from the accessiblegroups option.
*
* @param int $groupID this group is added or deleted in the value
* @param bool $delete flag for group deletion
* @return void
* @throws SystemException
*/
protected static function updateAccessibleGroups($groupID, $delete = false)
{
$sql = "SELECT optionID
FROM wcf1_user_group_option
WHERE optionName = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute(['admin.user.accessibleGroups']);
$optionID = $statement->fetchSingleColumn();
if (!$optionID) {
throw new SystemException("Unable to find 'admin.user.accessibleGroups' user option");
}
$ownerGroupID = UserGroup::getOwnerGroupID();
$userGroupList = new UserGroupList();
$userGroupList->getConditionBuilder()->add('user_group.groupID <> ?', [$groupID]);
if ($ownerGroupID) {
$userGroupList->getConditionBuilder()->add('user_group.groupID <> ?', [$ownerGroupID]);
}
$userGroupList->readObjects();
$groupIDs = [];
foreach ($userGroupList as $userGroup) {
$groupIDs[] = $userGroup->groupID;
}
$sql = "UPDATE wcf1_user_group_option_value
SET optionValue = ?
WHERE groupID = ?
AND optionID = ?";
$updateStatement = WCF::getDB()->prepare($sql);
$sql = "SELECT groupID, optionValue
FROM wcf1_user_group_option_value
WHERE optionID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$optionID]);
while ($row = $statement->fetchArray()) {
$valueIDs = \array_filter(
\explode(',', $row['optionValue']),
static function ($groupID) use ($ownerGroupID) {
return $groupID != $ownerGroupID;
}
);
if ($delete) {
$valueIDs = \array_filter($valueIDs, static function ($item) use ($groupID) {
return $item != $groupID;
});
} else {
if (\count(\array_diff($groupIDs, $valueIDs)) == 0) {
$valueIDs[] = $groupID;
}
}
if ($row['groupID'] == $ownerGroupID) {
$valueIDs[] = $ownerGroupID;
}
$updateStatement->execute([\implode(',', $valueIDs), $row['groupID'], $optionID]);
}
}
/**
* @inheritDoc
*/
public static function resetCache()
{
// Clear group cache.
UserGroupCacheBuilder::getInstance()->reset();
UserGroupPermissionCacheBuilder::getInstance()->reset();
// https://github.com/WoltLab/WCF/issues/4045
(new UserGroupAssignmentCache())->rebuild();
// Clear cached group assignments.
UserStorageHandler::getInstance()->resetAll('groupIDs');
}
}