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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Set individual PagePermissions
27 * Usage: <<WikiAdminSetAcl >> or called via WikiAdminSelect
28 * Author: Reini Urban <rurban@x-ray.at>
30 * TODO: UI to add custom group/username.
31 * Currently it's easier to dump a page, fix it manually and
32 * import it, than use Setacl
34 require_once 'lib/PageList.php';
35 require_once 'lib/plugin/WikiAdminSelect.php';
37 class WikiPlugin_WikiAdminSetAcl
38 extends WikiPlugin_WikiAdminSelect
42 return _("WikiAdminSetAcl");
45 function getDescription()
47 return _("Set individual page permissions.");
50 function getDefaultArguments()
54 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
56 'p' => "[]", // list of pages
57 /* Columns to include in listing */
58 'info' => 'pagename,perm,mtime,owner,author',
62 function setaclPages(&$request, $pages, $acl)
64 $result = HTML::div();
66 $dbi =& $request->_dbi;
67 // check new_group and new_perm
68 if (isset($acl['_add_group'])) {
69 //add groups with perm
70 foreach ($acl['_add_group'] as $access => $dummy) {
71 $group = $acl['_new_group'][$access];
72 $acl[$access][$group] = isset($acl['_new_perm'][$access]) ? 1 : 0;
74 unset($acl['_add_group']);
76 unset($acl['_new_group']);
77 unset($acl['_new_perm']);
78 if (isset($acl['_del_group'])) {
79 //del groups with perm
80 foreach ($acl['_del_group'] as $access => $del) {
81 while (list($group, $dummy) = each($del))
82 unset($acl[$access][$group]);
84 unset($acl['_del_group']);
86 if ($perm = new PagePermission($acl)) {
88 foreach ($pages as $pagename) {
89 // check if unchanged? we need a deep array_equal
90 $page = $dbi->getPage($pagename);
91 $oldperm = getPagePermissions($page);
94 if ($oldperm and $perm->equal($oldperm->perm)) {
95 $result->setAttr('class', 'error');
96 $result->pushContent(HTML::p(fmt("ACL not changed for page '%s'.", $pagename)));
97 } elseif (mayAccessPage('change', $pagename)) {
98 setPagePermissions($page, $perm);
99 $result->setAttr('class', 'feedback');
100 $result->pushContent(HTML::p(fmt("ACL changed for page '%s'",
102 $result->pushContent(HTML::p(fmt("from '%s'",
103 $oldperm ? $oldperm->asAclLines() : "None")));
104 $result->pushContent(HTML::p(fmt("to '%s'.",
105 $perm->asAclLines())));
107 // Create new revision so that ACL change appears in history.
108 $current = $page->getCurrentRevision();
109 $version = $current->getVersion();
110 $meta = $current->_data;
111 $text = $current->getPackedContent();
112 $meta['summary'] = sprintf(_("ACL changed for page '%s' from '%s' to '%s'."),
114 $oldperm ? $oldperm->asAclLines() : "None",
115 $perm->asAclLines());
116 $meta['is_minor_edit'] = 1;
117 $meta['author'] = $request->_user->UserName();
118 unset($meta['mtime']); // force new date
119 $page->save($text, $version + 1, $meta);
123 $result->setAttr('class', 'error');
124 $result->pushContent(HTML::p(fmt("Access denied to change page '%s'.", $pagename)));
128 $result->pushContent(HTML::p(_("Invalid ACL")));
132 $result->setAttr('class', 'feedback');
134 $result->pushContent(HTML::p(fmt("%d pages have been changed.", $count)));
137 $result->setAttr('class', 'error');
138 $result->pushContent(HTML::p(_("No pages changed.")));
143 function run($dbi, $argstr, &$request, $basepage)
145 if ($request->getArg('action') != 'browse') {
146 if ($request->getArg('action') != _("PhpWikiAdministration/SetAcl")) {
147 return $this->disabled(_("Plugin not run: not in browse mode"));
150 if (!ENABLE_PAGEPERM) {
151 return $this->disabled("ENABLE_PAGEPERM = false");
154 $args = $this->getArgs($argstr, $request);
155 $this->_args = $args;
156 $this->preSelectS($args, $request);
158 $p = $request->getArg('p');
159 $post_args = $request->getArg('admin_setacl');
160 $next_action = 'select';
162 if ($p && !$request->isPost())
164 elseif ($this->_list)
165 $pages = $this->_list;
166 $header = HTML::fieldset();
167 if ($p && $request->isPost() &&
168 !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?"))));
210 $button_label = _("Change Access Rights");
211 $header = $this->setaclForm($header, $post_args, $pages);
212 $header->pushContent(HTML::legend(_("Select the pages where to change access rights")));
215 $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
216 Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
217 $header->pushContent($buttons);
219 return HTML::form(array('action' => $request->getPostURL(),
222 $pagelist->getContent(),
223 HiddenInputs($request->getArgs(),
225 array('admin_setacl')),
226 HiddenInputs(array('admin_setacl[action]' => $next_action)),
229 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
232 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"); elseif ($type == 'default')
252 $type = _("default page permission");
253 $header->pushContent(HTML::strong(_("Type") . _(": ")), HTML::tt($type), HTML::br());
254 $header->pushContent(HTML::strong(_("ACL") . _(": ")), HTML::tt($perm->asAclLines()), HTML::br());
256 $header->pushContent(HTML::p(HTML::strong(_("Description") . _(": ")),
257 _("Selected Grant checkboxes allow access, unselected checkboxes deny access."),
258 _("To ignore delete the line."),
259 _("To add check 'Add' near the dropdown list.")
261 $header->pushContent($table);
263 // display array of checkboxes for existing perms
264 // and a dropdown for user/group to add perms.
265 // disabled if inherited,
266 // checkbox to disable inheritance,
267 // another checkbox to progate new permissions to all childs (if there exist some)
269 // warn if more pages are selected and they have different perms
270 //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
271 // 'value' => $post_args['acl'])));
272 $header->pushContent(HTML::br());
273 if (!empty($pages) and defined('EXPERIMENTAL') and EXPERIMENTAL) {
274 $checkbox = HTML::input(array('type' => 'checkbox',
275 'name' => 'admin_setacl[updatechildren]',
277 if (!empty($post_args['updatechildren'])) $checkbox->setAttr('checked', 'checked');
278 $header->pushContent($checkbox,
279 _("Propagate new permissions to all subpages?"),
280 HTML::raw(" "),
281 HTML::em(_("(disable individual page permissions, enable inheritance)?")),
282 HTML::br(), HTML::em(_("(Currently not working)"))
285 $header->pushContent(HTML::hr());
294 // c-hanging-comment-ender-p: nil
295 // indent-tabs-mode: nil