6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
22 class acp_permission_roles
26 function main($id, $mode)
28 global $db, $user, $auth, $template, $cache;
29 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
31 include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
32 include_once($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
34 $auth_admin = new auth_admin();
36 $user->add_lang('acp/permissions');
37 add_permission_language();
39 $this->tpl_name = 'acp_permission_roles';
41 $submit = (isset($_POST['submit'])) ? true : false;
42 $role_id = request_var('role_id', 0);
43 $action = request_var('action', '');
44 $action = (isset($_POST['add'])) ? 'add' : $action;
46 $form_name = 'acp_permissions';
47 add_form_key($form_name);
52 $permission_type = 'a_';
53 $this->page_title = 'ACP_ADMIN_ROLES';
57 $permission_type = 'u_';
58 $this->page_title = 'ACP_USER_ROLES';
62 $permission_type = 'm_';
63 $this->page_title = 'ACP_MOD_ROLES';
67 $permission_type = 'f_';
68 $this->page_title = 'ACP_FORUM_ROLES';
72 trigger_error('NO_MODE', E_USER_ERROR);
76 $template->assign_vars(array(
77 'L_TITLE' => $user->lang[$this->page_title],
78 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'])
81 // Take action... admin submitted something
82 if ($submit || $action == 'remove')
90 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
94 FROM ' . ACL_ROLES_TABLE . '
95 WHERE role_id = ' . $role_id;
96 $result = $db->sql_query($sql);
97 $role_row = $db->sql_fetchrow($result);
98 $db->sql_freeresult($result);
102 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
105 if (confirm_box(true))
107 $this->remove_role($role_id, $permission_type);
109 $role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
110 add_log('admin', 'LOG_' . strtoupper($permission_type) . 'ROLE_REMOVED', $role_name);
111 trigger_error($user->lang['ROLE_DELETED'] . adm_back_link($this->u_action));
115 confirm_box(false, 'DELETE_ROLE', build_hidden_fields(array(
118 'role_id' => $role_id,
128 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
133 FROM ' . ACL_ROLES_TABLE . '
134 WHERE role_id = ' . $role_id;
135 $result = $db->sql_query($sql);
136 $role_row = $db->sql_fetchrow($result);
137 $db->sql_freeresult($result);
141 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
148 if (!check_form_key($form_name))
150 trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
153 $role_name = utf8_normalize_nfc(request_var('role_name', '', true));
154 $role_description = utf8_normalize_nfc(request_var('role_description', '', true));
155 $auth_settings = request_var('setting', array('' => 0));
159 trigger_error($user->lang['NO_ROLE_NAME_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
162 if (utf8_strlen($role_description) > 4000)
164 trigger_error($user->lang['ROLE_DESCRIPTION_LONG'] . adm_back_link($this->u_action), E_USER_WARNING);
167 // if we add/edit a role we check the name to be unique among the settings...
168 $sql = 'SELECT role_id
169 FROM ' . ACL_ROLES_TABLE . "
170 WHERE role_type = '" . $db->sql_escape($permission_type) . "'
171 AND role_name = '" . $db->sql_escape($role_name) . "'";
172 $result = $db->sql_query($sql);
173 $row = $db->sql_fetchrow($result);
174 $db->sql_freeresult($result);
176 // Make sure we only print out the error if we add the role or change it's name
177 if ($row && ($mode == 'add' || ($mode == 'edit' && $role_row['role_name'] != $role_name)))
179 trigger_error(sprintf($user->lang['ROLE_NAME_ALREADY_EXIST'], $role_name) . adm_back_link($this->u_action), E_USER_WARNING);
183 'role_name' => (string) $role_name,
184 'role_description' => (string) $role_description,
185 'role_type' => (string) $permission_type,
188 if ($action == 'edit')
190 $sql = 'UPDATE ' . ACL_ROLES_TABLE . '
191 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
192 WHERE role_id = ' . $role_id;
193 $db->sql_query($sql);
197 // Get maximum role order for inserting a new role...
198 $sql = 'SELECT MAX(role_order) as max_order
199 FROM ' . ACL_ROLES_TABLE . "
200 WHERE role_type = '" . $db->sql_escape($permission_type) . "'";
201 $result = $db->sql_query($sql);
202 $max_order = (int) $db->sql_fetchfield('max_order');
203 $db->sql_freeresult($result);
205 $sql_ary['role_order'] = $max_order + 1;
207 $sql = 'INSERT INTO ' . ACL_ROLES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
208 $db->sql_query($sql);
210 $role_id = $db->sql_nextid();
213 // Now add the auth settings
214 $auth_admin->acl_set_role($role_id, $auth_settings);
216 $role_name = (!empty($user->lang[$role_name])) ? $user->lang[$role_name] : $role_name;
217 add_log('admin', 'LOG_' . strtoupper($permission_type) . 'ROLE_' . strtoupper($action), $role_name);
219 trigger_error($user->lang['ROLE_' . strtoupper($action) . '_SUCCESS'] . adm_back_link($this->u_action));
230 $options_from = request_var('options_from', 0);
233 'role_name' => utf8_normalize_nfc(request_var('role_name', '', true)),
234 'role_description' => utf8_normalize_nfc(request_var('role_description', '', true)),
235 'role_type' => $permission_type,
240 $sql = 'SELECT p.auth_option_id, p.auth_setting, o.auth_option
241 FROM ' . ACL_ROLES_DATA_TABLE . ' p, ' . ACL_OPTIONS_TABLE . ' o
242 WHERE o.auth_option_id = p.auth_option_id
243 AND p.role_id = ' . $options_from . '
244 ORDER BY p.auth_option_id';
245 $result = $db->sql_query($sql);
247 $auth_options = array();
248 while ($row = $db->sql_fetchrow($result))
250 $auth_options[$row['auth_option']] = $row['auth_setting'];
252 $db->sql_freeresult($result);
256 $sql = 'SELECT auth_option_id, auth_option
257 FROM ' . ACL_OPTIONS_TABLE . "
258 WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char) . "
259 AND auth_option <> '{$permission_type}'
260 ORDER BY auth_option_id";
261 $result = $db->sql_query($sql);
263 $auth_options = array();
264 while ($row = $db->sql_fetchrow($result))
266 $auth_options[$row['auth_option']] = ACL_NO;
268 $db->sql_freeresult($result);
275 if ($action == 'edit')
279 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
283 FROM ' . ACL_ROLES_TABLE . '
284 WHERE role_id = ' . $role_id;
285 $result = $db->sql_query($sql);
286 $role_row = $db->sql_fetchrow($result);
287 $db->sql_freeresult($result);
289 $sql = 'SELECT p.auth_option_id, p.auth_setting, o.auth_option
290 FROM ' . ACL_ROLES_DATA_TABLE . ' p, ' . ACL_OPTIONS_TABLE . ' o
291 WHERE o.auth_option_id = p.auth_option_id
292 AND p.role_id = ' . $role_id . '
293 ORDER BY p.auth_option_id';
294 $result = $db->sql_query($sql);
296 $auth_options = array();
297 while ($row = $db->sql_fetchrow($result))
299 $auth_options[$row['auth_option']] = $row['auth_setting'];
301 $db->sql_freeresult($result);
306 trigger_error($user->lang['NO_ROLE_SELECTED'] . adm_back_link($this->u_action), E_USER_WARNING);
309 $template->assign_vars(array(
312 'U_ACTION' => $this->u_action . "&action={$action}&role_id={$role_id}",
313 'U_BACK' => $this->u_action,
315 'ROLE_NAME' => $role_row['role_name'],
316 'ROLE_DESCRIPTION' => $role_row['role_description'],
317 'L_ACL_TYPE' => $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
321 // We need to fill the auth options array with ACL_NO options ;)
322 $sql = 'SELECT auth_option_id, auth_option
323 FROM ' . ACL_OPTIONS_TABLE . "
324 WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char) . "
325 AND auth_option <> '{$permission_type}'
326 ORDER BY auth_option_id";
327 $result = $db->sql_query($sql);
329 while ($row = $db->sql_fetchrow($result))
331 if (!isset($auth_options[$row['auth_option']]))
333 $auth_options[$row['auth_option']] = ACL_NO;
336 $db->sql_freeresult($result);
338 // Unset global permission option
339 unset($auth_options[$permission_type]);
341 // Display auth options
342 $this->display_auth_options($auth_options);
344 // Get users/groups/forums using this preset...
345 if ($action == 'edit')
347 $hold_ary = $auth_admin->get_role_mask($role_id);
349 if (sizeof($hold_ary))
351 $role_name = (!empty($user->lang[$role_row['role_name']])) ? $user->lang[$role_row['role_name']] : $role_row['role_name'];
353 $template->assign_vars(array(
354 'S_DISPLAY_ROLE_MASK' => true,
355 'L_ROLE_ASSIGNED_TO' => sprintf($user->lang['ROLE_ASSIGNED_TO'], $role_name))
358 $auth_admin->display_role_mask($hold_ary);
368 $order = request_var('order', 0);
369 $order_total = $order * 2 + (($action == 'move_up') ? -1 : 1);
371 $sql = 'UPDATE ' . ACL_ROLES_TABLE . '
372 SET role_order = ' . $order_total . " - role_order
373 WHERE role_type = '" . $db->sql_escape($permission_type) . "'
374 AND role_order IN ($order, " . (($action == 'move_up') ? $order - 1 : $order + 1) . ')';
375 $db->sql_query($sql);
380 // By default, check that role_order is valid and fix it if necessary
381 $sql = 'SELECT role_id, role_order
382 FROM ' . ACL_ROLES_TABLE . "
383 WHERE role_type = '" . $db->sql_escape($permission_type) . "'
384 ORDER BY role_order ASC";
385 $result = $db->sql_query($sql);
387 if ($row = $db->sql_fetchrow($result))
393 if ($row['role_order'] != $order)
395 $db->sql_query('UPDATE ' . ACL_ROLES_TABLE . " SET role_order = $order WHERE role_id = {$row['role_id']}");
398 while ($row = $db->sql_fetchrow($result));
400 $db->sql_freeresult($result);
402 // Display assigned items?
403 $display_item = request_var('display_item', 0);
405 // Select existing roles
407 FROM ' . ACL_ROLES_TABLE . "
408 WHERE role_type = '" . $db->sql_escape($permission_type) . "'
409 ORDER BY role_order ASC";
410 $result = $db->sql_query($sql);
412 $s_role_options = '';
413 while ($row = $db->sql_fetchrow($result))
415 $role_name = (!empty($user->lang[$row['role_name']])) ? $user->lang[$row['role_name']] : $row['role_name'];
417 $template->assign_block_vars('roles', array(
418 'ROLE_NAME' => $role_name,
419 'ROLE_DESCRIPTION' => (!empty($user->lang[$row['role_description']])) ? $user->lang[$row['role_description']] : nl2br($row['role_description']),
421 'U_EDIT' => $this->u_action . '&action=edit&role_id=' . $row['role_id'],
422 'U_REMOVE' => $this->u_action . '&action=remove&role_id=' . $row['role_id'],
423 'U_MOVE_UP' => $this->u_action . '&action=move_up&order=' . $row['role_order'],
424 'U_MOVE_DOWN' => $this->u_action . '&action=move_down&order=' . $row['role_order'],
425 'U_DISPLAY_ITEMS' => ($row['role_id'] == $display_item) ? '' : $this->u_action . '&display_item=' . $row['role_id'] . '#assigned_to')
428 $s_role_options .= '<option value="' . $row['role_id'] . '">' . $role_name . '</option>';
430 if ($display_item == $row['role_id'])
432 $template->assign_vars(array(
433 'L_ROLE_ASSIGNED_TO' => sprintf($user->lang['ROLE_ASSIGNED_TO'], $role_name))
437 $db->sql_freeresult($result);
439 $template->assign_vars(array(
440 'S_ROLE_OPTIONS' => $s_role_options)
445 $template->assign_vars(array(
446 'S_DISPLAY_ROLE_MASK' => true)
449 $hold_ary = $auth_admin->get_role_mask($display_item);
450 $auth_admin->display_role_mask($hold_ary);
455 * Display permission settings able to be set
457 function display_auth_options($auth_options)
459 global $template, $user;
461 $content_array = $categories = array();
462 $key_sort_array = array(0);
463 $auth_options = array(0 => $auth_options);
465 // Making use of auth_admin method here (we do not really want to change two similar code fragments)
466 auth_admin::build_permission_array($auth_options, $content_array, $categories, $key_sort_array);
468 $content_array = $content_array[0];
470 $template->assign_var('S_NUM_PERM_COLS', sizeof($categories));
472 // Assign to template
473 foreach ($content_array as $cat => $cat_array)
475 $template->assign_block_vars('auth', array(
476 'CAT_NAME' => $user->lang['permission_cat'][$cat],
478 'S_YES' => ($cat_array['S_YES'] && !$cat_array['S_NEVER'] && !$cat_array['S_NO']) ? true : false,
479 'S_NEVER' => ($cat_array['S_NEVER'] && !$cat_array['S_YES'] && !$cat_array['S_NO']) ? true : false,
480 'S_NO' => ($cat_array['S_NO'] && !$cat_array['S_NEVER'] && !$cat_array['S_YES']) ? true : false)
483 foreach ($cat_array['permissions'] as $permission => $allowed)
485 $template->assign_block_vars('auth.mask', array(
486 'S_YES' => ($allowed == ACL_YES) ? true : false,
487 'S_NEVER' => ($allowed == ACL_NEVER) ? true : false,
488 'S_NO' => ($allowed == ACL_NO) ? true : false,
490 'FIELD_NAME' => $permission,
491 'PERMISSION' => $user->lang['acl_' . $permission]['lang'])
500 function remove_role($role_id, $permission_type)
504 $auth_admin = new auth_admin();
506 // Get complete auth array
507 $sql = 'SELECT auth_option, auth_option_id
508 FROM ' . ACL_OPTIONS_TABLE . "
509 WHERE auth_option " . $db->sql_like_expression($permission_type . $db->any_char);
510 $result = $db->sql_query($sql);
512 $auth_settings = array();
513 while ($row = $db->sql_fetchrow($result))
515 $auth_settings[$row['auth_option']] = ACL_NO;
517 $db->sql_freeresult($result);
519 // Get the role auth settings we need to re-set...
520 $sql = 'SELECT o.auth_option, r.auth_setting
521 FROM ' . ACL_ROLES_DATA_TABLE . ' r, ' . ACL_OPTIONS_TABLE . ' o
522 WHERE o.auth_option_id = r.auth_option_id
523 AND r.role_id = ' . $role_id;
524 $result = $db->sql_query($sql);
526 while ($row = $db->sql_fetchrow($result))
528 $auth_settings[$row['auth_option']] = $row['auth_setting'];
530 $db->sql_freeresult($result);
532 // Get role assignments
533 $hold_ary = $auth_admin->get_role_mask($role_id);
535 // Re-assign permissions
536 foreach ($hold_ary as $forum_id => $forum_ary)
538 if (isset($forum_ary['users']))
540 $auth_admin->acl_set('user', $forum_id, $forum_ary['users'], $auth_settings, 0, false);
543 if (isset($forum_ary['groups']))
545 $auth_admin->acl_set('group', $forum_id, $forum_ary['groups'], $auth_settings, 0, false);
549 // Remove role from users and groups just to be sure (happens through acl_set)
550 $sql = 'DELETE FROM ' . ACL_USERS_TABLE . '
551 WHERE auth_role_id = ' . $role_id;
552 $db->sql_query($sql);
554 $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . '
555 WHERE auth_role_id = ' . $role_id;
556 $db->sql_query($sql);
558 // Remove role data and role
559 $sql = 'DELETE FROM ' . ACL_ROLES_DATA_TABLE . '
560 WHERE role_id = ' . $role_id;
561 $db->sql_query($sql);
563 $sql = 'DELETE FROM ' . ACL_ROLES_TABLE . '
564 WHERE role_id = ' . $role_id;
565 $db->sql_query($sql);
567 $auth_admin->acl_clear_prefetch();