forked from wikimedia/mediawiki-extensions-SemanticACL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticACL.class.php
511 lines (430 loc) · 16.5 KB
/
SemanticACL.class.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
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
<?php
/**
* SemanticACL extension - Allows per-page read and edit restrictions to be set with properties.
*
* @link https://www.mediawiki.org/wiki/Extension:SemanticACL Documentation
*
* @file SemanticACL.class.php
* @ingroup Extensions
* @defgroup SemanticACL
* @package MediaWiki
* @author Werdna (Andrew Garrett)
* @author Tinss (Antoine Mercier-Linteau)
* @copyright (C) 2011 Werdna
* @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*/
namespace MediaWiki\Extension\SemanticACL;
use Article;
use LogicException;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use RequestContext;
use SMW;
use SMWDIProperty;
use SMWDIWikiPage;
use SMWQueryResult;
use TextContent;
use Title;
/**
* SemanticACL extension main class.
*/
class SemanticACL {
/** The minimum key length for private link access. */
const MIN_KEY_LENGTH = 6;
/** The name of URL argument for private link access. */
const URL_ARG_NAME = 'semanticacl-key';
/**
* Initialize SMW properties.
*/
public static function onSMWPropertyinitProperties( $propertyRegistry ) {
// VISIBLE
$propertyRegistry->registerProperty( '___VISIBLE', '_txt', 'Visible to' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__VISIBLE', 'sacl-property-visibility' );
$propertyRegistry->registerProperty( '___VISIBLE_WL_GROUP', '_txt', 'Visible to group' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__VISIBLE_WL_GROUP', 'sacl-property-visibility-wl-group' );
$propertyRegistry->registerProperty( '___VISIBLE_WL_USER', '_txt', 'Visible to user' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__VISIBLE_WL_USER', 'sacl-property-visibility-wl-user' );
// EDITABLE
$propertyRegistry->registerProperty( '___EDITABLE', '_txt', 'Editable by' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__EDITABLE', 'sacl-property-Editable' );
$propertyRegistry->registerProperty( '___EDITABLE_WL_GROUP', '_txt', 'Editable by group' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__EDITABLE_WL_GROUP', 'sacl-property-editable-wl-group' );
$propertyRegistry->registerProperty( '___EDITABLE_WL_USER', '_txt', 'Editable by user' );
$propertyRegistry->registerPropertyDescriptionByMsgKey( '__EDITABLE_WL_USER', 'sacl-property-editable-wl-user' );
return true;
}
/**
* Filter results out of queries the current user is not supposed to see.
*/
public static function onSMWStoreAfterQueryResultLookupComplete( SMW\Store $store, SMWQueryResult &$queryResult ) {
/* NOTE: this filtering does not work with count queries. To do filtering on count queries, we would
* have to use SMW::Store::BeforeQueryResultLookupComplete to add conditions on ACL properties.
* However, doing that would make it extremely difficult to tweak caching on results.
*/
$filtered = [];
$changed = false; // If the result list was changed.
$user = RequestContext::getMain()->getUser();
foreach ( $queryResult->getResults() as $result ) {
$title = $result->getTitle();
if ( !$title instanceof Title ) {
// T296559
continue;
}
$accessible = true;
/* Check if the current user has permission to view that item.
* Disable the handling of caching so we can do it ourselves.
*/
if ( !self::hasPermission( $title, 'read', $user, false ) ) {
self::disableCaching(); // That item is not always visible, disable caching.
$accessible = false;
} elseif ( $title->getNamespace() == NS_FILE && !self::fileHasRequiredCategory( $title ) ) {
self::disableCaching(); // That item is not always visible, disable caching.
$accessible = false;
} else {
$semanticData = $store->getSemanticData( $result );
// Look for a SemanticACL property in the page's list of properties.
foreach ( $semanticData->getProperties() as $property ) {
$key = $property->getKey();
foreach ( [ '___VISIBLE', '___EDITABLE' ] as $semanticACLProperty ) {
// If this is a SemanticACL property.
if ( $key == $semanticACLProperty ) {
foreach ( $semanticData->getPropertyValues( $property ) as $dataItem ) {
// If this is a not a public item.
if ( $dataItem->getSerialization() != 'public' ) {
self::disableCaching(); // That item is not always visible, disable caching.
break 3;
}
}
}
}
}
}
if ( $accessible ) {
$filtered[] = $result;
} else {
// Skip that item.
$changed = true;
}
}
if ( !$changed ) {
// No changes to the query results.
return;
}
// Build a new query result object
$queryResult = new SMWQueryResult(
$queryResult->getPrintRequests(),
$queryResult->getQuery(),
$filtered,
$store,
$queryResult->hasFurtherResults()
);
}
/**
* When checking against the bad image list.
* Change $bad and return false to override.
* If an image is "bad", it is not rendered inline in wiki pages or galleries in category pages.
*
* @param string $name image name being checked
* @param bool &$bad Whether or not the image is "bad"
* @return bool false if the image is bad
*/
public static function onBadImage( $name, &$bad ) {
// Also works with galleries and categories.
$title = Title::newFromText( $name, NS_FILE );
$user = RequestContext::getMain()->getUser();
if ( !self::fileHasRequiredCategory( $title ) && !$user->isAllowed( 'view-non-categorized-media' ) ) {
self::disableCaching();
$bad = true;
return false;
}
if ( self::hasPermission( $title, 'read', $user, true ) ) {
// The user is allowed to view that file.
return true;
}
self::disableCaching();
$bad = true;
return false;
}
/**
* Called when the parser fetches a template.
* Replaces the template with an error message if the user cannot view the template.
*
* @param \Parser|false $parser Parser object or false
* @param Title $title Title object of the template to be fetched
* @param \Revision $rev Revision object of the template
* @param string|false|null $text transclusion text of the template or false or null
* @param array $deps array of template dependencies with 'title', 'page_id', 'rev_id' keys
* @return bool False if an error text oughta be shown, else true
*/
public static function onParserFetchTemplate( $parser, $title, $rev, &$text, &$deps ) {
$user = RequestContext::getMain()->getUser();
if ( self::hasPermission( $title, 'read', $user, true ) ) {
// User is allowed to view that template.
return true;
}
// Display error text instead of template.
if ( $user->isAnon() ) {
$msgKey = 'sacl-template-render-denied-anonymous';
} else {
$msgKey = 'sacl-template-render-denied-registered';
}
$text = wfMessage( $msgKey )->plain();
return false;
}
/**
* To interrupt/advise the "user can do X to Y article" check.
*
* @param Title $title Title object being checked against
* @param \User $user Current user object
* @param string $action Action being checked
* @param array|string &$result User permissions error to add. If none, return true.
* $result can be returned as a single error message key (string), or an array of error message
* keys when multiple messages are needed (although it seems to take an array as one message key with parameters?).
* @return bool False to abort execution of any other function in this hook, true to allow
* execution of other functions in this hook
*/
public static function onGetUserPermissionsErrors( $title, $user, $action, &$result ) {
// This hook is also triggered when displaying search results.
if( !self::hasPermission( $title, $action, $user, false ) ) {
$result = false;
return false;
}
return true;
}
/**
* Register render callbacks with the parser.
*
* @param \Parser &$parser
*/
public static function onParserFirstCallInit( &$parser ) {
$parser->setFunctionHook( 'SEMANTICACL_PRIVATE_LINK', __CLASS__ . '::getPrivateLink' );
}
/** @var string The key used for the private link. */
private static $_key = '';
/**
* Render callback to get a private link.
*
* @param \Parser &$parser The current parser
* @param string $key The key for the private link
* @return string A full URL with the correct arguments set on success, error message if $key
* is too short
*/
public static function getPrivateLink( \Parser &$parser, $key = '' ) {
global $wgEnablePrivateLinks;
if ( !$wgEnablePrivateLinks ) {
$key = wfMessage( 'sacl-private-links-disabled' )->text();
}
if ( strlen( $key ) <= self::MIN_KEY_LENGTH ) {
// Must specify a key.
return wfMessage( 'sacl-key-too-short' )->text();
}
self::disableCaching();
/* Save the key. If this function is called within hasPermission() through template
* expansion, it will be used to confirm access using a private link.
*/
self::$_key = $key;
return $parser->getTitle()->getFullURL( [ self::URL_ARG_NAME => urlencode( $key ) ] );
}
/**
* Checks if the provided user can do an action on a page.
*
* @param Title $title the title object to check permission on
* @param string $action the action the user wants to do
* @param \User $user the user to check permissions for
* @param bool $disableCaching force the page being checked to be rerendered for each user
* @return bool if the user is allowed to conduct the action
*/
protected static function hasPermission( $title, $action, $user, $disableCaching = true ) {
global $smwgNamespacesWithSemanticLinks;
global $wgSemanticACLWhitelistIPs;
global $wgRequest;
if ( $title->isTalkPage() ) {
// Talk pages get the same permission as their subject page.
$title = $title->getSubjectPage();
} elseif ( \ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
// If the Flow extension is installed.
if ( $title->getNamespace() == NS_TOPIC ) {
// Retrieve the board associated with the topic.
$storage = \Flow\Container::get( 'storage.workflow' );
$uuid = \Flow\WorkflowLoaderFactory::uuidFromTitle( $title );
$workflow = $storage->get( $uuid );
if ( $workflow ) {
// If for some reason there is no associated workflow, do not fail.
return self::hasPermission( $workflow->getOwnerTitle(), $action, $user, $disableCaching );
}
}
}
if (
!isset( $smwgNamespacesWithSemanticLinks[$title->getNamespace()] ) ||
!$smwgNamespacesWithSemanticLinks[$title->getNamespace()]
) {
// No need to check permissions on namespaces that do not support SemanticMediaWiki
return true;
}
// The prefix for the whitelisted group and user properties
// Either ___VISIBLE or ___EDITABLE
$prefix = '';
// Build the semantic property prefix according to the action.
if ( $action == 'read' || $action == 'raw' ) {
$prefix = '___VISIBLE';
} else {
$prefix = '___EDITABLE';
}
$subject = SMWDIWikiPage::newFromTitle( $title );
$store = SMW\StoreFactory::getStore()->getSemanticData( $subject );
$property = new SMWDIProperty( $prefix );
$aclTypes = $store->getPropertyValues( $property );
if ( $disableCaching ) {
/* If the parser caches the page, the same page will be returned without consideration for the user viewing the page.
* Disable the cache to it gets rendered anew for every user.
*/
self::disableCaching();
}
// Failsafe: Some users are exempt from Semantic ACLs.
if ( $user->isAllowed( 'sacl-exempt' ) ) {
return true;
}
// Always allow whitelisted IPs through.
if (
isset( $wgSemanticACLWhitelistIPs ) &&
in_array( $wgRequest->getIP(), $wgSemanticACLWhitelistIPs )
) {
return true;
}
if ( $title->getNamespace() == NS_FILE ) {
if ( !self::fileHasRequiredCategory( $title ) && !$user->isAllowed( 'view-non-categorized-media' ) ) {
return false;
}
}
$hasPermission = true;
foreach ( $aclTypes as $valueObj ) { // For each ACL specifier.
switch ( strtolower( $valueObj->getString() ) ) {
case 'whitelist':
$isWhitelisted = false;
$groupProperty = new SMWDIProperty( "{$prefix}_WL_GROUP" );
$userProperty = new SMWDIProperty( "{$prefix}_WL_USER" );
$whitelistValues = $store->getPropertyValues( $groupProperty );
// Check if the current user is part of a whitelisted group.
foreach ( $whitelistValues as $whitelistValue ) {
/* MediaWiki does not seem to specify whether groups are case sensitive or not.
* To account for all cases group comparison is done in a case insentitive way.
* See: https://www.mediawiki.org/wiki/Topic:Vi9pg5qywcfjpqox
*/
$group = strtolower( $whitelistValue->getString() );
if ( method_exists( MediaWikiServices::class, 'getUserGroupManager' ) ) {
// MW 1.35+
$effectiveGroups = MediaWikiServices::getInstance()->getUserGroupManager()
->getUserEffectiveGroups( $user );
} else {
$effectiveGroups = $user->getEffectiveGroups();
}
if ( in_array( $group, array_map( 'strtolower', $effectiveGroups ) ) ) {
$isWhitelisted = true;
break;
}
}
$whitelistValues = $store->getPropertyValues( $userProperty );
foreach ( $whitelistValues as $whitelistValue ) {
$userPage = Title::newFromDBkey( $whitelistValue->getString() );
if ( $user->getUserPage()->equals( $userPage ) ) {
$isWhitelisted = true;
}
}
if ( !$isWhitelisted ) {
$hasPermission = false;
}
break;
case 'key':
/* The key is parsed out of the page's content because it cannot be set as semantic
* property. Doing so would expose it to searches and queries.
*/
global $wgEnablePrivateLinks;
if ( !$wgEnablePrivateLinks ) {
// Private links have been disabled.
break;
}
// Only works when viewing pages.
if ( $action != 'read' && $action != 'raw' ) {
break;
}
/* Expand all the templates in the accessed page to retrieve the magic word.
* The magic word will be stored in self::$_key and set there by the getPrivateLink() parser hook.
*/
// Use a new parser to avoid interfering with the current parser.
$parser = \MediaWiki\MediaWikiServices::getInstance()->getParserFactory()->create();
$parser->startExternalParse( $title, \ParserOptions::newFromContext( RequestContext::getMain() ), \Parser::OT_PREPROCESS );
$revision = Article::newFromTitle( $title, RequestContext::getMain() )->getPage()->getRevisionRecord();
if ( !$revision ) {
// Should not happen
throw new LogicException( 'SemanticACL: unknown revision with the \'key\' mechanism' );
}
$content = $revision->getContent( SlotRecord::MAIN );
if ( !( $content instanceof TextContent ) ) {
// Not implemented on non-text contents
break;
}
$text = $parser->recursivePreprocess(
$content->getText(),
$title,
$parser->mOptions
);
$query = RequestContext::getMain()->getRequest()->getQueryValues();
$key = self::$_key; // The key normally should have been set during page parsing and template expansion.
if (
strlen( $key ) > self::MIN_KEY_LENGTH && // The key must be a certain length.
isset( $query[self::URL_ARG_NAME] ) &&
// If the key provided in the request arguments matches the key in the page.
$query[self::URL_ARG_NAME] === $key
) {
return true;
}
break;
case 'users':
$hasPermission = !$user->isAnon();
break;
case 'public':
return true;
}
}
return $hasPermission;
}
/**
* Disable caching for the page currently being rendered.
*/
protected static function disableCaching() {
global $wgParser;
if ( $wgParser->getOutput() ) {
$wgParser->getOutput()->updateCacheExpiry( 0 );
}
RequestContext::getMain()->getOutput()->disableClientCache();
}
/**
* Files that have not been categorized most likely have an unknown status when it comes to author's rights.
* This function tests if a file is part of a category for files whose's permissions have been set.
*
* @param Title $title the title of the file
* @return bool if the file has been properly categorized
*/
protected static function fileHasRequiredCategory( $title ) {
global $wgPublicImagesCategory;
if (
isset( $wgPublicImagesCategory ) && $wgPublicImagesCategory &&
$title->getNamespace() == NS_FILE
) {
$inCategory = false;
$page = Article::newFromTitle( $title, RequestContext::getMain() );
$file = $page->getFile();
if ( !$file->isLocal() ) {
// Foreign files are always shown.
return true;
}
foreach ( $page->getCategories() as $category ) {
if ( $category->getDBkey() == str_replace( ' ', '_', $wgPublicImagesCategory ) ) {
return true;
}
}
return false;
}
return true;
}
}