forked from wikipathways/mediawiki-extensions-WikiPathways
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamespacePermissions.php
75 lines (69 loc) · 2.5 KB
/
NamespacePermissions.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
<?php
/* NamespacePermissions - MediaWiki extension
*
* provides separate permissions for each action (read,edit,create,move)
* on articles in custom namespaces for fine access management
*
* Author: Petr Andreev
*
* Sample usage:3
*
* $wgExtraNamespaces = array(100 => "Foo", 101 => "Foo_Talk");
* // optional (example): allow registered users to view and edit articles in Foo
* $wgGroupPermissions[ 'user' ][ 'ns100_read' ] = true;
* $wgGroupPermissions[ 'user' ][ 'ns100_edit' ] = true;
* // end of optional
* require('extensions/NamespacePermissions.php');
*
* Permissions provided:
* # ns{$num}_read
* # ns{$num}_edit
* # ns{$num}_create
* # ns{$num}_move
* where {$num} - namespace number (e.g. ns100_read, ns101_create)
*
* Groups provided:
* # ns{$title}RW - full access to the namespace {$title}
* # ns{$title}RO - read-only access to the namespace {$title}
* e.g. nsFoo_talkRW, nsFooRO
*/
// permissions for autocreated groups should be set now,
// before the User object for current user is instantiated
namespacePermissionsCreateGroups();
// other stuff should better be done via standard mechanism of running extensions
$wgExtensionFunctions[] = "wfNamespacePermissions";
// create groups for each custom namespace
function namespacePermissionsCreateGroups() {
global $wgExtraNamespaces, $wgGroupPermissions;
foreach ( $wgExtraNamespaces as $num => $title ) {
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_edit" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_read" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_create" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_move" ] = true;
$wgGroupPermissions[ "ns{$title}RO" ][ "ns{$num}_read" ] = true;
}
}
function wfNamespacePermissions() {
global $wgHooks;
// use the userCan hook to check permissions
$wgHooks[ 'userCan' ][] = 'namespacePermissionsCheckNamespace';
}
function namespacePermissionsCheckNamespace( $title, $user, $action, $result ) {
if ( ( $ns = $title->getNamespace() ) >= 100 ) {
if ( ! $user->isAllowed( "ns{$ns}_{$action}" ) ) {
$result = false;
return false;
}
}
return true;
}
/**
* Add extension information to Special:Version
*/
$wgExtensionCredits['other'][] = [
'name' => 'NamespacePermissions',
'version' => '',
'author' => 'Petr Andreev',
'description' => 'flexible access management for custom namespaces',
'url' => 'http://www.mediawiki.org/wiki/Extension:NamespacePermissions'
];