View source
<?php
function filter_perms_form_system_modules_alter(&$form, &$form_state, $form_id) {
if (isset($form['modules'])) {
foreach ($form['modules'] as $group_key => $module_group) {
if (is_array($module_group)) {
$modules = element_children($module_group);
foreach ($modules as $key => $module) {
if (!empty($form['modules'][$group_key][$module]['links']['permissions']['#options']['fragment'])) {
$form['modules'][$group_key][$module]['links']['permissions']['#href'] .= '/' . $form['modules'][$group_key][$module]['links']['permissions']['#options']['fragment'];
unset($form['modules'][$group_key][$module]['links']['permissions']['#options']['fragment']);
}
}
}
}
}
}
function filter_perms_form_user_admin_permissions_alter(&$form, $form_state, $form_id) {
drupal_add_css(drupal_get_path('module', 'filter_perms') . '/filter_perms.css');
if (isset($_SESSION['filter_perm_roles']) && count($_SESSION['filter_perm_roles'])) {
$roles = $_SESSION['filter_perm_roles'];
if (isset($roles['-1'])) {
$user_roles = user_roles();
foreach ($user_roles as $user_role_key => $user_role) {
$roles[$user_role_key] = $user_role_key;
}
}
}
else {
$roles = array();
}
$cp_field_perms = array();
if (isset($_SESSION['filter_perm_modules']) && count($_SESSION['filter_perm_modules'])) {
$modules = $_SESSION['filter_perm_modules'];
if (isset($modules['-1'])) {
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
if (module_invoke($module, 'permission')) {
$modules[$module] = $module;
}
}
}
}
else {
$modules = array();
}
$perms = array();
foreach ($form['permission'] as $perm_key => $perm) {
if (is_numeric($perm_key)) {
$perm_module = $perm['#id'];
$perm_id = $perm_key;
}
else {
if (in_array($perm_module, $modules)) {
$perms[$perm_module][$perm_key] = $perm_id;
}
}
}
$old_perms = $form['permission'];
$old_checkboxes = $form['checkboxes'];
$old_roles = $form['role_names'];
$form['permission'] = array();
$form['checkboxes'] = array();
$form['role_names'] = array();
if (count($roles) && count($modules)) {
$new_roles = array();
foreach ($old_roles as $rid => $role) {
if (in_array($rid, $roles)) {
$new_roles[$rid] = $role;
}
}
$perm_index = 0;
foreach ($perms as $perm_key => $perm) {
foreach ($perms[$perm_key] as $perm_name => $perm_value) {
$form['permission'][$perm_index] = $old_perms[$perm_value];
$form['permission'][$perm_name] = $old_perms[$perm_name];
foreach ($new_roles as $rid => $role) {
$form['checkboxes'][$rid]['#type'] = 'checkboxes';
$form['checkboxes'][$rid]['#options'][$perm_name] = '';
$form['checkboxes'][$rid]['#default_value'] = $old_checkboxes[$rid]['#default_value'];
}
}
$perm_index++;
}
$form['role_names'] = $new_roles;
foreach ($new_roles as $new_rid => $new_role) {
$form['role_names']['#value'][$new_rid] = $new_role['#markup'];
}
$form['role_names']['#type'] = 'value';
}
else {
$form['permission']['-1'] = array(
'#markup' => 'Filter Selection',
'#id' => 'empty',
);
$form['permission']['empty'] = array(
'#type' => 'item',
'#markup' => 'Please select at least one value from both the Roles and Modules select boxes above and then click the "Filter Permissions" button',
);
$form['role_names']['#value'] = array();
unset($form['actions']['submit']);
}
}
function filter_perms_menu_alter(&$callbacks) {
$callbacks['admin/people/permissions']['page callback'] = 'filter_perms_admin_perm';
}
function filter_perms_admin_perm() {
$output = array();
$output['filter_perms_admin_perm_filter'] = drupal_get_form('filter_perms_admin_perm_filter');
$output['user_admin_permissions'] = drupal_get_form('user_admin_permissions');
return $output;
}
function filter_perms_admin_perm_filter() {
$url_parts = explode('/', request_uri());
$url_last = $url_parts[count($url_parts) - 1];
if (is_numeric($url_last)) {
$_SESSION['filter_perm_roles'] = array(
$url_last => $url_last,
);
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
if (module_invoke($module, 'permission')) {
$_SESSION['filter_perm_modules'][$module] = $module;
}
}
}
elseif (strstr($url_last, 'module-')) {
$single_module = str_replace('module-', '', $url_last);
$_SESSION['filter_perm_modules'] = array(
$single_module => $single_module,
);
$user_roles = user_roles();
foreach ($user_roles as $user_role_key => $user_role) {
$_SESSION['filter_perm_roles'][$user_role_key] = $user_role_key;
}
}
if ($url_last != 'permissions' && strpos($url_last, 'permissions?') === FALSE) {
drupal_goto('admin/people/permissions');
}
$form['filters'] = array(
'#type' => 'fieldset',
'#title' => t('Permission Filters'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$role_options = user_roles();
$form['filters']['roles'] = array(
'#title' => t('Roles to display'),
'#type' => 'select',
'#options' => array(
'-1' => '--All Roles',
) + $role_options,
'#default_value' => isset($_SESSION['filter_perm_roles']) ? $_SESSION['filter_perm_roles'] : '',
'#size' => 8,
'#multiple' => TRUE,
);
$module_options = array();
foreach (module_list(FALSE, FALSE, TRUE) as $module) {
if (module_invoke($module, 'permission')) {
$module_options[$module] = $module;
}
}
$form['filters']['modules'] = array(
'#title' => t('Modules to display'),
'#type' => 'select',
'#options' => array(
'-1' => '--All Modules',
) + $module_options,
'#default_value' => isset($_SESSION['filter_perm_modules']) ? $_SESSION['filter_perm_modules'] : '',
'#size' => 8,
'#multiple' => TRUE,
);
$form['filters']['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter Permissions'),
);
return $form;
}
function filter_perms_admin_perm_filter_submit($form, &$form_state) {
$_SESSION['filter_perm_roles'] = $form_state['values']['roles'];
$_SESSION['filter_perm_modules'] = $form_state['values']['modules'];
}