View source
<?php
function user_permissions_forms() {
$forms['user_permissions_user_admin_new_role'] = array(
'callback' => 'user_admin_role',
);
return $forms;
}
function user_permissions_menu($may_cache) {
$items = array();
if (!$may_cache && user_access('administer permissions')) {
if (user_access('administer permissions')) {
$arg = arg(1);
$user = user_load(array(
'uid' => $arg,
));
if ($user != FALSE) {
$items[] = array(
'path' => 'user/' . $arg . '/permissions',
'title' => t('User Permissions'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'user_permissions_profile_permissions_form',
$arg,
),
'access' => user_access('administer permissions'),
'type' => MENU_LOCAL_TASK,
);
}
}
$items[] = array(
'path' => 'admin/user/roles',
'title' => t('Roles'),
'description' => t('List, edit, or add user roles.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'user_permissions_user_admin_new_role',
),
'access' => user_access('administer access control'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
function user_permissions_profile_permissions_form($user_id) {
$role_name = '_user_role_' . $user_id;
$rid = db_result(db_query('SELECT rid FROM {role} WHERE name = "%s"', $role_name));
if ($rid) {
$form = user_admin_perm($rid);
}
else {
$rid = DRUPAL_ANONYMOUS_RID;
$form = user_admin_perm($rid);
$form['checkboxes'][$rid]['#default_value'] = array();
$form['role_names'][$rid]['#value'] = $role_name;
}
$form['role_name'] = array(
'#type' => 'hidden',
'#value' => $role_name,
);
$form['rid'] = array(
'#type' => 'hidden',
'#value' => $rid,
);
return $form;
}
function user_permissions_profile_permissions_form_submit($form_id, $form_values) {
$rid = $form_values["rid"];
$uid = (int) substr_replace($form_values["role_name"], "", 0, 11);
$perms = array_filter($form_values[$rid]);
if ($rid == DRUPAL_ANONYMOUS_RID) {
if (!empty($perms)) {
$role_name = $form_values["role_name"];
db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
$rid = db_result(db_query("SELECT LAST_INSERT_ID()"));
db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $rid);
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
}
}
else {
db_query("DELETE FROM {permission} WHERE rid = %d", $rid);
if (!empty($perms)) {
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
}
else {
db_query("DELETE FROM {users_roles} WHERE uid = %d and rid = %d", $uid, $rid);
db_query("DELETE FROM {role} WHERE rid = %d", $rid);
}
}
drupal_set_message(t('The changes have been saved.'));
menu_rebuild();
}
function theme_user_permissions_profile_permissions_form($form) {
foreach (element_children($form['permission']) as $key) {
if (is_array($form['permission'][$key])) {
$row = array();
if (is_numeric($key)) {
$row[] = array(
'data' => t('@module module', array(
'@module' => drupal_render($form['permission'][$key]),
)),
'class' => 'module',
'id' => 'module-' . $form['permission'][$key]['#value'],
'colspan' => count($form['role_names']) + 1,
);
}
else {
$row[] = array(
'data' => drupal_render($form['permission'][$key]),
'class' => 'permission',
);
foreach (element_children($form['checkboxes']) as $rid) {
if (is_array($form['checkboxes'][$rid])) {
$row[] = array(
'data' => drupal_render($form['checkboxes'][$rid][$key]),
'align' => 'center',
'title' => t($key),
);
}
}
}
$rows[] = $row;
}
}
$header[] = t('Permission');
foreach (element_children($form['role_names']) as $rid) {
if (is_array($form['role_names'][$rid])) {
$header[] = drupal_render($form['role_names'][$rid]);
}
}
$output = theme('table', $header, $rows, array(
'id' => 'permissions',
));
$output .= drupal_render($form);
return $output;
}
function user_permissions_form_alter($form_id, &$form) {
if ($form_id == 'user_filter_form') {
if (is_array($form['filters']['status']['role']['#options'])) {
foreach ($form['filters']['status']['role']['#options'] as $key => $value) {
if (strpos($value, "_user_role_") !== FALSE) {
unset($form['filters']['status']['role']['#options'][$key]);
}
}
}
}
if ($form_id == 'user_admin_account') {
if (is_array($form['options']['operation']['#options']['Add a role to the selected users']) && is_array($form['options']['operation']['#options']['Remove a role from the selected users'])) {
foreach ($form['options']['operation']['#options']['Add a role to the selected users'] as $key => $value) {
if (strpos($value, "_user_role_") !== FALSE) {
unset($form['options']['operation']['#options']['Add a role to the selected users'][$key]);
}
}
foreach ($form['options']['operation']['#options']['Remove a role from the selected users'] as $key => $value) {
if (strpos($value, "_user_role_") !== FALSE) {
unset($form['options']['operation']['#options']['Remove a role from the selected users'][$key]);
}
}
}
}
if ($form_id == 'user_admin_perm') {
$role_id_filter = array();
foreach (user_roles() as $key => $value) {
if (strpos($value, "_user_role_") !== FALSE) {
$role_id_filter[] = $key;
}
}
foreach ($form['checkboxes'] as $key => $value) {
if (in_array($key, $role_id_filter)) {
unset($form['checkboxes'][$key]);
}
}
foreach ($form['role_names'] as $key => $value) {
if (in_array($key, $role_id_filter)) {
unset($form['role_names'][$key]);
}
}
}
}
function theme_user_permissions_user_admin_new_role($form) {
$header = array(
t('Name'),
array(
'data' => t('Operations'),
'colspan' => 2,
),
);
foreach (user_roles() as $rid => $name) {
$edit_permissions = l(t('edit permissions'), 'admin/user/access/' . $rid);
if (!in_array($rid, array(
DRUPAL_ANONYMOUS_RID,
DRUPAL_AUTHENTICATED_RID,
))) {
if (strpos($name, '_user_role_') === FALSE) {
$rows[] = array(
$name,
l(t('edit role'), 'admin/user/roles/edit/' . $rid),
$edit_permissions,
);
}
}
else {
$rows[] = array(
$name,
t('locked'),
$edit_permissions,
);
}
}
$rows[] = array(
drupal_render($form['name']),
array(
'data' => drupal_render($form['submit']),
'colspan' => 2,
),
);
$output = drupal_render($form);
$output .= theme('table', $header, $rows);
return $output;
}