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