source: trunk/admin/cat_perm.php @ 815

Last change on this file since 815 was 808, checked in by plg, 19 years ago
  • new : external authentication in another users table. Previous users table is divided between users (common properties with any web application) and user_infos (phpwebgallery specific informations). External table and fields can be configured.
  • modification : profile.php is not reachable through administration anymore (not useful).
  • modification : in profile.php, current password is mandatory only if user tries to change his password. Username can't be changed.
  • deletion : of obsolete functions get_user_restrictions, update_user_restrictions, get_user_all_restrictions, is_user_allowed, update_user
  • modification : user_forbidden table becomes user_cache so that not only restriction informations can be stored in this table.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-08-08 20:52:19 +0000 (Mon, 08 Aug 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 808 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28if (!defined('PHPWG_ROOT_PATH'))
29{
30  die ("Hacking attempt!");
31}
32include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
33
34// +-----------------------------------------------------------------------+
35// |                       variable initialization                         |
36// +-----------------------------------------------------------------------+
37
38// if the category is not correct (not numeric, not private)
39if (isset($_GET['cat']) and is_numeric($_GET['cat']))
40{
41  $query = '
42SELECT status
43  FROM '.CATEGORIES_TABLE.'
44  WHERE id = '.$_GET['cat'].'
45;';
46  list($status) = mysql_fetch_array(pwg_query($query));
47 
48  if ('private' == $status)
49  {
50    $page['cat'] = $_GET['cat'];
51  }
52}
53
54if (!isset($page['cat']))
55{
56  $query = '
57SELECT id
58  FROM '.CATEGORIES_TABLE.'
59  WHERE status = \'private\'
60  LIMIT 0,1
61;';
62
63  list($page['cat']) = mysql_fetch_array(pwg_query($query));
64}
65
66// +-----------------------------------------------------------------------+
67// |                           form submission                             |
68// +-----------------------------------------------------------------------+
69
70if (isset($_POST) and false)
71{
72  echo '<pre>';
73  print_r($_POST);
74  echo '</pre>';
75}
76
77if (isset($_POST['deny_groups_submit'])
78         and isset($_POST['deny_groups'])
79         and count($_POST['deny_groups']) > 0)
80{
81  // if you forbid access to a category, all sub-categories become
82  // automatically forbidden
83  $query = '
84DELETE
85  FROM '.GROUP_ACCESS_TABLE.'
86  WHERE group_id IN ('.implode(',', $_POST['deny_groups']).')
87    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
88;';
89  pwg_query($query);
90}
91else if (isset($_POST['grant_groups_submit'])
92         and isset($_POST['grant_groups'])
93         and count($_POST['grant_groups']) > 0)
94{
95  $query = '
96SELECT id
97  FROM '.CATEGORIES_TABLE.'
98  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
99  AND status = \'private\'
100;';
101  $private_uppercats = array_from_query($query, 'id');
102
103  // We must not reinsert already existing lines in group_access table
104  $granteds = array();
105  foreach ($private_uppercats as $cat_id)
106  {
107    $granteds[$cat_id] = array();
108  }
109 
110  $query = '
111SELECT group_id, cat_id
112  FROM '.GROUP_ACCESS_TABLE.'
113  WHERE cat_id IN ('.implode(',', $private_uppercats).')
114    AND group_id IN ('.implode(',', $_POST['grant_groups']).')
115;';
116  $result = pwg_query($query);
117  while ($row = mysql_fetch_array($result))
118  {
119    array_push($granteds[$row['cat_id']], $row['group_id']);
120  }
121
122  $inserts = array();
123 
124  foreach ($private_uppercats as $cat_id)
125  {
126    $group_ids = array_diff($_POST['grant_groups'], $granteds[$cat_id]);
127    foreach ($group_ids as $group_id)
128    {
129      array_push($inserts, array('group_id' => $group_id,
130                                 'cat_id' => $cat_id));
131    }
132  }
133
134  mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
135}
136else if (isset($_POST['deny_users_submit'])
137         and isset($_POST['deny_users'])
138         and count($_POST['deny_users']) > 0)
139{
140  // if you forbid access to a category, all sub-categories become
141  // automatically forbidden
142  $query = '
143DELETE
144  FROM '.USER_ACCESS_TABLE.'
145  WHERE user_id IN ('.implode(',', $_POST['deny_users']).')
146    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
147;';
148  pwg_query($query);
149}
150else if (isset($_POST['grant_users_submit'])
151         and isset($_POST['grant_users'])
152         and count($_POST['grant_users']) > 0)
153{
154  $query = '
155SELECT id
156  FROM '.CATEGORIES_TABLE.'
157  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
158  AND status = \'private\'
159;';
160  $private_uppercats = array_from_query($query, 'id');
161
162  // We must not reinsert already existing lines in user_access table
163  $granteds = array();
164  foreach ($private_uppercats as $cat_id)
165  {
166    $granteds[$cat_id] = array();
167  }
168 
169  $query = '
170SELECT user_id, cat_id
171  FROM '.USER_ACCESS_TABLE.'
172  WHERE cat_id IN ('.implode(',', $private_uppercats).')
173    AND user_id IN ('.implode(',', $_POST['grant_users']).')
174;';
175  $result = pwg_query($query);
176  while ($row = mysql_fetch_array($result))
177  {
178    array_push($granteds[$row['cat_id']], $row['user_id']);
179  }
180
181  $inserts = array();
182 
183  foreach ($private_uppercats as $cat_id)
184  {
185    $user_ids = array_diff($_POST['grant_users'], $granteds[$cat_id]);
186    foreach ($user_ids as $user_id)
187    {
188      array_push($inserts, array('user_id' => $user_id,
189                                 'cat_id' => $cat_id));
190    }
191  }
192
193  mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts);
194}
195
196// +-----------------------------------------------------------------------+
197// |                       template initialization                         |
198// +-----------------------------------------------------------------------+
199$template->set_filenames(array('cat_perm'=>'admin/cat_perm.tpl'));
200
201$form_action = PHPWG_ROOT_PATH.'admin.php';
202$form_action.= '?page=cat_perm&amp;cat='.$page['cat'];
203
204$template->assign_vars(array('F_ACTION' => $form_action));
205
206// +-----------------------------------------------------------------------+
207// |                          form construction                            |
208// +-----------------------------------------------------------------------+
209
210// groups denied are the groups not granted. So we need to find all groups
211// minus groups granted to find groups denied.
212
213$groups = array();
214
215$query = '
216SELECT id, name
217  FROM '.GROUPS_TABLE.'
218;';
219$result = pwg_query($query);
220
221while ($row = mysql_fetch_array($result))
222{
223  $groups[$row['id']] = $row['name'];
224}
225
226$query = '
227SELECT group_id
228  FROM '.GROUP_ACCESS_TABLE.'
229  WHERE cat_id = '.$page['cat'].'
230;';
231$group_granted_ids = array_from_query($query, 'group_id');
232
233// groups granted to access the category
234foreach ($group_granted_ids as $group_id)
235{
236  $template->assign_block_vars(
237    'group_granted',
238    array(
239      'NAME'=>$groups[$group_id],
240      'ID'=>$group_id
241      )
242    );
243}
244
245// groups denied
246foreach (array_diff(array_keys($groups), $group_granted_ids) as $group_id)
247{
248  $template->assign_block_vars(
249    'group_denied',
250    array(
251      'NAME'=>$groups[$group_id],
252      'ID'=>$group_id
253      )
254    );
255}
256
257// users...
258$users = array();
259
260$query = '
261SELECT '.$conf['user_fields']['id'].' AS id,
262       '.$conf['user_fields']['username'].' AS username
263  FROM '.USERS_TABLE.'
264  WHERE id != '.$conf['guest_id'].'
265;';
266$result = pwg_query($query);
267while($row = mysql_fetch_array($result))
268{
269  $users[$row['id']] = $row['username'];
270}
271
272$query = '
273SELECT user_id
274  FROM '.USER_ACCESS_TABLE.'
275  WHERE cat_id = '.$page['cat'].'
276;';
277$user_granted_direct_ids = array_from_query($query, 'user_id');
278
279foreach ($user_granted_direct_ids as $user_id)
280{
281  $template->assign_block_vars(
282    'user_granted',
283    array(
284      'NAME'=>$users[$user_id],
285      'ID'=>$user_id
286      )
287    );
288}
289
290$user_granted_indirect_ids = array();
291if (count($group_granted_ids) > 0)
292{
293  $granted_groups = array();
294
295  $query = '
296SELECT user_id, group_id
297  FROM '.USER_GROUP_TABLE.'
298  WHERE group_id IN ('.implode(',', $group_granted_ids).')
299';
300  $result = pwg_query($query);
301  while ($row = mysql_fetch_array($result))
302  {
303    if (!isset($granted_groups[$row['group_id']]))
304    {
305      $granted_groups[$row['group_id']] = array();
306    }
307    array_push($granted_groups[$row['group_id']], $row['user_id']);
308  }
309
310  $user_granted_by_group_ids = array();
311
312  foreach ($granted_groups as $group_users)
313  {
314    $user_granted_by_group_ids = array_merge($user_granted_by_group_ids,
315                                             $group_users);
316  }
317  $user_granted_by_group_ids = array_unique($user_granted_by_group_ids);
318 
319 
320  $user_granted_indirect_ids = array_diff($user_granted_by_group_ids,
321                                          $user_granted_direct_ids);
322 
323  foreach ($user_granted_indirect_ids as $user_id)
324  {
325    $group = '';
326   
327    foreach ($granted_groups as $group_id => $group_users)
328    {
329      if (in_array($user_id, $group_users))
330      {
331        $group = $groups[$group_id];
332        break;
333      }
334    }
335   
336    $template->assign_block_vars(
337      'user_granted_indirect',
338      array(
339        'NAME'=>$users[$user_id],
340        'GROUP'=>$group
341        )
342      );
343  }
344}
345
346$user_denied_ids = array_diff(array_keys($users),
347                              $user_granted_indirect_ids,
348                              $user_granted_direct_ids);
349
350foreach ($user_denied_ids as $user_id)
351{
352  $template->assign_block_vars(
353    'user_denied',
354    array(
355      'NAME'=>$users[$user_id],
356      'ID'=>$user_id
357      )
358    );
359}
360
361
362// +-----------------------------------------------------------------------+
363// |                           sending html code                           |
364// +-----------------------------------------------------------------------+
365$template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm');
366?>
Note: See TracBrowser for help on using the repository browser.