Commit 32566aae authored by Dries's avatar Dries

- Patch #228061 by quicksketch: usability fix: allow roles to be weighted.

parent 8745ceab
......@@ -801,57 +801,141 @@ function theme_user_permission_description($variables) {
}
/**
* Menu callback: administer roles.
*
* @param $role
* A user role object, as returned from user_role_load(). This represents the
* role which will be edited. If not set, a new role will be added instead.
* Form to re-order roles or add a new one.
*
* @ingroup forms
* @see user_role_load()
* @see user_admin_role_validate()
* @see user_admin_role_submit()
* @see theme_user_admin_new_role()
* @see theme_user_admin_roles()
*/
function user_admin_role($form, &$form_state, $role = NULL) {
if (!empty($role)) {
// Display the edit role form.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Role name'),
'#default_value' => $role->name,
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
);
$form['rid'] = array(
'#type' => 'value',
'#value' => $role->rid,
);
$form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions')));
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save role'),
);
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete role'),
function user_admin_roles($form, $form_state) {
$roles = user_roles();
$form['roles'] = array(
'#tree' => TRUE,
);
$order = 0;
foreach ($roles as $rid => $name) {
$form['roles'][$rid]['#role'] = (object) array(
'rid' => $rid,
'name' => $name,
'weight' => $order,
);
}
else {
$form['name'] = array(
$form['roles'][$rid]['#weight'] = $order;
$form['roles'][$rid]['weight'] = array(
'#type' => 'textfield',
'#size' => 32,
'#maxlength' => 64,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add role'),
'#size' => 4,
'#default_value' => $order,
'#attributes' => array('class' => array('role-weight')),
);
$form['#submit'][] = 'user_admin_role_submit';
$form['#validate'][] = 'user_admin_role_validate';
$order++;
}
$form['name'] = array(
'#type' => 'textfield',
'#size' => 32,
'#maxlength' => 64,
);
$form['add'] = array(
'#type' => 'submit',
'#value' => t('Add role'),
'#validate' => array('user_admin_role_validate'),
'#submit' => array('user_admin_role_submit'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save order'),
'#submit' => array('user_admin_roles_order_submit'),
);
return $form;
}
/**
* Form submit function. Update the role weights.
*/
function user_admin_roles_order_submit($form, &$form_state) {
foreach ($form_state['values']['roles'] as $rid => $role_values) {
$role = $form['roles'][$rid]['#role'];
$role->weight = $role_values['weight'];
user_role_save($role);
}
}
/**
* Theme the role order and new role form.
*
* @ingroup themeable
*/
function theme_user_admin_roles($variables) {
$form = $variables['form'];
$header = array(t('Name'), t('Weight'), array('data' => t('Operations'), 'colspan' => 2));
foreach (element_children($form['roles']) as $rid) {
$name = $form['roles'][$rid]['#role']->name;
$row = array();
if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
$row[] = t('@name <em>(locked)</em>', array('@name' => $name));
$row[] = drupal_render($form['roles'][$rid]['weight']);
$row[] = '';
$row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
}
else {
$row[] = check_plain($name);
$row[] = drupal_render($form['roles'][$rid]['weight']);
$row[] = l(t('edit role'), 'admin/people/permissions/roles/edit/' . $rid);
$row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
}
$rows[] = array('data' => $row, 'class' => array('draggable'));
}
$rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['add']), 'colspan' => 4, 'class' => 'edit-name'));
drupal_add_tabledrag('user-roles', 'order', 'sibling', 'role-weight');
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'user-roles')));
$output .= drupal_render_children($form);
return $output;
}
/**
* Form to configure a single role.
*
* @ingroup forms
* @see user_admin_role_validate()
* @see user_admin_role_submit()
*/
function user_admin_role($form, $form_state, $role) {
if ($role->rid == DRUPAL_ANONYMOUS_RID || $role->rid == DRUPAL_AUTHENTICATED_RID) {
drupal_goto('admin/people/permissions/roles');
}
// Display the edit role form.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Role name'),
'#default_value' => $role->name,
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 64,
'#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
);
$form['rid'] = array(
'#type' => 'value',
'#value' => $role->rid,
);
$form['weight'] = array(
'#type' => 'value',
'#value' => $role->weight,
);
$form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions')));
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save role'),
);
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete role'),
);
return $form;
}
......@@ -895,32 +979,6 @@ function user_admin_role_submit($form, &$form_state) {
return;
}
/**
* Theme the new-role form.
*
* @ingroup themeable
*/
function theme_user_admin_new_role($variables) {
$form = $variables['form'];
$header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
foreach (user_roles() as $rid => $name) {
$edit_permissions = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
$rows[] = array(t('!name %locked', array('!name' => $name, '%locked' => t('(locked)'))), '', $edit_permissions);
}
else {
$rows[] = array($name, l(t('edit role'), 'admin/people/permissions/roles/edit/' . $rid), $edit_permissions);
}
}
$rows[] = array(array('data' => drupal_render($form['name']) . drupal_render($form['submit']), 'colspan' => 3, 'class' => 'edit-name'));
$output = drupal_render_children($form);
$output .= theme('table', array('header' => $header, 'rows' => $rows));
return $output;
}
/**
* Theme user administration filter selector.
*
......
......@@ -33,10 +33,10 @@
* Override default textfield float to put the "Add role" button next to
* the input textfield.
*/
#user-admin-new-role td.edit-name {
#user-admin-roles td.edit-name {
clear: both;
}
#user-admin-new-role .form-item-name {
#user-admin-roles .form-item-name {
float: left;
margin-right: 1em;
}
......
......@@ -98,11 +98,20 @@ function user_schema() {
'default' => '',
'description' => 'Unique role name.',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The weight of this role in listings and the user interface.',
),
),
'unique keys' => array(
'name' => array('name'),
),
'primary key' => array('rid'),
'indexes' => array(
'name_weight' => array('name', 'weight'),
),
);
$schema['users'] = array(
......@@ -280,10 +289,10 @@ function user_install() {
// Built-in roles.
$rid_anonymous = db_insert('role')
->fields(array('name' => 'anonymous user'))
->fields(array('name' => 'anonymous user', 'weight' => 0))
->execute();
$rid_authenticated = db_insert('role')
->fields(array('name' => 'authenticated user'))
->fields(array('name' => 'authenticated user', 'weight' => 1))
->execute();
// Sanity check to ensure the anonymous and authenticated role IDs are the
......@@ -544,11 +553,6 @@ function user_update_7005(&$sandbox) {
db_change_field('users', 'mail', 'mail', $schema['users']['fields']['mail']);
}
/**
* @} End of "defgroup user-updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/
/**
* Add module data to {role_permission}.
*/
......@@ -574,3 +578,16 @@ function user_update_7006(&$sandbox) {
->execute();
}
}
/**
* Add a weight column to user roles.
*/
function user_update_7007() {
db_add_field('role', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
db_add_index('role', 'name_weight', array('name', 'weight'));
}
/**
* @} End of "defgroup user-updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/
......@@ -42,7 +42,7 @@ function user_help($path, $arg) {
case 'admin/people/permissions':
return '<p>' . t('Permissions let you control what users can do and see on your site. You can define a specific set of permissions for each role. (See the <a href="@role">Roles</a> page to create a role). Two important roles to consider are Authenticated Users and Administrators. Any permissions granted to the Authenticated Users role will be given to any user who can log into your site. You can make any role the Administrator role for the site, meaning this will be granted all new permissions automatically. You can do this on the <a href="@settings">User Settings</a> page. You should be careful to ensure that only trusted users are given this access and level of control of your site.', array('@role' => url('admin/people/permissions/roles'), '@settings' => url('admin/config/people/accounts'))) . '</p>';
case 'admin/people/permissions/roles':
$output = '<p>' . t('Roles allow you to fine tune the security and administration of Drupal. A role defines a group of users that have certain privileges as defined in <a href="@permissions">user permissions</a>. Examples of roles include: anonymous user, authenticated user, moderator, administrator and so on. In this area you will define the <em>role names</em> of the various roles. To delete a role choose "edit".', array('@permissions' => url('admin/people/permissions'))) . '</p>';
$output = '<p>' . t('Roles allow you to fine tune the security and administration of Drupal. A role defines a group of users that have certain privileges as defined on the <a href="@permissions">permissions page</a>. Examples of roles include: anonymous user, authenticated user, moderator, administrator and so on. In this area you will define the names and order of the roles on your site. It is recommended to order your roles from least permissive (anonymous user) to most permissive (administrator). To delete a role choose "edit role".', array('@permissions' => url('admin/people/permissions'))) . '</p>';
$output .= '<p>'. t('By default, Drupal comes with two user roles:') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t("Anonymous user: this role is used for users that don't have a user account or that are not authenticated.") . '</li>';
......@@ -102,7 +102,7 @@ function user_theme() {
'render element' => 'form',
'file' => 'user.admin.inc',
),
'user_admin_new_role' => array(
'user_admin_roles' => array(
'render element' => 'form',
'file' => 'user.admin.inc',
),
......@@ -1504,6 +1504,8 @@ function user_menu() {
'weight' => -10,
'file' => 'user.admin.inc',
);
// Permissions and role forms.
$items['admin/people/permissions'] = array(
'title' => 'Permissions',
'description' => 'Determine access to features by selecting permissions for roles.',
......@@ -1523,7 +1525,7 @@ function user_menu() {
'title' => 'Roles',
'description' => 'List, edit, or add user roles.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_admin_new_role'),
'page arguments' => array('user_admin_roles'),
'access arguments' => array('administer permissions'),
'file' => 'user.admin.inc',
'type' => MENU_LOCAL_TASK,
......@@ -2499,17 +2501,11 @@ function user_mail_tokens(&$replacements, $data, $options) {
* value.
*/
function user_roles($membersonly = FALSE, $permission = NULL) {
// System roles take the first two positions.
$roles = array(
DRUPAL_ANONYMOUS_RID => NULL,
DRUPAL_AUTHENTICATED_RID => NULL,
);
if (!empty($permission)) {
$result = db_query("SELECT r.* FROM {role} r INNER JOIN {role_permission} p ON r.rid = p.rid WHERE p.permission = :permission ORDER BY r.name", array(':permission' => $permission));
$result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {role_permission} p ON r.rid = p.rid WHERE p.permission = :permission ORDER BY r.weight, r.name", array(':permission' => $permission));
}
else {
$result = db_query('SELECT * FROM {role} ORDER BY name');
$result = db_query('SELECT rid, name FROM {role} ORDER BY weight, name');
}
foreach ($result as $role) {
......@@ -2528,8 +2524,7 @@ function user_roles($membersonly = FALSE, $permission = NULL) {
}
}
// Filter to remove unmatched system roles.
return array_filter($roles);
return $roles;
}
/**
......@@ -2589,6 +2584,12 @@ function user_role_save($role) {
// Prevent leading and trailing spaces in role names.
$role->name = trim($role->name);
}
if (!isset($role->weight)) {
// Set a role weight to make this new role last.
$query = db_select('role');
$query->addExpression('MAX(weight)');
$role->weight = $query->execute()->fetchField() + 1;
}
if (!empty($role->rid) && $role->name) {
$status = drupal_write_record('role', $role, 'rid');
module_invoke_all('user_role_update', $role);
......@@ -3090,7 +3091,6 @@ function user_build_filter_query(SelectQuery $query) {
function user_forms() {
$forms['user_admin_access_add_form']['callback'] = 'user_admin_access_form';
$forms['user_admin_access_edit_form']['callback'] = 'user_admin_access_form';
$forms['user_admin_new_role']['callback'] = 'user_admin_role';
return $forms;
}
......
......@@ -408,6 +408,7 @@ function standard_install() {
// Create a default role for site administrators, with all available permissions assigned.
$admin_role = new stdClass();
$admin_role->name = 'administrator';
$admin_role->weight = 2;
user_role_save($admin_role);
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
// Set this as the administrator role.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment