-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathLabelHandler.class.php
More file actions
448 lines (392 loc) · 13.9 KB
/
LabelHandler.class.php
File metadata and controls
448 lines (392 loc) · 13.9 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
<?php
namespace wcf\system\label;
use wcf\data\label\group\LabelGroup;
use wcf\data\label\group\ViewableLabelGroup;
use wcf\data\label\Label;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\data\user\User;
use wcf\system\cache\builder\LabelCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\SystemException;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
/**
* Manages labels and label-to-object associations.
*
* @author Alexander Ebert, Joshua Ruesweg
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @phpstan-import-type LabelCache from LabelCacheBuilder
*/
class LabelHandler extends SingletonFactory
{
/**
* cached list of object types
* @var array{
* objectTypes: array<int, ObjectType>,
* objectTypeNames: array<string, int>
* }
*/
protected $cache;
/**
* list of label groups
* @var LabelCache
*/
protected $labelGroups;
/**
* @inheritDoc
*/
protected function init()
{
$this->cache = [
'objectTypes' => [],
'objectTypeNames' => [],
];
$cache = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.label.object');
foreach ($cache as $objectType) {
$this->cache['objectTypes'][$objectType->objectTypeID] = $objectType;
$this->cache['objectTypeNames'][$objectType->objectType] = $objectType->objectTypeID;
}
$this->labelGroups = LabelCacheBuilder::getInstance()->getData();
}
/**
* Returns the id of the label ACL option with the given name or null if
* no such option exists.
*
* @param string $optionName
* @return ?int
*/
public function getOptionID($optionName)
{
foreach ($this->labelGroups['options'] as $option) {
if ($option->optionName === $optionName) {
return $option->optionID;
}
}
return null;
}
/**
* Returns the label object type with the given name or null of no such
* object.
*
* @param string $objectType
* @return ?ObjectType
*/
public function getObjectType($objectType)
{
if (isset($this->cache['objectTypeNames'][$objectType])) {
$objectTypeID = $this->cache['objectTypeNames'][$objectType];
return $this->cache['objectTypes'][$objectTypeID];
}
return null;
}
/**
* Returns an array with view permissions for the labels with the given id.
*
* @param int[] $labelIDs
* @param User $user
* @return array<int, bool>|array{}
* @see \wcf\system\label\LabelHandler::getPermissions()
*/
public function validateCanView(array $labelIDs, ?User $user = null)
{
return $this->getPermissions('canViewLabel', $labelIDs, $user);
}
/**
* Returns an array with use permissions for the labels with the given id.
*
* @param int[] $labelIDs
* @param User $user
* @return array<int, bool>|array{}
* @see \wcf\system\label\LabelHandler::getPermissions()
*/
public function validateCanUse(array $labelIDs, ?User $user = null)
{
return $this->getPermissions('canUseLabel', $labelIDs, $user);
}
/**
* Returns an array with boolean values for each given label id.
*
* @param string $optionName
* @param int[] $labelIDs
* @param User $user
* @return array<int, bool>|array{}
* @throws SystemException
*/
public function getPermissions($optionName, array $labelIDs, ?User $user = null)
{
if (empty($labelIDs)) {
// nothing to validate anyway
return [];
}
if (empty($this->labelGroups['groups'])) {
// pretend given label ids aren't valid
$data = [];
foreach ($labelIDs as $labelID) {
$data[$labelID] = false;
}
return $data;
}
$optionID = $this->getOptionID($optionName);
if ($optionID === null) {
throw new SystemException("cannot validate label ids, ACL options missing");
}
// validate each label
$data = [];
foreach ($labelIDs as $labelID) {
$isValid = false;
foreach ($this->labelGroups['groups'] as $group) {
if (!$group->isValid($labelID)) {
continue;
}
if (!$group->hasPermissions() || $group->getPermission($optionID, $user)) {
$isValid = true;
break;
}
}
$data[$labelID] = $isValid;
}
return $data;
}
/**
* Sets labels for given object id, pass an empty array to remove all previously
* assigned labels.
*
* @param int[] $labelIDs
* @param int $objectTypeID
* @param int $objectID
* @param bool $validatePermissions
* @return void
*/
public function setLabels(array $labelIDs, $objectTypeID, $objectID, $validatePermissions = true)
{
// get accessible label ids to prevent inaccessible ones to be removed
$accessibleLabelIDs = $this->getAccessibleLabelIDs();
// delete previous labels
if (!$validatePermissions || $accessibleLabelIDs !== []) {
$conditions = new PreparedStatementConditionBuilder();
if ($validatePermissions) {
$conditions->add("labelID IN (?)", [$accessibleLabelIDs]);
}
$conditions->add("objectTypeID = ?", [$objectTypeID]);
$conditions->add("objectID = ?", [$objectID]);
$sql = "DELETE FROM wcf1_label_object
" . $conditions;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
}
// insert new labels
if (!empty($labelIDs)) {
$sql = "INSERT INTO wcf1_label_object
(labelID, objectTypeID, objectID)
VALUES (?, ?, ?)";
$statement = WCF::getDB()->prepare($sql);
foreach ($labelIDs as $labelID) {
$statement->execute([
$labelID,
$objectTypeID,
$objectID,
]);
}
}
}
/**
* Replaces the labels of the label groups with the given ids with the labels with the given
* ids. Existing labels of the object from other label groups will not be changed. If no
* label for any of the given label group is given, an existing label from this group will
* be removed.
*
* @param int[] $groupIDs ids of the relevant label groups
* @param int[] $labelIDs ids of the new labels
* @param string $objectType label object type of the updated object
* @param int $objectID id of the updated object
* @return void
* @since 5.2
*/
public function replaceLabels(array $groupIDs, array $labelIDs, $objectType, $objectID)
{
$objectTypeID = $this->getObjectType($objectType)->objectTypeID;
// get the ids of the labels in the relevant label groups
$replacedLabelIDs = [];
foreach ($groupIDs as $groupID) {
$replacedLabelIDs = \array_merge(
$replacedLabelIDs,
$this->getLabelGroup($groupID)->getLabelIDs()
);
}
// delete old labels first
$conditionBuilder = new PreparedStatementConditionBuilder();
$conditionBuilder->add('labelID IN (?)', [$replacedLabelIDs]);
$conditionBuilder->add("objectTypeID = ?", [$objectTypeID]);
$conditionBuilder->add("objectID = ?", [$objectID]);
$sql = "DELETE FROM wcf1_label_object
" . $conditionBuilder;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditionBuilder->getParameters());
// assign new labels
if (!empty($labelIDs)) {
$sql = "INSERT INTO wcf1_label_object
(labelID, objectTypeID, objectID)
VALUES (?, ?, ?)";
$statement = WCF::getDB()->prepare($sql);
foreach ($labelIDs as $labelID) {
$statement->execute([
$labelID,
$objectTypeID,
$objectID,
]);
}
}
}
/**
* Returns all assigned labels, optionally filtered to validate permissions.
*
* @param int $objectTypeID
* @param int[] $objectIDs
* @param bool $validatePermissions
* @return Label[][]
*/
public function getAssignedLabels($objectTypeID, array $objectIDs, $validatePermissions = true)
{
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("objectTypeID = ?", [$objectTypeID]);
$conditions->add("objectID IN (?)", [$objectIDs]);
$sql = "SELECT objectID, labelID
FROM wcf1_label_object
" . $conditions;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
$labels = $statement->fetchMap('labelID', 'objectID', false);
// optionally filter out labels without permissions
if ($validatePermissions) {
$labelIDs = \array_keys($labels);
$result = $this->validateCanView($labelIDs);
foreach ($labelIDs as $labelID) {
if (!$result[$labelID]) {
unset($labels[$labelID]);
}
}
}
// reorder the array by object id
$data = [];
foreach ($labels as $labelID => $objectIDs) {
foreach ($objectIDs as $objectID) {
if (!isset($data[$objectID])) {
$data[$objectID] = [];
}
/** @var ViewableLabelGroup $group */
foreach ($this->labelGroups['groups'] as $group) {
$label = $group->getLabel($labelID);
if ($label !== null) {
$data[$objectID][$labelID] = $label;
}
}
}
}
// order label ids by label group
$labelGroups = &$this->labelGroups;
foreach ($data as &$labels) {
\uasort($labels, static function ($a, $b) use ($labelGroups) {
$groupA = $labelGroups['groups'][$a->groupID];
$groupB = $labelGroups['groups'][$b->groupID];
if ($groupA->showOrder == $groupB->showOrder) {
return ($groupA->groupID > $groupB->groupID) ? 1 : -1;
}
return ($groupA->showOrder > $groupB->showOrder) ? 1 : -1;
});
}
unset($labels);
return $data;
}
/**
* Returns given label groups by id.
*
* @param int[] $groupIDs
* @param bool $validatePermissions
* @param string $permission
* @return ViewableLabelGroup[]
* @throws SystemException
*/
public function getLabelGroups(array $groupIDs = [], $validatePermissions = true, $permission = 'canSetLabel')
{
$data = [];
$optionID = null;
if ($validatePermissions) {
$optionID = $this->getOptionID($permission);
if ($optionID === null) {
throw new SystemException("cannot validate group ids, ACL options missing");
}
}
if (empty($groupIDs)) {
$groupIDs = \array_keys($this->labelGroups['groups']);
}
foreach ($groupIDs as $groupID) {
// validate given group ids
if (!isset($this->labelGroups['groups'][$groupID])) {
throw new SystemException("unknown label group identified by group id '" . $groupID . "'");
}
// validate permissions
if ($validatePermissions) {
if (
$this->labelGroups['groups'][$groupID]->hasPermissions()
&& !$this->labelGroups['groups'][$groupID]->getPermission($optionID)
) {
continue;
}
}
$data[$groupID] = $this->getLabelGroup($groupID);
}
// @phpstan-ignore argument.type
\uasort($data, [LabelGroup::class, 'sortLabelGroups']);
return $data;
}
/**
* Returns a list of accessible label ids.
*
* @return int[]
*/
public function getAccessibleLabelIDs()
{
$labelIDs = [];
$groups = $this->getLabelGroups();
foreach ($groups as $group) {
$labelIDs = \array_merge($labelIDs, $group->getLabelIDs());
}
return $labelIDs;
}
/**
* Returns label group by id.
*
* @param int $groupID
* @return ViewableLabelGroup|null
*/
public function getLabelGroup($groupID): ?ViewableLabelGroup
{
if (!isset($this->labelGroups['groups'][$groupID])) {
return null;
}
$labelGroup = $this->labelGroups['groups'][$groupID];
if ($labelGroup->sortAlphabetically) {
$labelGroup->sortLabelsAlphabetically();
}
return $labelGroup;
}
/**
* Removes all assigned labels for given object ids.
*
* @param int $objectTypeID
* @param int[] $objectIDs
* @return void
*/
public function removeLabels($objectTypeID, array $objectIDs)
{
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("objectTypeID = ?", [$objectTypeID]);
$conditions->add("objectID IN (?)", [$objectIDs]);
$sql = "DELETE FROM wcf1_label_object
" . $conditions;
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
}
}