source: trunk/admin/cat_perm.php @ 12019

Last change on this file since 12019 was 12019, checked in by flop25, 13 years ago

feature:2418
adding an option to apply permission of users to sub-albums
changing the name of vars $private_uppercats->$private_cats

  • Property svn:eol-style set to LF
File size: 9.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die ("Hacking attempt!");
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36// +-----------------------------------------------------------------------+
37// |                       variable initialization                         |
38// +-----------------------------------------------------------------------+
39
40// if the category is not correct (not numeric, not private)
41if (isset($_GET['cat']) and is_numeric($_GET['cat']))
42{
43  $query = '
44SELECT status
45  FROM '.CATEGORIES_TABLE.'
46  WHERE id = '.$_GET['cat'].'
47;';
48  list($status) = pwg_db_fetch_row(pwg_query($query));
49 
50  if ('private' == $status)
51  {
52    $page['cat'] = $_GET['cat'];
53  }
54}
55
56if (!isset($page['cat']))
57{
58  $query = '
59SELECT id
60  FROM '.CATEGORIES_TABLE.'
61  WHERE status = \'private\'
62  LIMIT 1
63;';
64
65  list($page['cat']) = pwg_db_fetch_row(pwg_query($query));
66}
67
68// +-----------------------------------------------------------------------+
69// |                           form submission                             |
70// +-----------------------------------------------------------------------+
71
72
73if (isset($_POST['deny_groups_submit'])
74         and isset($_POST['deny_groups'])
75         and count($_POST['deny_groups']) > 0)
76{
77  // if you forbid access to a category, all sub-categories become
78  // automatically forbidden
79  $query = '
80DELETE
81  FROM '.GROUP_ACCESS_TABLE.'
82  WHERE group_id IN ('.implode(',', $_POST['deny_groups']).')
83    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
84;';
85  pwg_query($query);
86}
87else if (isset($_POST['grant_groups_submit'])
88         and isset($_POST['grant_groups'])
89         and count($_POST['grant_groups']) > 0)
90{
91  $cat_ids = (isset($_POST['apply_on_sub'])) ? implode(',', get_subcat_ids(array($page['cat']))).",".implode(',', get_uppercat_ids(array($page['cat']))) : implode(',', get_uppercat_ids(array($page['cat'])));
92
93  $query = '
94SELECT id
95  FROM '.CATEGORIES_TABLE.'
96  WHERE id IN ('.$cat_ids.')
97  AND status = \'private\'
98;';
99  $private_cats = array_from_query($query, 'id');
100
101  // We must not reinsert already existing lines in group_access table
102  $granteds = array();
103  foreach ($private_cats as $cat_id)
104  {
105    $granteds[$cat_id] = array();
106  }
107 
108  $query = '
109SELECT group_id, cat_id
110  FROM '.GROUP_ACCESS_TABLE.'
111  WHERE cat_id IN ('.implode(',', $private_cats).')
112    AND group_id IN ('.implode(',', $_POST['grant_groups']).')
113;';
114  $result = pwg_query($query);
115  while ($row = pwg_db_fetch_assoc($result))
116  {
117    array_push($granteds[$row['cat_id']], $row['group_id']);
118  }
119
120  $inserts = array();
121 
122  foreach ($private_cats as $cat_id)
123  {
124    $group_ids = array_diff($_POST['grant_groups'], $granteds[$cat_id]);
125    foreach ($group_ids as $group_id)
126    {
127      array_push($inserts, array('group_id' => $group_id,
128                                 'cat_id' => $cat_id));
129    }
130  }
131
132  mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
133}
134else if (isset($_POST['deny_users_submit'])
135         and isset($_POST['deny_users'])
136         and count($_POST['deny_users']) > 0)
137{
138  // if you forbid access to a category, all sub-categories become
139  // automatically forbidden
140  $query = '
141DELETE
142  FROM '.USER_ACCESS_TABLE.'
143  WHERE user_id IN ('.implode(',', $_POST['deny_users']).')
144    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
145;';
146  pwg_query($query);
147}
148else if (isset($_POST['grant_users_submit']))
149{
150  add_permission_on_category($page['cat'], $_POST['grant_users']);
151}
152
153// +-----------------------------------------------------------------------+
154// |                       template initialization                         |
155// +-----------------------------------------------------------------------+
156
157$template->set_filename('cat_perm', 'cat_perm.tpl');
158
159$template->assign(
160  array(
161    'CATEGORIES_NAV' =>
162      get_cat_display_name_from_id(
163        $page['cat'],
164        'admin.php?page=cat_modify&amp;cat_id='
165        ),
166    'U_HELP' => get_root_url().'admin/popuphelp.php?page=cat_perm',
167    'F_ACTION' => get_root_url().'admin.php?page=cat_perm&amp;cat='.$page['cat']
168    )
169  );
170
171// +-----------------------------------------------------------------------+
172// |                          form construction                            |
173// +-----------------------------------------------------------------------+
174
175// groups denied are the groups not granted. So we need to find all groups
176// minus groups granted to find groups denied.
177
178$groups = array();
179
180$query = '
181SELECT id, name
182  FROM '.GROUPS_TABLE.'
183  ORDER BY name ASC
184;';
185$groups = simple_hash_from_query($query, 'id', 'name');
186$template->assign('all_groups', $groups);
187
188// groups granted to access the category
189$query = '
190SELECT group_id
191  FROM '.GROUP_ACCESS_TABLE.'
192  WHERE cat_id = '.$page['cat'].'
193;';
194$group_granted_ids = array_from_query($query, 'group_id');
195$group_granted_ids = order_by_name($group_granted_ids, $groups);
196$template->assign('group_granted_ids', $group_granted_ids);
197
198
199// groups denied
200$template->assign('group_denied_ids',
201    order_by_name(array_diff(array_keys($groups), $group_granted_ids), $groups)
202  );
203
204// users...
205$users = array();
206
207$query = '
208SELECT '.$conf['user_fields']['id'].' AS id,
209       '.$conf['user_fields']['username'].' AS username
210  FROM '.USERS_TABLE.'
211;';
212$users = simple_hash_from_query($query, 'id', 'username');
213$template->assign('all_users', $users);
214
215
216$query = '
217SELECT user_id
218  FROM '.USER_ACCESS_TABLE.'
219  WHERE cat_id = '.$page['cat'].'
220;';
221$user_granted_direct_ids = array_from_query($query, 'user_id');
222$user_granted_direct_ids = order_by_name($user_granted_direct_ids, $users);
223$template->assign('user_granted_direct_ids', $user_granted_direct_ids);
224
225
226
227$user_granted_indirect_ids = array();
228if (count($group_granted_ids) > 0)
229{
230  $granted_groups = array();
231
232  $query = '
233SELECT user_id, group_id
234  FROM '.USER_GROUP_TABLE.'
235  WHERE group_id IN ('.implode(',', $group_granted_ids).')
236';
237  $result = pwg_query($query);
238  while ($row = pwg_db_fetch_assoc($result))
239  {
240    if (!isset($granted_groups[$row['group_id']]))
241    {
242      $granted_groups[$row['group_id']] = array();
243    }
244    array_push($granted_groups[$row['group_id']], $row['user_id']);
245  }
246
247  $user_granted_by_group_ids = array();
248
249  foreach ($granted_groups as $group_users)
250  {
251    $user_granted_by_group_ids = array_merge($user_granted_by_group_ids,
252                                             $group_users);
253  }
254  $user_granted_by_group_ids = array_unique($user_granted_by_group_ids);
255 
256 
257  $user_granted_indirect_ids = array_diff($user_granted_by_group_ids,
258                                          $user_granted_direct_ids);
259  $user_granted_indirect_ids = 
260    order_by_name($user_granted_indirect_ids, $users); 
261  foreach ($user_granted_indirect_ids as $user_id)
262  {
263    foreach ($granted_groups as $group_id => $group_users)
264    {
265      if (in_array($user_id, $group_users))
266      {
267        $template->append(
268          'user_granted_indirects',
269          array(
270            'USER'=>$users[$user_id],
271            'GROUP'=>$groups[$group_id]
272            )
273          );
274        break;
275      }
276    }
277  }
278}
279
280$user_denied_ids = array_diff(array_keys($users),
281                              $user_granted_indirect_ids,
282                              $user_granted_direct_ids);
283$user_denied_ids = order_by_name($user_denied_ids, $users);
284$template->assign('user_denied_ids', $user_denied_ids);
285
286
287// +-----------------------------------------------------------------------+
288// |                           sending html code                           |
289// +-----------------------------------------------------------------------+
290$template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm');
291?>
Note: See TracBrowser for help on using the repository browser.