From 32566aaee47a42724abae9ffda9f20e4d479c835 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 24 Mar 2010 07:34:10 +0000 Subject: - Patch #228061 by quicksketch: usability fix: allow roles to be weighted. --- modules/user/user.admin.inc | 198 ++++++++++++++++++++++++------------- modules/user/user.css | 4 +- modules/user/user.install | 31 ++++-- modules/user/user.module | 28 +++--- profiles/standard/standard.install | 1 + 5 files changed, 169 insertions(+), 93 deletions(-) diff --git a/modules/user/user.admin.inc b/modules/user/user.admin.inc index fcf626c7e..2d6e7df60 100644 --- a/modules/user/user.admin.inc +++ b/modules/user/user.admin.inc @@ -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 (locked)', 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. * diff --git a/modules/user/user.css b/modules/user/user.css index 51222f0c1..414fbc9ed 100644 --- a/modules/user/user.css +++ b/modules/user/user.css @@ -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; } diff --git a/modules/user/user.install b/modules/user/user.install index d9d017661..a0dc12dc5 100644 --- a/modules/user/user.install +++ b/modules/user/user.install @@ -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. + */ diff --git a/modules/user/user.module b/modules/user/user.module index bbbbc8b78..fa74dd647 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -42,7 +42,7 @@ function user_help($path, $arg) { case 'admin/people/permissions': return '

' . 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 Roles 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 User Settings 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'))) . '

'; case 'admin/people/permissions/roles': - $output = '

' . 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 user permissions. Examples of roles include: anonymous user, authenticated user, moderator, administrator and so on. In this area you will define the role names of the various roles. To delete a role choose "edit".', array('@permissions' => url('admin/people/permissions'))) . '

'; + $output = '

' . 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 permissions page. 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'))) . '

'; $output .= '

'. t('By default, Drupal comes with two user roles:') . '

'; $output .= '