Skip to content

Commit f5a4fbf

Browse files
committed
Move data handling to helper functions
This allows us to reuse the code in other plugins or templates. Signed-off-by: Frieder Schrempf <[email protected]>
1 parent fd02095 commit f5a4fbf

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

helper.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* DokuWiki Plugin aclinfo (Helper Component)
4+
*
5+
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6+
* @author Frieder Schrempf <[email protected]>
7+
*/
8+
9+
// must be run within Dokuwiki
10+
if(!defined('DOKU_INC')) die();
11+
12+
/**
13+
* Class helper_plugin_aclinfo
14+
*/
15+
class helper_plugin_aclinfo extends DokuWiki_Plugin {
16+
17+
/**
18+
* Create a list of file permissions for a page
19+
*
20+
* @param string $pageid
21+
*/
22+
public function getACLInfo($pageid) {
23+
global $AUTH_ACL;
24+
25+
$info = array();
26+
$subjects = array();
27+
28+
/*
29+
* Get the permissions for @ALL in the beginning, we will use it
30+
* to compare and filter other permissions that are lower.
31+
*/
32+
$allperm = auth_aclcheck($page, '', array('ALL'));
33+
34+
/*
35+
* Go through each entry of the ACL rules.
36+
*/
37+
foreach($AUTH_ACL as $rule){
38+
$rule = preg_replace('/#.*$/', '', $rule); // Ignore comments
39+
$subject = preg_split('/[ \t]+/', $rule)[1];
40+
$subject = urldecode($subject);
41+
$groups = array();
42+
$user = '';
43+
44+
// Skip if we already checked this user/group
45+
if(in_array($subject, $subjects))
46+
continue;
47+
48+
$subjects[] = $subject;
49+
50+
// Check if this entry is about a user or a group (starting with '@')
51+
if(substr($subject, 0, 1) === '@')
52+
$groups[] = substr($subject, 1);
53+
else
54+
$user = $subject;
55+
56+
$perm = auth_aclcheck($page, $user, $groups);
57+
58+
// Skip permissions of 0 or if lower than @ALL
59+
if($perm == AUTH_NONE || ($subject != '@ALL' && $perm <= $allperm))
60+
continue;
61+
62+
$info[] = array('subject' => $subject, 'perm' => $perm);
63+
}
64+
65+
return $info;
66+
}
67+
68+
/**
69+
* Get a string representation for the permission info
70+
*
71+
* @param array $info
72+
*/
73+
public function getACLInfoString($info) {
74+
return sprintf($this->getLang('perm'.$info['perm']), $info['subject']);
75+
}
76+
}

syntax.php

+12-34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
* need to inherit from this class
1616
*/
1717
class syntax_plugin_aclinfo extends DokuWiki_Syntax_Plugin {
18+
/** @var helper_plugin_aclinfo */
19+
protected $helper;
20+
21+
/**
22+
* syntax_plugin_aclinfo constructor.
23+
*/
24+
public function __construct() {
25+
$this->helper = plugin_load('helper', 'aclinfo');
26+
}
1827

1928
/**
2029
* What kind of syntax are we?
@@ -59,7 +68,6 @@ function handle($match, $state, $pos, Doku_Handler $handler){
5968
*/
6069
function render($format, Doku_Renderer $R, $data) {
6170
global $INFO;
62-
global $AUTH_ACL;
6371

6472
if($format != 'xhtml') return false;
6573

@@ -69,47 +77,17 @@ function render($format, Doku_Renderer $R, $data) {
6977
$page = $data[0];
7078
}
7179

72-
$subjects = array();
73-
74-
/*
75-
* Get the permissions for @ALL in the beginning, we will use it
76-
* to compare and filter other permissions that are lower.
77-
*/
78-
$allperm = auth_aclcheck($page, '', array('ALL'));
80+
$info = $this->helper->getACLInfo($page);
7981

8082
$R->listu_open();
8183

8284
/*
8385
* Go through each entry of the ACL rules.
8486
*/
85-
foreach($AUTH_ACL as $rule){
86-
$rule = preg_replace('/#.*$/', '', $rule); // Ignore comments
87-
$subject = preg_split('/[ \t]+/', $rule)[1];
88-
$subject = urldecode($subject);
89-
$groups = array();
90-
$user = '';
91-
92-
// Skip if we already checked this user/group
93-
if(in_array($subject, $subjects))
94-
continue;
95-
96-
$subjects[] = $subject;
97-
98-
// Check if this entry is about a user or a group (starting with '@')
99-
if(substr($subject, 0, 1) === '@')
100-
$groups[] = substr($subject, 1);
101-
else
102-
$user = $subject;
103-
104-
$perm = auth_aclcheck($page, $user, $groups);
105-
106-
// Skip permissions of 0 or if lower than @ALL
107-
if($perm == AUTH_NONE || ($subject != '@ALL' && $perm <= $allperm))
108-
continue;
109-
87+
foreach($info as $entry){
11088
$R->listitem_open(1);
11189
$R->listcontent_open();
112-
$R->cdata(sprintf($this->getLang('perm'.$perm), $subject));
90+
$R->cdata($this->helper->getACLInfoString($entry));
11391
$R->listcontent_close();
11492
$R->listitem_close();
11593
}

0 commit comments

Comments
 (0)