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