source: branches/branch-1_7/admin/cat_perm.php @ 2745

Last change on this file since 2745 was 2348, checked in by vdigital, 17 years ago

Bug 703: Authorization system in Cat management works now with well ordered lists

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: cat_perm.php 2348 2008-05-17 20:16:45Z vdigital $
8// | last update   : $Date: 2008-05-17 20:16:45 +0000 (Sat, 17 May 2008) $
9// | last modifier : $Author: vdigital $
10// | revision      : $Revision: 2348 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27if (!defined('PHPWG_ROOT_PATH'))
28{
29  die ("Hacking attempt!");
30}
31
32include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
33
34// +-----------------------------------------------------------------------+
35// | Check Access and exit when user status is not ok                      |
36// +-----------------------------------------------------------------------+
37check_status(ACCESS_ADMINISTRATOR);
38
39// +-----------------------------------------------------------------------+
40// |                       variable initialization                         |
41// +-----------------------------------------------------------------------+
42
43// if the category is not correct (not numeric, not private)
44if (isset($_GET['cat']) and is_numeric($_GET['cat']))
45{
46  $query = '
47SELECT status
48  FROM '.CATEGORIES_TABLE.'
49  WHERE id = '.$_GET['cat'].'
50;';
51  list($status) = mysql_fetch_array(pwg_query($query));
52 
53  if ('private' == $status)
54  {
55    $page['cat'] = $_GET['cat'];
56  }
57}
58
59if (!isset($page['cat']))
60{
61  $query = '
62SELECT id
63  FROM '.CATEGORIES_TABLE.'
64  WHERE status = \'private\'
65  LIMIT 0,1
66;';
67
68  list($page['cat']) = mysql_fetch_array(pwg_query($query));
69}
70
71// +-----------------------------------------------------------------------+
72// |                           form submission                             |
73// +-----------------------------------------------------------------------+
74
75if (isset($_POST) and false)
76{
77  echo '<pre>';
78  print_r($_POST);
79  echo '</pre>';
80}
81
82if (isset($_POST['deny_groups_submit'])
83         and isset($_POST['deny_groups'])
84         and count($_POST['deny_groups']) > 0)
85{
86  // if you forbid access to a category, all sub-categories become
87  // automatically forbidden
88  $query = '
89DELETE
90  FROM '.GROUP_ACCESS_TABLE.'
91  WHERE group_id IN ('.implode(',', $_POST['deny_groups']).')
92    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
93;';
94  pwg_query($query);
95}
96else if (isset($_POST['grant_groups_submit'])
97         and isset($_POST['grant_groups'])
98         and count($_POST['grant_groups']) > 0)
99{
100  $query = '
101SELECT id
102  FROM '.CATEGORIES_TABLE.'
103  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
104  AND status = \'private\'
105;';
106  $private_uppercats = array_from_query($query, 'id');
107
108  // We must not reinsert already existing lines in group_access table
109  $granteds = array();
110  foreach ($private_uppercats as $cat_id)
111  {
112    $granteds[$cat_id] = array();
113  }
114 
115  $query = '
116SELECT group_id, cat_id
117  FROM '.GROUP_ACCESS_TABLE.'
118  WHERE cat_id IN ('.implode(',', $private_uppercats).')
119    AND group_id IN ('.implode(',', $_POST['grant_groups']).')
120;';
121  $result = pwg_query($query);
122  while ($row = mysql_fetch_array($result))
123  {
124    array_push($granteds[$row['cat_id']], $row['group_id']);
125  }
126
127  $inserts = array();
128 
129  foreach ($private_uppercats as $cat_id)
130  {
131    $group_ids = array_diff($_POST['grant_groups'], $granteds[$cat_id]);
132    foreach ($group_ids as $group_id)
133    {
134      array_push($inserts, array('group_id' => $group_id,
135                                 'cat_id' => $cat_id));
136    }
137  }
138
139  mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
140}
141else if (isset($_POST['deny_users_submit'])
142         and isset($_POST['deny_users'])
143         and count($_POST['deny_users']) > 0)
144{
145  // if you forbid access to a category, all sub-categories become
146  // automatically forbidden
147  $query = '
148DELETE
149  FROM '.USER_ACCESS_TABLE.'
150  WHERE user_id IN ('.implode(',', $_POST['deny_users']).')
151    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
152;';
153  pwg_query($query);
154}
155else if (isset($_POST['grant_users_submit'])
156         and isset($_POST['grant_users'])
157         and count($_POST['grant_users']) > 0)
158{
159  $query = '
160SELECT id
161  FROM '.CATEGORIES_TABLE.'
162  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
163  AND status = \'private\'
164;';
165  $private_uppercats = array_from_query($query, 'id');
166
167  // We must not reinsert already existing lines in user_access table
168  $granteds = array();
169  foreach ($private_uppercats as $cat_id)
170  {
171    $granteds[$cat_id] = array();
172  }
173 
174  $query = '
175SELECT user_id, cat_id
176  FROM '.USER_ACCESS_TABLE.'
177  WHERE cat_id IN ('.implode(',', $private_uppercats).')
178    AND user_id IN ('.implode(',', $_POST['grant_users']).')
179;';
180  $result = pwg_query($query);
181  while ($row = mysql_fetch_array($result))
182  {
183    array_push($granteds[$row['cat_id']], $row['user_id']);
184  }
185
186  $inserts = array();
187 
188  foreach ($private_uppercats as $cat_id)
189  {
190    $user_ids = array_diff($_POST['grant_users'], $granteds[$cat_id]);
191    foreach ($user_ids as $user_id)
192    {
193      array_push($inserts, array('user_id' => $user_id,
194                                 'cat_id' => $cat_id));
195    }
196  }
197
198  mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts);
199}
200
201// +-----------------------------------------------------------------------+
202// |                       template initialization                         |
203// +-----------------------------------------------------------------------+
204
205$template->set_filenames(array('cat_perm'=>'admin/cat_perm.tpl'));
206
207$template->assign_vars(
208  array(
209    'CATEGORIES_NAV' =>
210      get_cat_display_name_from_id(
211        $page['cat'],
212        'admin.php?page=cat_modify&amp;cat_id='
213        ),
214    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=cat_perm',
215    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=cat_perm&amp;cat='.$page['cat']
216    )
217  );
218
219// +-----------------------------------------------------------------------+
220// |                          form construction                            |
221// +-----------------------------------------------------------------------+
222
223// groups denied are the groups not granted. So we need to find all groups
224// minus groups granted to find groups denied.
225
226$groups = array();
227
228$query = '
229SELECT id, name
230  FROM '.GROUPS_TABLE.'
231  ORDER BY name ASC
232;';
233$result = pwg_query($query);
234
235while ($row = mysql_fetch_array($result))
236{
237  $groups[$row['id']] = $row['name'];
238}
239
240$query = '
241SELECT group_id
242  FROM '.GROUP_ACCESS_TABLE.'
243  WHERE cat_id = '.$page['cat'].'
244;';
245$group_granted_ids = array_from_query($query, 'group_id');
246$group_granted_ids = order_by_name($group_granted_ids, $groups);
247// groups granted to access the category
248foreach ($group_granted_ids as $group_id)
249{
250  $template->assign_block_vars(
251    'group_granted',
252    array(
253      'NAME'=>$groups[$group_id],
254      'ID'=>$group_id
255      )
256    );
257}
258$group_denied_ids = array_diff(array_keys($groups), $group_granted_ids);
259$group_denied_ids = order_by_name($group_denied_ids, $groups);
260// groups denied
261foreach ($group_denied_ids as $group_id)
262{
263  $template->assign_block_vars(
264    'group_denied',
265    array(
266      'NAME'=>$groups[$group_id],
267      'ID'=>$group_id
268      )
269    );
270}
271
272// users...
273$users = array();
274
275$query = '
276SELECT '.$conf['user_fields']['id'].' AS id,
277       '.$conf['user_fields']['username'].' AS username
278  FROM '.USERS_TABLE.'
279;';
280$result = pwg_query($query);
281while($row = mysql_fetch_array($result))
282{
283  $users[$row['id']] = $row['username'];
284}
285
286$query = '
287SELECT user_id
288  FROM '.USER_ACCESS_TABLE.'
289  WHERE cat_id = '.$page['cat'].'
290;';
291$user_granted_direct_ids = array_from_query($query, 'user_id');
292$user_granted_direct_ids = order_by_name($user_granted_direct_ids, $users);
293foreach ($user_granted_direct_ids as $user_id)
294{
295  $template->assign_block_vars(
296    'user_granted',
297    array(
298      'NAME'=>$users[$user_id],
299      'ID'=>$user_id
300      )
301    );
302}
303
304$user_granted_indirect_ids = array();
305if (count($group_granted_ids) > 0)
306{
307  $granted_groups = array();
308
309  $query = '
310SELECT user_id, group_id
311  FROM '.USER_GROUP_TABLE.'
312  WHERE group_id IN ('.implode(',', $group_granted_ids).')
313';
314  $result = pwg_query($query);
315  while ($row = mysql_fetch_array($result))
316  {
317    if (!isset($granted_groups[$row['group_id']]))
318    {
319      $granted_groups[$row['group_id']] = array();
320    }
321    array_push($granted_groups[$row['group_id']], $row['user_id']);
322  }
323
324  $user_granted_by_group_ids = array();
325
326  foreach ($granted_groups as $group_users)
327  {
328    $user_granted_by_group_ids = array_merge($user_granted_by_group_ids,
329                                             $group_users);
330  }
331  $user_granted_by_group_ids = array_unique($user_granted_by_group_ids);
332 
333 
334  $user_granted_indirect_ids = array_diff($user_granted_by_group_ids,
335                                          $user_granted_direct_ids);
336  $user_granted_indirect_ids = 
337    order_by_name($user_granted_indirect_ids, $users);
338  foreach ($user_granted_indirect_ids as $user_id)
339  {
340    $group = '';
341   
342    foreach ($granted_groups as $group_id => $group_users)
343    {
344      if (in_array($user_id, $group_users))
345      {
346        $group = $groups[$group_id];
347        break;
348      }
349    }
350   
351    $template->assign_block_vars(
352      'user_granted_indirect',
353      array(
354        'NAME'=>$users[$user_id],
355        'GROUP'=>$group
356        )
357      );
358  }
359}
360
361$user_denied_ids = array_diff(array_keys($users),
362                              $user_granted_indirect_ids,
363                              $user_granted_direct_ids);
364$user_denied_ids = order_by_name($user_denied_ids, $users);
365foreach ($user_denied_ids as $user_id)
366{
367  $template->assign_block_vars(
368    'user_denied',
369    array(
370      'NAME'=>$users[$user_id],
371      'ID'=>$user_id
372      )
373    );
374}
375
376// Warning: this function breaks original keys
377// This function should be move in the futur to ./include/functions_html.inc
378function order_by_name($element_ids,$name)
379{
380  $ordered_element_ids = array();
381  foreach ($element_ids as $k_id => $element_id)
382  {
383    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
384    $ordered_element_ids[$key] = $element_id;
385  }
386  ksort($ordered_element_ids);
387  return $ordered_element_ids;
388}
389
390// +-----------------------------------------------------------------------+
391// |                           sending html code                           |
392// +-----------------------------------------------------------------------+
393$template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm');
394?>
Note: See TracBrowser for help on using the repository browser.