Skip to content

Commit 00e5b49

Browse files
committed
feat: allow configuration of who may create new namespaces
1 parent e04f0d1 commit 00e5b49

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

action.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,31 @@ class action_plugin_newnamespacepermissions extends DokuWiki_Action_Plugin {
1919
*/
2020
public function register(Doku_Event_Handler $controller) {
2121

22-
$controller->register_hook('ACTION_ACT_PREPROCESS', 'FIXME', $this, 'handle_action_act_preprocess');
23-
22+
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess');
23+
$controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'checkPagesave');
24+
25+
}
26+
27+
/**
28+
* [Custom event handler which performs action]
29+
*
30+
* @param Doku_Event $event event object by reference
31+
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
32+
* handler was registered]
33+
* @return void
34+
*/
35+
public function handleActionActPreprocess(Doku_Event $event, $param) {
36+
if (act_clean($event->data) !== 'edit') {
37+
return;
38+
}
39+
40+
if ($this->isSaveAllowed()) {
41+
return;
42+
}
43+
44+
// user tries to create namespace but isn't allowed to do so
45+
$event->data = 'show';
46+
msg($this->getLang('namespace creation not allowed'),-1);
2447
}
2548

2649
/**
@@ -31,8 +54,35 @@ public function register(Doku_Event_Handler $controller) {
3154
* handler was registered]
3255
* @return void
3356
*/
57+
public function checkPagesave(Doku_Event $event, $param) {
58+
if ($this->isSaveAllowed()) {
59+
return;
60+
}
61+
62+
$event->preventDefault();
63+
msg($this->getLang('namespace creation not allowed'), -1);
64+
msg($this->getLang('you tried to save') . '<pre>' . hsc($event->data['newContent']) . '</pre>', 0);
65+
}
66+
67+
/**
68+
* Check if we're creating a new page in a new namespace and if we're allowed to do so
69+
*/
70+
protected function isSaveAllowed() {
71+
global $ID, $INPUT, $USERINFO;
72+
$namespaceDir = dirname(wikiFN($ID));
73+
if (file_exists($namespaceDir) && is_dir($namespaceDir)) {
74+
return true;
75+
}
76+
77+
$allowedToCreateNamespaces = $this->getConf('allow_namespace_creation');
78+
$user = $INPUT->server->str('REMOTE_USER');
79+
$groups = $USERINFO['grps'] ?: ['ALL'];
80+
81+
if (auth_isMember($allowedToCreateNamespaces, $user, $groups) || auth_ismanager(null, null, true)) {
82+
return true;
83+
}
3484

35-
public function handle_action_act_preprocess(Doku_Event &$event, $param) {
85+
return false;
3686
}
3787

3888
}

conf/default.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @author Michael Große <[email protected]>
66
*/
77

8-
//$conf['fixme'] = 'FIXME';
8+
$conf['allow_namespace_creation'] = '@user';

conf/metadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
*/
77

88

9-
//$meta['fixme'] = array('string');
9+
$meta['allow_namespace_creation'] = array('string');
1010

lang/en/lang.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// $lang['menu'] = 'Your menu entry';
1010

1111
// custom language strings for the plugin
12-
// $lang['fixme'] = 'FIXME';
13-
12+
$lang['namespace creation not allowed'] = 'You do not have the permission to create a new namespace.';
13+
$lang['you tried to save'] = 'You tried to save the following text: ';
1414

1515

1616
//Setup VIM: ex: et ts=4 :

lang/en/settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
// keys need to match the config setting name
9-
// $lang['fixme'] = 'FIXME';
9+
$lang['allow_namespace_creation'] = 'List of users and @groups that are allowed to create new namespaces';
1010

1111

1212

0 commit comments

Comments
 (0)