4 * Copyright 2004 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Set individual PagePermissions
27 * Usage: <?plugin WikiAdminSetAcl ?> or called via WikiAdminSelect
28 * Author: Reini Urban <rurban@x-ray.at>
31 * Requires PHP 4.2 so far.
33 * TODO: UI to add custom group/username.
34 * Currently it's easier to dump a page, fix it manually and
35 * import it, than use Setacl
37 require_once('lib/PageList.php');
38 require_once('lib/plugin/WikiAdminSelect.php');
40 class WikiPlugin_WikiAdminSetAcl
41 extends WikiPlugin_WikiAdminSelect
44 return _("WikiAdminSetAcl");
47 function getDescription() {
48 return _("Set individual page permissions.");
51 function getVersion() {
52 return preg_replace("/[Revision: $]/", '',
56 function getDefaultArguments() {
59 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
61 'p' => "[]", // list of pages
62 /* Columns to include in listing */
63 'info' => 'pagename,perm,mtime,owner,author',
67 function setaclPages(&$request, $pages, $acl) {
68 $result = HTML::div();
70 $dbi =& $request->_dbi;
71 // check new_group and new_perm
72 if (isset($acl['_add_group'])) {
73 //add groups with perm
74 foreach ($acl['_add_group'] as $access => $dummy) {
75 $group = $acl['_new_group'][$access];
76 $acl[$access][$group] = isset($acl['_new_perm'][$access]) ? 1 : 0;
78 unset($acl['_add_group']);
80 unset($acl['_new_group']); unset($acl['_new_perm']);
81 if (isset($acl['_del_group'])) {
82 //del groups with perm
83 foreach ($acl['_del_group'] as $access => $del) {
84 while (list($group,$dummy) = each($del))
85 unset($acl[$access][$group]);
87 unset($acl['_del_group']);
89 if ($perm = new PagePermission($acl)) {
91 foreach ($pages as $pagename) {
92 // check if unchanged? we need a deep array_equal
93 $page = $dbi->getPage($pagename);
94 $oldperm = getPagePermissions($page);
97 if ($oldperm and $perm->equal($oldperm->perm)) {
98 $result->setAttr('class', 'error');
99 $result->pushContent(HTML::p(fmt("ACL not changed for page '%s'.",$pagename)));
100 } elseif (mayAccessPage('change', $pagename)) {
101 setPagePermissions ($page, $perm);
102 $result->setAttr('class', 'feedback');
103 $result->pushContent(HTML::p(fmt("ACL changed for page '%s'",
105 $result->pushContent(HTML::p(fmt("from '%s'",
106 $oldperm ? $oldperm->asAclGroupLines() : "None")));
107 $result->pushContent(HTML::p(fmt("to '%s'.",
108 $perm->asAclGroupLines())));
110 // Create new revision so that ACL change appears in history.
111 $current = $page->getCurrentRevision();
112 $version = $current->getVersion();
113 $meta = $current->_data;
114 $text = $current->getPackedContent();
115 $meta['summary'] = sprintf(_("ACL changed for page '%s' from '%s' to '%s'."),
117 $oldperm ? $oldperm->asAclGroupLines() : "None",
118 $perm->asAclGroupLines());
119 $meta['is_minor_edit'] = 1;
120 $meta['author'] = $request->_user->UserName();
121 unset($meta['mtime']); // force new date
122 $page->save($text, $version + 1, $meta);
126 $result->setAttr('class', 'error');
127 $result->pushContent(HTML::p(fmt("Access denied to change page '%s'.",$pagename)));
131 $result->pushContent(HTML::p(fmt("Invalid ACL")));
135 $result->setAttr('class', 'feedback');
137 $result->pushContent(HTML::p(fmt("%s pages have been changed.",$count)));
140 $result->setAttr('class', 'error');
141 $result->pushContent(HTML::p(fmt("No pages changed.")));
146 function run($dbi, $argstr, &$request, $basepage) {
148 // return $this->disabled("WikiAdminSetAcl not yet enabled. Set DEBUG to try it.");
149 if ($request->getArg('action') != 'browse')
150 if ($request->getArg('action') != _("PhpWikiAdministration/SetAcl"))
151 return $this->disabled("(action != 'browse')");
152 if (!ENABLE_PAGEPERM)
153 return $this->disabled("ENABLE_PAGEPERM = false");
155 $args = $this->getArgs($argstr, $request);
156 $this->_args = $args;
157 $this->preSelectS($args, $request);
159 $p = $request->getArg('p');
160 $post_args = $request->getArg('admin_setacl');
161 $next_action = 'select';
163 if ($p && !$request->isPost())
165 elseif ($this->_list)
166 $pages = $this->_list;
167 $header = HTML::fieldset();
168 if ($p && $request->isPost() &&
169 !empty($post_args['acl']) && empty($post_args['cancel'])) {
170 // without individual PagePermissions:
171 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
172 $request->_notAuthorized(WIKIAUTH_ADMIN);
173 $this->disabled("! user->isAdmin");
175 if ($post_args['action'] == 'verify') {
177 return $this->setaclPages($request, array_keys($p), $request->getArg('acl'));
179 if ($post_args['action'] == 'select') {
180 if (!empty($post_args['acl']))
181 $next_action = 'verify';
182 foreach ($p as $name => $c) {
187 if ($next_action == 'select' and empty($pages)) {
188 // List all pages to select from.
189 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
191 if ($next_action == 'verify') {
192 $args['info'] = "checkbox,pagename,perm,mtime,owner,author";
194 $pagelist = new PageList_Selectable($args['info'],
196 array('types' => array(
198 => new _PageList_Column_perm('perm', _("Permission")),
200 => new _PageList_Column_acl('acl', _("ACL")))));
202 $pagelist->addPageList($pages);
203 if ($next_action == 'verify') {
204 $button_label = _("Yes");
205 $header = $this->setaclForm($header, $post_args, $pages);
206 $header->pushContent(
207 HTML::p(HTML::strong(
208 _("Are you sure you want to permanently change access rights to the selected files?"))));
211 $button_label = _("Change Access Rights");
212 $header = $this->setaclForm($header, $post_args, $pages);
213 $header->pushContent(HTML::legend(_("Select the pages where to change access rights")));
216 $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
217 Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
218 $header->pushContent($buttons);
220 return HTML::form(array('action' => $request->getPostURL(),
223 $pagelist->getContent(),
224 HiddenInputs($request->getArgs(),
226 array('admin_setacl')),
227 HiddenInputs(array('admin_setacl[action]' => $next_action)),
230 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
233 function setaclForm(&$header, $post_args, $pagehash) {
234 $acl = $post_args['acl'];
236 //FIXME: find intersection of all pages perms, not just from the last pagename
238 foreach ($pagehash as $name => $checked) {
239 if ($checked) $pages[] = $name;
241 $perm_tree = pagePermissions($name);
242 $table = pagePermissionsAclFormat($perm_tree, !empty($pages));
243 $header->pushContent(HTML::strong(_("Selected Pages: ")), HTML::tt(join(', ',$pages)), HTML::br());
244 $first_page = $GLOBALS['request']->_dbi->getPage($name);
245 $owner = $first_page->getOwner();
246 list($type, $perm) = pagePermissionsAcl($perm_tree[0], $perm_tree);
247 //if (DEBUG) $header->pushContent(HTML::pre("Permission tree for $name:\n",print_r($perm_tree,true)));
248 if ($type == 'inherited')
249 $type = sprintf(_("page permission inherited from %s"), $perm_tree[1][0]);
250 elseif ($type == 'page')
251 $type = _("individual page permission");
252 elseif ($type == 'default')
253 $type = _("default page permission");
254 $header->pushContent(HTML::strong(_("Type").': '), HTML::tt($type),HTML::br());
255 $header->pushContent(HTML::strong(_("ACL").': '), HTML::tt($perm->asAclGroupLines()),HTML::br());
257 $header->pushContent(HTML::p(HTML::strong(_("Description").': '),
258 _("Selected Grant checkboxes allow access, unselected checkboxes deny access."),
259 _("To ignore delete the line."),
260 _("To add check 'Add' near the dropdown list.")
262 $header->pushContent($table);
264 // display array of checkboxes for existing perms
265 // and a dropdown for user/group to add perms.
266 // disabled if inherited,
267 // checkbox to disable inheritance,
268 // another checkbox to progate new permissions to all childs (if there exist some)
270 // warn if more pages are selected and they have different perms
271 //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
272 // 'value' => $post_args['acl'])));
273 $header->pushContent(HTML::br());
274 if (!empty($pages) and defined('EXPERIMENTAL') and EXPERIMENTAL) {
275 $checkbox = HTML::input(array('type' => 'checkbox',
276 'name' => 'admin_setacl[updatechildren]',
278 if (!empty($post_args['updatechildren'])) $checkbox->setAttr('checked','checked');
279 $header->pushContent($checkbox,
280 _("Propagate new permissions to all subpages?"),
281 HTML::raw(" "),
282 HTML::em(_("(disable individual page permissions, enable inheritance)?")),
283 HTML::br(),HTML::em(_("(Currently not working)"))
286 $header->pushContent(HTML::hr());
291 class _PageList_Column_acl extends _PageList_Column {
292 function _getValue ($page_handle, &$revision_handle) {
293 $perm_tree = pagePermissions($page_handle->_pagename);
295 list($type, $perm) = pagePermissionsAcl($perm_tree[0], $perm_tree);
296 if ($type == 'inherited') {
297 $type = sprintf(_("page permission inherited from %s"), $perm_tree[1][0]);
298 } elseif ($type == 'page') {
299 $type = _("individual page permission");
300 } elseif ($type == 'default') {
301 $type = _("default page permission");
303 $result = HTML::span();
304 $result->pushContent($type);
305 $result->pushContent(HTML::br());
306 $result->pushContent($perm->asAclGroupLines());
311 class _PageList_Column_perm extends _PageList_Column {
312 function _getValue ($page_handle, &$revision_handle) {
313 $perm_array = pagePermissions($page_handle->_pagename);
314 return pagePermissionsSimpleFormat($perm_array,
315 $page_handle->get('author'),
316 $page_handle->get('group'));
324 // c-hanging-comment-ender-p: nil
325 // indent-tabs-mode: nil