]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSetAcl.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSetAcl.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright 2004 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 /**
25  * Set individual PagePermissions
26  *
27  * Usage:   <?plugin WikiAdminSetAcl ?> or called via WikiAdminSelect
28  * Author:  Reini Urban <rurban@x-ray.at>
29  *
30  * KNOWN ISSUES:
31  * Requires PHP 4.2 so far.
32  *
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
36  */
37 require_once('lib/PageList.php');
38 require_once('lib/plugin/WikiAdminSelect.php');
39
40 class WikiPlugin_WikiAdminSetAcl
41 extends WikiPlugin_WikiAdminSelect
42 {
43     function getName() {
44         return _("WikiAdminSetAcl");
45     }
46
47     function getDescription() {
48         return _("Set individual page permissions.");
49     }
50
51     function getVersion() {
52         return preg_replace("/[Revision: $]/", '',
53                             "\$Revision$");
54     }
55
56     function getDefaultArguments() {
57         return array_merge
58             (
59              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
60              array(
61                      'p'        => "[]",  // list of pages
62                      /* Columns to include in listing */
63                      'info'     => 'pagename,perm,mtime,owner,author',
64                      ));
65     }
66
67     function setaclPages(&$request, $pages, $acl) {
68         $result = HTML::div();
69         $count = 0;
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;
77             }
78             unset($acl['_add_group']);
79         }
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]);
86             }
87             unset($acl['_del_group']);
88         }
89         if ($perm = new PagePermission($acl)) {
90             $perm->sanify();
91             foreach ($pages as $pagename) {
92                     // check if unchanged? we need a deep array_equal
93                     $page = $dbi->getPage($pagename);
94                     $oldperm = getPagePermissions($page);
95                 if ($oldperm)
96                     $oldperm->sanify();
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'",
104                                                      $pagename)));
105                     $result->pushContent(HTML::p(fmt("from '%s'",
106                                                      $oldperm ? $oldperm->asAclGroupLines() : "None")));
107                     $result->pushContent(HTML::p(fmt("to '%s'.",
108                                                      $perm->asAclGroupLines())));
109
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'."),
116                                                $pagename,
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);
123
124                     $count++;
125                 } else {
126                     $result->setAttr('class', 'error');
127                     $result->pushContent(HTML::p(fmt("Access denied to change page '%s'.",$pagename)));
128                 }
129             }
130         } else {
131             $result->pushContent(HTML::p(fmt("Invalid ACL")));
132         }
133         if ($count) {
134             $dbi->touch();
135             $result->setAttr('class', 'feedback');
136             if ($count > 1) {
137                 $result->pushContent(HTML::p(fmt("%s pages have been changed.",$count)));
138             }
139         } else {
140             $result->setAttr('class', 'error');
141             $result->pushContent(HTML::p(fmt("No pages changed.")));
142         }
143         return $result;
144     }
145
146     function run($dbi, $argstr, &$request, $basepage) {
147         //if (!DEBUG)
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");
154
155         $args = $this->getArgs($argstr, $request);
156         $this->_args = $args;
157         $this->preSelectS($args, $request);
158
159         $p = $request->getArg('p');
160         $post_args = $request->getArg('admin_setacl');
161         $next_action = 'select';
162         $pages = array();
163         if ($p && !$request->isPost())
164             $pages = $p;
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");
174             }
175             if ($post_args['action'] == 'verify') {
176                 // Real action
177                 return $this->setaclPages($request, array_keys($p), $request->getArg('acl'));
178             }
179             if ($post_args['action'] == 'select') {
180                 if (!empty($post_args['acl']))
181                     $next_action = 'verify';
182                 foreach ($p as $name => $c) {
183                     $pages[$name] = 1;
184                 }
185             }
186         }
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']);
190         }
191         if ($next_action == 'verify') {
192             $args['info'] = "checkbox,pagename,perm,mtime,owner,author";
193         }
194         $pagelist = new PageList_Selectable($args['info'],
195                                             $args['exclude'],
196                                             array('types' => array(
197                                                   'perm'
198                                                   => new _PageList_Column_perm('perm', _("Permission")),
199                                                   'acl'
200                                                   => new _PageList_Column_acl('acl', _("ACL")))));
201
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?"))));
209         }
210         else {
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")));
214         }
215
216         $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
217                            Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
218         $header->pushContent($buttons);
219
220         return HTML::form(array('action' => $request->getPostURL(),
221                                 'method' => 'post'),
222                           $header,
223                           $pagelist->getContent(),
224                           HiddenInputs($request->getArgs(),
225                                         false,
226                                         array('admin_setacl')),
227                           HiddenInputs(array('admin_setacl[action]' => $next_action)),
228                           ENABLE_PAGEPERM
229                           ? ''
230                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
231     }
232
233     function setaclForm(&$header, $post_args, $pagehash) {
234         $acl = $post_args['acl'];
235
236         //FIXME: find intersection of all pages perms, not just from the last pagename
237         $pages = array();
238         foreach ($pagehash as $name => $checked) {
239            if ($checked) $pages[] = $name;
240         }
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());
256
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.")
261                                      ));
262         $header->pushContent($table);
263         //
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)
269         //Todo:
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]',
277                                         'value' => 1));
278           if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
279           $header->pushContent($checkbox,
280                     _("Propagate new permissions to all subpages?"),
281                   HTML::raw("&nbsp;&nbsp;"),
282                   HTML::em(_("(disable individual page permissions, enable inheritance)?")),
283                   HTML::br(),HTML::em(_("(Currently not working)"))
284                                );
285         }
286         $header->pushContent(HTML::hr());
287         return $header;
288     }
289 }
290
291 class _PageList_Column_acl extends _PageList_Column {
292     function _getValue ($page_handle, &$revision_handle) {
293         $perm_tree = pagePermissions($page_handle->_pagename);
294
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");
302         }
303         $result = HTML::span();
304         $result->pushContent($type);
305         $result->pushContent(HTML::br());
306         $result->pushContent($perm->asAclGroupLines());
307         return $result;
308     }
309 };
310
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'));
317     }
318 };
319
320 // Local Variables:
321 // mode: php
322 // tab-width: 8
323 // c-basic-offset: 4
324 // c-hanging-comment-ender-p: nil
325 // indent-tabs-mode: nil
326 // End:
327 ?>