Index: modules/user/user.install =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.install,v retrieving revision 1.40 diff -u -r1.40 user.install --- modules/user/user.install 1 Mar 2010 07:39:12 -0000 1.40 +++ modules/user/user.install 17 Mar 2010 23:29:49 -0000 @@ -98,6 +98,13 @@ 'default' => '', 'description' => 'Unique role name.', ), + 'weight' => array( + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The order of this role in listings and the user interface.', + ), ), 'unique keys' => array( 'name' => array('name'), @@ -545,11 +552,6 @@ } /** - * @} 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}. */ function user_update_7006(&$sandbox) { @@ -574,3 +576,17 @@ ->execute(); } } + +/** + * Add a weight column to user roles. + */ +function user_update_7007() { + db_drop_index('role', 'name'); + db_add_field('role', 'weight', array('type' => 'int', 'size' => 'tiny', '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. + */ Index: modules/user/user.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.admin.inc,v retrieving revision 1.101 diff -u -r1.101 user.admin.inc --- modules/user/user.admin.inc 7 Mar 2010 06:49:10 -0000 1.101 +++ modules/user/user.admin.inc 17 Mar 2010 23:29:49 -0000 @@ -764,57 +764,141 @@ } /** - * Menu callback: administer roles. + * Form to re-order roles or add a new one. * * @ingroup forms - * @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() { - $rid = arg(5); - if ($rid) { - if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) { - drupal_goto('admin/people/permissions/roles'); - } - // Display the edit role form. - $role = db_query('SELECT * FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchObject(); - $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' => $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() { + $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'), - ); - $form['#submit'][] = 'user_admin_role_submit'; - $form['#validate'][] = 'user_admin_role_validate'; + '#size' => 4, + '#default_value' => $order, + '#attributes' => array('class' => array('role-weight')), + ); + $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'), '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; } @@ -856,32 +940,6 @@ } /** - * 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. * * @ingroup themeable Index: modules/user/user.css =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.css,v retrieving revision 1.19 diff -u -r1.19 user.css --- modules/user/user.css 30 Jan 2010 07:59:26 -0000 1.19 +++ modules/user/user.css 17 Mar 2010 23:29:49 -0000 @@ -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; } Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.1136 diff -u -r1.1136 user.module --- modules/user/user.module 12 Mar 2010 15:56:30 -0000 1.1136 +++ modules/user/user.module 17 Mar 2010 23:29:51 -0000 @@ -48,6 +48,7 @@ $output .= '
' . t('You may reorder permissions by using the drag handles. It is generally recommended to sort your roles from most restricted (anonymous) to most privileged (administrator roles).') . '
'; return $output; case 'admin/config/people/accounts/fields': return '' . t('This form lets administrators add, edit, and arrange fields for storing user data.') . '
'; @@ -102,7 +103,7 @@ 'render element' => 'form', 'file' => 'user.admin.inc', ), - 'user_admin_new_role' => array( + 'user_admin_roles' => array( 'render element' => 'form', 'file' => 'user.admin.inc', ), @@ -1538,6 +1539,8 @@ '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.', @@ -1557,20 +1560,20 @@ '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, 'weight' => -5, ); - $items['admin/people/permissions/roles/edit'] = array( + $items['admin/people/permissions/roles/edit/%user_role'] = array( 'title' => 'Edit role', - 'page arguments' => array('user_admin_role'), + 'page arguments' => array('user_admin_role', 5), 'access arguments' => array('administer permissions'), 'type' => MENU_CALLBACK, ); - + // Add a new user form. $items['admin/people/create'] = array( 'title' => 'Add user', 'page arguments' => array('create'), @@ -2532,17 +2535,11 @@ * 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.* 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 * FROM {role} ORDER BY weight, name'); } foreach ($result as $role) { @@ -2575,7 +2572,7 @@ * exists, FALSE otherwise. */ function user_role_load($role) { - $field = is_int($role) ? 'rid' : 'name'; + $field = is_numeric($role) ? 'rid' : 'name'; return db_select('role', 'r') ->fields('r') ->condition($field, $role) @@ -3083,7 +3080,6 @@ 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; }