]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSetAcl.php
new support for arguments owner, author, creator in most relevant
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSetAcl.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSetAcl.php,v 1.17 2004-06-13 15:33:20 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * Set individual PagePermissions
25  *
26  * Usage:   <?plugin WikiAdminSetAcl ?> or called via WikiAdminSelect
27  * Author:  Reini Urban <rurban@x-ray.at>
28  *
29  * KNOWN ISSUES:
30  * Requires PHP 4.2 so far.
31  */
32 require_once('lib/PageList.php');
33 require_once('lib/plugin/WikiAdminSelect.php');
34
35 class WikiPlugin_WikiAdminSetAcl
36 extends WikiPlugin_WikiAdminSelect
37 {
38     function getName() {
39         return _("WikiAdminSetAcl");
40     }
41
42     function getDescription() {
43         return _("Set individual page permissions.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.17 $");
49     }
50
51     function getDefaultArguments() {
52         return array(
53                      'p'        => "[]",  // list of pages
54                      's'        => false, /* select by pagename */
55                      /* select pages by meta-data: */
56                      'author'   => false,
57                      'owner'    => false,
58                      'creator'  => false,
59                      /* Pages to exclude in listing */
60                      'exclude'  => '',
61                      /* Columns to include in listing */
62                      'info'     => 'pagename,perm,mtime,owner,author',
63                      /* How to sort */
64                      'sortby'   => 'pagename',
65                      'limit'    => 0,
66                      );
67     }
68
69     function setaclPages(&$request, $pages, $acl) {
70         $ul = HTML::ul();
71         $count = 0;
72         $dbi =& $request->_dbi; 
73         // check new_group and new_perm
74         if (isset($acl['_add_group'])) {
75             //add groups with perm
76             foreach ($acl['_add_group'] as $access => $dummy) {
77                 $group = $acl['_new_group'][$access];
78                 $acl[$access][$group] = isset($acl['_new_perm'][$access]) ? 1 : 0;
79             }
80             unset($acl['_add_group']); 
81         }
82         unset($acl['_new_group']); unset($acl['_new_perm']);
83         if (isset($acl['_del_group'])) {
84             //del groups with perm
85             foreach ($acl['_del_group'] as $access => $del) {
86                 while (list($group,$dummy) = each($del)) 
87                     unset($acl[$access][$group]);
88             }
89             unset($acl['_del_group']);
90         }
91         if ($perm = new PagePermission($acl)) {
92             $perm->sanify();
93             foreach ($pages as $pagename) {
94                 // check if unchanged? we need a deep array_equal
95                 $page = $dbi->getPage($pagename);
96                 $oldperm = getPagePermissions($page);
97                 if ($oldperm)
98                     $oldperm->sanify();
99                 if ($oldperm and $perm->equal($oldperm->perm)) // (serialize($oldperm->perm) == serialize($perm->perm))
100                     $ul->pushContent(HTML::li(fmt("ACL not changed for page '%s'.",$pagename)));
101                 elseif (mayAccessPage('change', $pagename)) {
102                     setPagePermissions ($page, $perm);
103                     $ul->pushContent(HTML::li(fmt("ACL changed for page '%s'.",$pagename)));
104                     $count++;
105                 } else {
106                     $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
107                 }
108             }
109         } else {
110             $ul->pushContent(HTML::li(fmt("Invalid ACL")));
111         }
112         if ($count) {
113             $dbi->touch();
114             return HTML($ul,
115                         HTML::p(fmt("%s pages have been changed.",$count)));
116         } else {
117             return HTML($ul,
118                         HTML::p(fmt("No pages changed.")));
119         }
120     }
121     
122     function run($dbi, $argstr, &$request, $basepage) {
123         //if (!DEBUG)
124         //    return $this->disabled("WikiAdminSetAcl not yet enabled. Set DEBUG to try it.");
125         if ($request->getArg('action') != 'browse')
126             if ($request->getArg('action') != _("PhpWikiAdministration/SetAcl"))
127                 return $this->disabled("(action != 'browse')");
128         
129         $args = $this->getArgs($argstr, $request);
130         $this->_args = $args;
131         if (!empty($args['exclude']))
132             $exclude = explodePageList($args['exclude']);
133         else
134             $exclude = false;
135         $this->preSelectS(&$args, &$request);
136
137         $p = $request->getArg('p');
138         $post_args = $request->getArg('admin_setacl');
139         $next_action = 'select';
140         $pages = array();
141         if ($p && !$request->isPost())
142             $pages = $p;
143         elseif ($this->_list)
144             $pages = $this->_list;
145         $header = HTML::p();
146         if ($p && $request->isPost() &&
147             !empty($post_args['acl']) && empty($post_args['cancel'])) {
148             // without individual PagePermissions:
149             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
150                 $request->_notAuthorized(WIKIAUTH_ADMIN);
151                 $this->disabled("! user->isAdmin");
152             }
153             if ($post_args['action'] == 'verify') {
154                 // Real action
155                 $header->pushContent(
156                     $this->setaclPages($request, array_keys($p),
157                                        $request->getArg('acl')));
158             }
159             if ($post_args['action'] == 'select') {
160                 if (!empty($post_args['acl']))
161                     $next_action = 'verify';
162                 foreach ($p as $name => $c) {
163                     $pages[$name] = 1;
164                 }
165             }
166         }
167         if ($next_action == 'select' and empty($pages)) {
168             // List all pages to select from.
169             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
170         }
171         if ($next_action == 'verify') {
172             $args['info'] = "checkbox,pagename,perm,mtime,owner,author";
173         }
174         $pagelist = new PageList_Selectable($args['info'], 
175                                             $exclude,
176                                             array('types' => array(
177                                                   'perm'
178                                                   => new _PageList_Column_perm('perm', _("Permission")),
179                                                   'acl'
180                                                   => new _PageList_Column_acl('acl', _("ACL")))));
181
182         $pagelist->addPageList($pages);
183         if ($next_action == 'verify') {
184             $button_label = _("Yes");
185             $header = $this->setaclForm($header, $post_args, $pages);
186             $header->pushContent(
187               HTML::p(HTML::strong(
188                   _("Are you sure you want to permanently change access to the selected files?"))));
189         }
190         else {
191             $button_label = _("SetAcl");
192             $header = $this->setaclForm($header, $post_args, $pages);
193             $header->pushContent(HTML::p(_("Select the pages to change:")));
194         }
195
196         $buttons = HTML::p(Button('submit:admin_setacl[acl]', $button_label, 'wikiadmin'),
197                            Button('submit:admin_setacl[cancel]', _("Cancel"), 'button'));
198
199         return HTML::form(array('action' => $request->getPostURL(),
200                                 'method' => 'post'),
201                           $header,
202                           $pagelist->getContent(),
203                           HiddenInputs($request->getArgs(),
204                                         false,
205                                         array('admin_setacl')),
206                           HiddenInputs(array('admin_setacl[action]' => $next_action)),
207                           ENABLE_PAGEPERM
208                           ? ''
209                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
210                           $buttons);
211     }
212
213     function setaclForm(&$header, $post_args, $pagehash) {
214         $acl = $post_args['acl'];
215
216         //FIXME: find intersection of all pages perms, not just from the last pagename
217         $pages = array();
218         foreach ($pagehash as $name => $checked) {
219            if ($checked) $pages[] = $name;
220         }
221         $perm_tree = pagePermissions($name);
222         $table = pagePermissionsAclFormat($perm_tree, !empty($pages));
223         $header->pushContent(HTML::strong(_("Selected Pages: ")), HTML::tt(join(', ',$pages)), HTML::br());
224         $first_page = $GLOBALS['request']->_dbi->getPage($name);
225         $owner = $first_page->getOwner();
226         list($type, $perm) = pagePermissionsAcl($perm_tree[0], $perm_tree);
227         //if (DEBUG) $header->pushContent(HTML::pre("Permission tree for $name:\n",print_r($perm_tree,true)));
228         if ($type == 'inherited')
229             $type = sprintf(_("page permission inherited from %s"), $perm_tree[1][0]);
230         elseif ($type == 'page')
231             $type = _("invidual page permission");
232         elseif ($type == 'default')
233             $type = _("default page permission");
234         $header->pushContent(HTML::strong(_("Type: ")), HTML::tt($type),HTML::br());
235         $header->pushContent(HTML::strong(_("getfacl: ")), pagePermissionsSimpleFormat($perm_tree, $owner),HTML::br());
236         $header->pushContent(HTML::strong(_("ACL: ")), HTML::tt($perm->asAclLines()),HTML::br());
237         
238         $header->pushContent(HTML::p(HTML::strong(_("Description: ")),
239                                      _("Selected Grant checkboxes allow access, unselected checkboxes deny access."),
240                                      _("To ignore delete the line."),
241                                      _("To add check 'Add' near the dropdown list.")
242                                      ));
243         $header->pushContent(HTML::blockquote($table));
244         //
245         // display array of checkboxes for existing perms
246         // and a dropdown for user/group to add perms.
247         // disabled if inherited, 
248         // checkbox to disable inheritance, 
249         // another checkbox to progate new permissions to all childs (if there exist some)
250         //Todo:
251         // warn if more pages are selected and they have different perms
252         //$header->pushContent(HTML::input(array('name' => 'admin_setacl[acl]',
253         //                                       'value' => $post_args['acl'])));
254         $header->pushContent(HTML::br());
255         if (!empty($pages) and DEBUG) {
256           $checkbox = HTML::input(array('type' => 'checkbox',
257                                         'name' => 'admin_setacl[updatechildren]',
258                                         'value' => 1));
259           if (!empty($post_args['updatechildren']))  $checkbox->setAttr('checked','checked');
260           $header->pushContent($checkbox,
261                   _("Propagate new permissions to all subpages?"),
262                   HTML::raw("&nbsp;&nbsp;"),
263                   HTML::em(_("(disable individual page permissions, enable inheritance)?")),
264                   HTML::br(),HTML::em(_("(Currently not working)"))
265                                );
266         }
267         $header->pushContent(HTML::hr(),HTML::p());
268         return $header;
269     }
270 }
271
272 class _PageList_Column_acl extends _PageList_Column {
273     function _getValue ($page_handle, &$revision_handle) {
274         $perm_tree = pagePermissions($page_handle->_pagename);
275         return pagePermissionsAclFormat($perm_tree);
276         if (0) {
277             ob_start();
278             var_dump($perm_array);
279             $xml = ob_get_contents();
280             ob_end_clean();
281             return $xml;
282         }
283     }
284 };
285
286 class _PageList_Column_perm extends _PageList_Column {
287     function _getValue ($page_handle, &$revision_handle) {
288         $perm_array = pagePermissions($page_handle->_pagename);
289         return pagePermissionsSimpleFormat($perm_array,
290                                            $page_handle->get('author'),
291                                            $page_handle->get('group'));
292         if (0) {
293             ob_start();
294             var_dump($perm_array);
295             $xml = ob_get_contents();
296             ob_end_clean();
297             return $xml;
298         }
299     }
300 };
301
302 // $Log: not supported by cvs2svn $
303 // Revision 1.16  2004/06/08 13:50:43  rurban
304 // show getfacl and acl line
305 //
306 // Revision 1.15  2004/06/08 10:05:12  rurban
307 // simplified admin action shortcuts
308 //
309 // Revision 1.14  2004/06/07 22:28:06  rurban
310 // add acl field to mimified dump
311 //
312 // Revision 1.13  2004/06/04 20:32:54  rurban
313 // Several locale related improvements suggested by Pierrick Meignen
314 // LDAP fix by John Cole
315 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
316 //
317 // Revision 1.12  2004/06/03 22:24:48  rurban
318 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
319 //
320 // Revision 1.11  2004/06/01 15:28:02  rurban
321 // AdminUser only ADMIN_USER not member of Administrators
322 // some RateIt improvements by dfrankow
323 // edit_toolbar buttons
324 //
325 // Revision 1.10  2004/05/27 17:49:06  rurban
326 // renamed DB_Session to DbSession (in CVS also)
327 // added WikiDB->getParam and WikiDB->getAuthParam method to get rid of globals
328 // remove leading slash in error message
329 // added force_unlock parameter to File_Passwd (no return on stale locks)
330 // fixed adodb session AffectedRows
331 // added FileFinder helpers to unify local filenames and DATA_PATH names
332 // editpage.php: new edit toolbar javascript on ENABLE_EDIT_TOOLBAR
333 //
334 // Revision 1.9  2004/05/24 17:34:53  rurban
335 // use ACLs
336 //
337 // Revision 1.8  2004/05/16 22:32:54  rurban
338 // setacl icons
339 //
340 // Revision 1.7  2004/05/16 22:07:35  rurban
341 // check more config-default and predefined constants
342 // various PagePerm fixes:
343 //   fix default PagePerms, esp. edit and view for Bogo and Password users
344 //   implemented Creator and Owner
345 //   BOGOUSERS renamed to BOGOUSER
346 // fixed syntax errors in signin.tmpl
347 //
348 // Revision 1.5  2004/04/07 23:13:19  rurban
349 // fixed pear/File_Passwd for Windows
350 // fixed FilePassUser sessions (filehandle revive) and password update
351 //
352 // Revision 1.4  2004/03/17 20:23:44  rurban
353 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
354 //
355 // Revision 1.3  2004/03/12 13:31:43  rurban
356 // enforce PagePermissions, errormsg if not Admin
357 //
358 // Revision 1.2  2004/02/24 04:02:07  rurban
359 // Better warning messages
360 //
361 // Revision 1.1  2004/02/23 21:30:25  rurban
362 // more PagePerm stuff: (working against 1.4.0)
363 //   ACL editing and simplification of ACL's to simple rwx------ string
364 //   not yet working.
365 //
366 //
367
368 // Local Variables:
369 // mode: php
370 // tab-width: 8
371 // c-basic-offset: 4
372 // c-hanging-comment-ender-p: nil
373 // indent-tabs-mode: nil
374 // End:
375 ?>