diff --git a/core/modules/block/block.admin.inc b/core/modules/block/block.admin.inc index f9fa425..a83dfad 100644 --- a/core/modules/block/block.admin.inc +++ b/core/modules/block/block.admin.inc @@ -466,7 +466,7 @@ function block_admin_configure($form, &$form_state, $module, $delta) { ':module' => $block->module, ':delta' => $block->delta, ))->fetchCol(); - $role_options = array_map('check_plain', user_roles()); + $role_options = array_map('check_plain', user_role_names()); $form['visibility']['role'] = array( '#type' => 'details', '#title' => t('Roles'), diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 26bcb25..3fc2e5b 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -1029,7 +1029,7 @@ function template_preprocess_block(&$variables) { */ function block_user_role_delete($role) { db_delete('block_role') - ->condition('rid', $role->rid) + ->condition('rid', $role->id()) ->execute(); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index d049aa3..91cab48 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -39,7 +39,7 @@ function testCommentLinks() { // Remove additional user permissions from $this->web_user added by setUp(), // since this test is limited to anonymous and authenticated roles only. - user_role_delete(key($this->web_user->roles)); + entity_delete_multiple('user_role', array(key($this->web_user->roles))); // Matrix of possible environmental conditions and configuration settings. // See setEnvironment() for details. diff --git a/core/modules/filter/filter.admin.inc b/core/modules/filter/filter.admin.inc index c01fb93..0b75ba0 100644 --- a/core/modules/filter/filter.admin.inc +++ b/core/modules/filter/filter.admin.inc @@ -190,7 +190,7 @@ function filter_admin_format_form($form, &$form_state, $format) { $form['roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles'), - '#options' => array_map('check_plain', user_roles()), + '#options' => array_map('check_plain', user_role_names()), '#disabled' => $is_fallback, ); if ($is_fallback) { diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index 1aff070..632edde 100644 --- a/core/modules/filter/filter.module +++ b/core/modules/filter/filter.module @@ -505,11 +505,11 @@ function filter_formats_reset() { function filter_get_roles_by_format($format) { // Handle the fallback format upfront (all roles have access to this format). if ($format->format == filter_fallback_format()) { - return user_roles(); + return user_role_names(); } // Do not list any roles if the permission does not exist. $permission = filter_permission_name($format); - return !empty($permission) ? user_roles(FALSE, $permission) : array(); + return !empty($permission) ? user_role_names(FALSE, $permission) : array(); } /** diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php index 4636610..9c2fd68 100644 --- a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php +++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php @@ -147,7 +147,7 @@ function testFormatRoles() { $this->assertFalse(in_array($this->disallowed_format->format, array_keys(filter_get_formats_by_role($rid))), 'A text format which a role does not have access to does not appear in the list of formats available to that role.'); // Check that the fallback format is always allowed. - $this->assertEqual(filter_get_roles_by_format(filter_format_load(filter_fallback_format())), user_roles(), 'All roles have access to the fallback format.'); + $this->assertEqual(filter_get_roles_by_format(filter_format_load(filter_fallback_format())), user_role_names(), 'All roles have access to the fallback format.'); $this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_get_formats_by_role($rid))), 'The fallback format appears in the list of allowed formats for any role.'); } diff --git a/core/modules/node/node.views.inc b/core/modules/node/node.views.inc index a67b870..e77fffb 100644 --- a/core/modules/node/node.views.inc +++ b/core/modules/node/node.views.inc @@ -690,11 +690,9 @@ function node_views_analyze($view) { // check for no access control $access = $display->getOption('access'); if (empty($access['type']) || $access['type'] == 'none') { - $select = db_select('role', 'r'); - $select->innerJoin('role_permission', 'p', 'r.rid = p.rid'); - $result = $select->fields('r', array('rid')) - ->fields('p', array('permission')) - ->condition('r.rid', array('anonymous', 'authenticated'), 'IN') + $result = db_select('role_permissions', 'p') + ->fields('p', array('rid', 'permission')) + ->condition('p.rid', array('anonymous', 'authenticated'), 'IN') ->condition('p.permission', 'access content') ->execute(); diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 6e41540..faa2566 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -482,22 +482,23 @@ protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NUL } // Create new role. - $role = new stdClass(); - $role->rid = $rid; - $role->name = $name; - $result = user_role_save($role); + $role = entity_create('user_role', array( + 'id' => $rid, + 'label' => $name, + )); + $result = $role->save(); $this->assertIdentical($result, SAVED_NEW, t('Created role ID @rid with name @name.', array( - '@name' => var_export($role->name, TRUE), - '@rid' => var_export($role->rid, TRUE), + '@name' => var_export($role->label(), TRUE), + '@rid' => var_export($role->id(), TRUE), )), t('Role')); if ($result === SAVED_NEW) { // Grant the specified permissions to the role, if any. if (!empty($permissions)) { - user_role_grant_permissions($role->rid, $permissions); + user_role_grant_permissions($role->id(), $permissions); - $assigned_permissions = db_query('SELECT permission FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchCol(); + $assigned_permissions = db_query('SELECT permission FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->id()))->fetchCol(); $missing_permissions = array_diff($permissions, $assigned_permissions); if (!$missing_permissions) { $this->pass(t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role')); @@ -506,7 +507,7 @@ protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NUL $this->fail(t('Failed to create permissions: @perms', array('@perms' => implode(', ', $missing_permissions))), t('Role')); } } - return $role->rid; + return $role->id(); } else { return FALSE; diff --git a/core/modules/user/config/user.role.anonymous.yml b/core/modules/user/config/user.role.anonymous.yml new file mode 100644 index 0000000..11defb0 --- /dev/null +++ b/core/modules/user/config/user.role.anonymous.yml @@ -0,0 +1,3 @@ +id: anonymous +label: Anonymous user +weight: 0 diff --git a/core/modules/user/config/user.role.authenticated.yml b/core/modules/user/config/user.role.authenticated.yml new file mode 100644 index 0000000..dc4b65d --- /dev/null +++ b/core/modules/user/config/user.role.authenticated.yml @@ -0,0 +1,3 @@ +id: authenticated +label: Authenticated user +weight: 1 diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 512f703..b3043de 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -126,7 +126,7 @@ public function form(array $form, array &$form_state, EntityInterface $account) '#access' => $admin, ); - $roles = array_map('check_plain', user_roles(TRUE)); + $roles = array_map('check_plain', user_role_names(TRUE)); // The disabled checkbox subelement for the 'authenticated user' role // must be generated separately and added to the checkboxes element, // because of a limitation in Form API not supporting a single disabled diff --git a/core/modules/user/lib/Drupal/user/Plugin/Core/Entity/Role.php b/core/modules/user/lib/Drupal/user/Plugin/Core/Entity/Role.php new file mode 100644 index 0000000..5cfb857 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Plugin/Core/Entity/Role.php @@ -0,0 +1,60 @@ +options['role']); return check_plain($rids[$rid]); } diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php b/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php index 634307e..da3bd92 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php +++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php @@ -23,16 +23,7 @@ class RolesRid extends ManyToOne { function title_query() { - $titles = array(); - - $query = db_select('role', 'r'); - $query->addField('r', 'name'); - $query->condition('r.rid', $this->value); - $result = $query->execute(); - foreach ($result as $term) { - $titles[] = check_plain($term->name); - } - return $titles; + return array(entity_load('user_role', $this->value)->label()); } } diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php b/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php index a31b0af..36a6a5a 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php +++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.php @@ -56,7 +56,7 @@ public function buildOptionsForm(&$form, &$form_state) { $form['roles'] = array( '#type' => 'checkboxes', '#title' => t('Restrict to the selected roles'), - '#options' => array_map('check_plain', user_roles(TRUE)), + '#options' => array_map('check_plain', user_role_names(TRUE)), '#default_value' => $this->options['roles'], '#description' => t('If no roles are selected, users from any role will be allowed.'), '#states' => array( diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/field/Roles.php b/core/modules/user/lib/Drupal/user/Plugin/views/field/Roles.php index c4eb050..3de90d4 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/views/field/Roles.php +++ b/core/modules/user/lib/Drupal/user/Plugin/views/field/Roles.php @@ -47,15 +47,15 @@ function pre_render(&$values) { } if ($uids) { - $query = db_select('role', 'r'); - $query->join('users_roles', 'u', 'u.rid = r.rid'); - $query->addField('r', 'name'); + $roles = user_roles(); + $query = db_select('users_roles', 'u'); $query->fields('u', array('uid', 'rid')); + $query->condition('u.rid', array_keys($roles)); $query->condition('u.uid', $uids); $query->orderBy('r.name'); $result = $query->execute(); foreach ($result as $role) { - $this->items[$role->uid][$role->rid]['role'] = check_plain($role->name); + $this->items[$role->uid][$role->rid]['role'] = $roles[$role->rid]->label(); $this->items[$role->uid][$role->rid]['rid'] = $role->rid; } } diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/filter/Roles.php b/core/modules/user/lib/Drupal/user/Plugin/views/filter/Roles.php index 8ee3569..139856e 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/views/filter/Roles.php +++ b/core/modules/user/lib/Drupal/user/Plugin/views/filter/Roles.php @@ -23,7 +23,7 @@ class Roles extends ManyToOne { function get_value_options() { - $this->value_options = user_roles(TRUE); + $this->value_options = user_role_names(TRUE); unset($this->value_options[DRUPAL_AUTHENTICATED_RID]); } diff --git a/core/modules/user/lib/Drupal/user/RoleStorageController.php b/core/modules/user/lib/Drupal/user/RoleStorageController.php new file mode 100644 index 0000000..4ab5727 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/RoleStorageController.php @@ -0,0 +1,76 @@ +label = trim($entity->label); + + if (!isset($entity->weight)) { + $roles = entity_load_multiple('entity_roles'); + // Set a role weight to make this new role last. + $max = array_reduce($roles, function($max, $entity) { + return $max > $entity->weight ? $max : $entity->weight; + }); + $entity->weight = $max + 1; + } + + return parent::save($entity); + } + + /** + * Overrides ConfigStorageController::resetCache(). + */ + public function resetCache(array $ids = NULL) { + parent::resetCache($ids); + + // Clear the user access cache. + drupal_static_reset('user_access'); + drupal_static_reset('user_role_permissions'); + } + + /** + * Overrides ConfigStorageController::postDelete(). + */ + protected function postDelete($entities) { + $rids = array_keys($entities); + + foreach ($entities as $entity) { + db_delete('role_permission') + ->condition('rid', $entity->id()) + ->execute(); + } + + // Update the users who have this role set: + db_delete('users_roles') + ->condition('rid', $rids) + ->execute(); + } + + /** + * Overrides ConfigStorageController::attachLoad(). + */ + protected function attachLoad(&$queried_entities, $revision_id = FALSE) { + // Sort the queried roles by their weight. + uasort($queried_entities, 'drupal_sort_weight'); + + parent::attachLoad($queried_entities, $revision_id); + } + +} diff --git a/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php b/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php index 50d4da7..a024a84 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php @@ -37,10 +37,10 @@ function testRoleAdministration() { // correspond to an integer, to test that the role administration pages // correctly distinguish between role names and IDs.) $role_name = '123'; - $edit = array('role[name]' => $role_name, 'role[rid]' => $role_name); + $edit = array('role[label]' => $role_name, 'role[id]' => $role_name); $this->drupalPost('admin/people/roles', $edit, t('Add role')); $this->assertText(t('The role has been added.'), 'The role has been added.'); - $role = user_role_load($role_name); + $role = entity_load('user_role', $role_name); $this->assertTrue(is_object($role), 'The role was successfully retrieved from the database.'); // Try adding a duplicate role. @@ -50,18 +50,18 @@ function testRoleAdministration() { // Test renaming a role. $old_name = $role_name; $role_name = '456'; - $edit = array('role[name]' => $role_name); - $this->drupalPost("admin/people/roles/edit/{$role->rid}", $edit, t('Save role')); + $edit = array('role[label]' => $role_name); + $this->drupalPost("admin/people/roles/edit/{$role->id()}", $edit, t('Save role')); $this->assertText(t('The role has been renamed.'), 'The role has been renamed.'); - $new_role = user_role_load($old_name); - $this->assertEqual($new_role->name, $role_name, 'The role name has been successfully changed.'); + $new_role = entity_load('user_role', $old_name); + $this->assertEqual($new_role->label(), $role_name, 'The role name has been successfully changed.'); // Test deleting a role. - $this->drupalPost("admin/people/roles/edit/{$role->rid}", NULL, t('Delete role')); + $this->drupalPost("admin/people/roles/edit/{$role->id()}", NULL, t('Delete role')); $this->drupalPost(NULL, NULL, t('Delete')); $this->assertText(t('The role has been deleted.'), 'The role has been deleted'); - $this->assertNoLinkByHref("admin/people/roles/edit/{$role->rid}", 'Role edit link removed.'); - $this->assertFalse(user_role_load($role_name), 'A deleted role can no longer be loaded.'); + $this->assertNoLinkByHref("admin/people/roles/edit/{$role->id()}", 'Role edit link removed.'); + $this->assertFalse(entity_load('user_role', $role_name), 'A deleted role can no longer be loaded.'); // Make sure that the system-defined roles can be edited via the user // interface. @@ -80,8 +80,8 @@ function testRoleWeightChange() { $this->drupalLogin($this->admin_user); // Pick up a random role and get its weight. - $rid = array_rand(user_roles()); - $role = user_role_load($rid); + $rid = array_rand(user_role_names()); + $role = entity_load('user_role', $rid); $old_weight = $role->weight; // Change the role weight and submit the form. @@ -90,7 +90,7 @@ function testRoleWeightChange() { $this->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.'); // Retrieve the saved role and compare its weight. - $role = user_role_load($rid); + $role = entity_load('user_role', $rid); $new_weight = $role->weight; $this->assertTrue(($old_weight + 1) == $new_weight, 'Role weight updated successfully.'); } diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index 5d7b634..3d8e981 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -34,9 +34,9 @@ function attachLoad(&$queried_users, $load_revision = FALSE) { } // Add any additional roles from the database. - $result = db_query('SELECT r.rid, r.name, ur.uid FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid IN (:uids)', array(':uids' => array_keys($queried_users))); + $result = db_query('SELECT rid, uid FROM {users_roles} WHERE uid IN (:uids)', array(':uids' => array_keys($queried_users))); foreach ($result as $record) { - $queried_users[$record->uid]->roles[$record->rid] = $record->name; + $queried_users[$record->uid]->roles[$record->rid] = $record->rid; } // Call the default attachLoad() method. This will add fields and call diff --git a/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc index dcd76d7..5fe9fbf 100644 --- a/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -208,7 +208,7 @@ function user_admin_account() { $destination = drupal_get_destination(); $status = array(t('blocked'), t('active')); - $roles = array_map('check_plain', user_roles(TRUE)); + $roles = array_map('check_plain', user_role_names(TRUE)); $accounts = array(); foreach ($result as $account) { $account = user_load($account->uid); @@ -316,7 +316,7 @@ function user_admin_settings($form, &$form_state) { // Do not allow users to set the anonymous or authenticated user roles as the // administrator role. - $roles = user_roles(); + $roles = user_role_names(); unset($roles[DRUPAL_ANONYMOUS_RID]); unset($roles[DRUPAL_AUTHENTICATED_RID]); $roles[0] = t('disabled'); @@ -659,7 +659,7 @@ function user_admin_settings_submit($form, &$form_state) { function user_admin_permissions($form, $form_state, $rid = NULL) { // Retrieve role names for columns. - $role_names = user_roles(); + $role_names = user_role_names(); if (isset($rid)) { $role_names = array($rid => $role_names[$rid]); } @@ -760,7 +760,7 @@ function user_admin_permissions_submit($form, &$form_state) { function theme_user_admin_permissions($variables) { $form = $variables['form']; - $roles = user_roles(); + $roles = user_role_names(); foreach (element_children($form['permission']) as $key) { $row = array(); // Module name @@ -829,27 +829,22 @@ function theme_user_permission_description($variables) { * @see theme_user_admin_roles() */ function user_admin_roles($form, $form_state) { - $roles = db_select('role', 'r') - ->addTag('translatable') - ->fields('r') - ->orderBy('weight') - ->orderBy('name') - ->execute(); + $roles = user_roles(); $form['roles'] = array( '#tree' => TRUE, ); $max_weight = 0; - foreach ($roles as $role) { + foreach ($roles as $rid => $role) { $max_weight = max($max_weight, $role->weight); - $form['roles'][$role->rid]['#role'] = $role; - $form['roles'][$role->rid]['#weight'] = $role->weight; - $form['roles'][$role->rid]['name'] = array( - '#markup' => check_plain($role->name), + $form['roles'][$rid]['#role'] = $role; + $form['roles'][$rid]['#weight'] = $role->weight; + $form['roles'][$rid]['name'] = array( + '#markup' => check_plain($role->label()), ); - $form['roles'][$role->rid]['weight'] = array( + $form['roles'][$rid]['weight'] = array( '#type' => 'textfield', - '#title' => t('Weight for @title', array('@title' => $role->name)), + '#title' => t('Weight for @title', array('@title' => $role->label())), '#title_display' => 'invisible', '#size' => 4, '#default_value' => $role->weight, @@ -857,26 +852,26 @@ function user_admin_roles($form, $form_state) { ); $links['edit'] = array( 'title' => t('edit role'), - 'href' => 'admin/people/roles/edit/' . $role->rid, + 'href' => 'admin/people/roles/edit/' . $rid, 'weight' => 0, ); $links['permissions'] = array( 'title' => t('edit permissions'), - 'href' => 'admin/people/permissions/' . $role->rid, + 'href' => 'admin/people/permissions/' . $rid, 'weight' => 5, ); - $form['roles'][$role->rid]['operations'] = array( + $form['roles'][$rid]['operations'] = array( '#type' => 'operations', '#links' => $links, ); } // Embed the role add form. - $add_role = (object) array( - 'rid' => NULL, - 'name' => NULL, + $add_role = entity_create('user_role', array( + 'id' => NULL, + 'label' => NULL, 'weight' => $max_weight + 1, - ); + )); $add_form = user_admin_role(array(), $form_state, $add_role); $add_form['actions']['submit']['#submit'] = array('user_admin_role_submit'); $add_form['role']['actions'] = $add_form['actions']; @@ -902,7 +897,7 @@ 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); + $role->save(); } drupal_set_message(t('The role settings have been updated.')); } @@ -956,32 +951,32 @@ function theme_user_admin_roles($variables) { * * @ingroup forms * @see user_admin_role_submit() + * @todo Move into a RoleFormController. */ function user_admin_role($form, $form_state, $role) { $form['role'] = array( '#tree' => TRUE, '#parents' => array('role'), ); - - $form['role']['name'] = array( + $form['role']['label'] = array( '#type' => 'textfield', '#title' => t('Role name'), - '#default_value' => $role->name, + '#default_value' => $role->label(), '#size' => 30, '#required' => TRUE, '#maxlength' => 64, '#description' => t('The name for this role. Example: "Moderator", "Editorial board", "Site architect".'), ); - $form['role']['rid'] = array( + $form['role']['id'] = array( '#type' => 'machine_name', - '#default_value' => $role->rid, + '#default_value' => $role->id(), '#required' => TRUE, - '#disabled' => !empty($role->rid), + '#disabled' => !$role->isNew(), '#size' => 30, '#maxlength' => 64, '#machine_name' => array( 'exists' => 'user_role_load', - 'source' => array('role', 'name'), + 'source' => array('role', 'label'), ), ); $form['role']['weight'] = array( @@ -991,12 +986,12 @@ function user_admin_role($form, $form_state, $role) { $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', - '#value' => !empty($role->rid) ? t('Save role') : t('Add role'), + '#value' => !$role->isNew() ? t('Save role') : t('Add role'), ); $form['actions']['delete'] = array( '#type' => 'submit', '#value' => t('Delete role'), - '#access' => !empty($role->rid) && !in_array($role->rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)), + '#access' => !$role->isNew() && !in_array($role->id(), array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)), '#submit' => array('user_admin_role_delete_submit'), ); @@ -1007,14 +1002,14 @@ function user_admin_role($form, $form_state, $role) { * Form submit handler for the user_admin_role() form. */ function user_admin_role_submit($form, &$form_state) { - $role = (object) $form_state['values']['role']; - $status = user_role_save($role); - if ($status === SAVED_UPDATED) { + $role = entity_create('user_role', $form_state['values']['role']); + if ($role->save() == SAVED_UPDATED) { drupal_set_message(t('The role has been renamed.')); } else { drupal_set_message(t('The role has been added.')); } + ; $form_state['redirect'] = 'admin/people/roles'; } @@ -1022,25 +1017,25 @@ function user_admin_role_submit($form, &$form_state) { * Form submit handler for the user_admin_role() form. */ function user_admin_role_delete_submit($form, &$form_state) { - $form_state['redirect'] = 'admin/people/roles/delete/' . $form_state['values']['role']['rid']; + $form_state['redirect'] = 'admin/people/roles/delete/' . $form_state['values']['role']['id']; } /** * Form to confirm role delete operation. */ function user_admin_role_delete_confirm($form, &$form_state, $role) { - $form['rid'] = array( + $form['id'] = array( '#type' => 'value', - '#value' => $role->rid, + '#value' => $role->id(), ); - return confirm_form($form, t('Are you sure you want to delete the role %name ?', array('%name' => $role->name)), 'admin/people/roles', t('This action cannot be undone.'), t('Delete')); + return confirm_form($form, t('Are you sure you want to delete the role %name ?', array('%name' => $role->label())), 'admin/people/roles', t('This action cannot be undone.'), t('Delete')); } /** * Form submit handler for user_admin_role_delete_confirm(). */ function user_admin_role_delete_confirm_submit($form, &$form_state) { - user_role_delete($form_state['values']['rid']); + entity_delete_multiple('user_role', array($form_state['values']['id'])); drupal_set_message(t('The role has been deleted.')); $form_state['redirect'] = 'admin/people/roles'; } diff --git a/core/modules/user/user.api.php b/core/modules/user/user.api.php index b5bbbf7..1faed14 100644 --- a/core/modules/user/user.api.php +++ b/core/modules/user/user.api.php @@ -426,7 +426,7 @@ function hook_user_role_insert($role) { // Save extra fields provided by the module to user roles. db_insert('my_module_table') ->fields(array( - 'rid' => $role->rid, + 'rid' => $role->id(), 'role_description' => $role->description, )) ->execute(); @@ -446,7 +446,7 @@ function hook_user_role_insert($role) { function hook_user_role_update($role) { // Save extra fields provided by the module to user roles. db_merge('my_module_table') - ->key(array('rid' => $role->rid)) + ->key(array('rid' => $role->id())) ->fields(array( 'role_description' => $role->description )) @@ -467,7 +467,7 @@ function hook_user_role_update($role) { function hook_user_role_delete($role) { // Delete existing instances of the deleted role. db_delete('my_module_table') - ->condition('rid', $role->rid) + ->condition('rid', $role->id()) ->execute(); } diff --git a/core/modules/user/user.install b/core/modules/user/user.install index 705a725..2553e17 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -148,39 +148,6 @@ function user_schema() { ), ); - $schema['role'] = array( - 'description' => 'Stores user roles.', - 'fields' => array( - 'rid' => array( - 'type' => 'varchar', - // The role ID is often used as part of a compound index; at least MySQL - // has a maximum index length of 1000 characters (333 on utf8), so we - // limit the maximum length. - 'length' => 64, - 'not null' => TRUE, - 'description' => 'Primary Key: Unique role ID.', - ), - 'name' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - 'description' => 'Role label.', - 'translatable' => TRUE, - ), - 'weight' => array( - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'The weight of this role in listings and the user interface.', - ), - ), - 'primary key' => array('rid'), - 'indexes' => array( - 'name_weight' => array('name', 'weight'), - ), - ); - $schema['role_permission'] = array( 'description' => 'Stores the permissions assigned to user roles.', 'fields' => array( @@ -326,13 +293,6 @@ function user_install() { 'status' => 1, )) ->execute(); - - // Insert built-in roles. - db_insert('role') - ->fields(array('rid', 'name', 'weight')) - ->values(array(DRUPAL_ANONYMOUS_RID, 'Anonymous user', 0)) - ->values(array(DRUPAL_AUTHENTICATED_RID, 'Authenticated user', 1)) - ->execute(); } /** @@ -1059,5 +1019,23 @@ function user_update_8016() { } /** + * Turn the {role} table into configuration entities. + */ +function user_update_8017() { + $roles = db_select('role', 'r') + ->fields('r') + ->execute() + ->fetchAll(); + + foreach ($roles as $role) { + config('user.role.' . $role->rid) + ->set('id', $role->rid) + ->set('label', $role->name) + ->set('weight', $role->weight) + ->save(); + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". */ diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 1e14a0d..154a8cb 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1,6 +1,7 @@ label(); + }, user_roles($membersonly, $permission)); +} + +/** + * Retrieve an array of roles matching specified conditions. + * + * @param $membersonly + * Set this to TRUE to exclude the 'anonymous' role. + * @param $permission + * A string containing a permission. If set, only roles containing that + * permission are returned. + * + * @return + * An associative array with the role id as the key and the role object as + * value. + */ function user_roles($membersonly = FALSE, $permission = NULL) { $user_roles = &drupal_static(__FUNCTION__); @@ -1987,23 +2007,22 @@ function user_roles($membersonly = FALSE, $permission = NULL) { } } - $query = db_select('role', 'r'); - $query->addTag('translatable'); - $query->fields('r', array('rid', 'name')); - $query->orderBy('weight'); - $query->orderBy('name'); - if (!empty($permission)) { - $query->innerJoin('role_permission', 'p', 'r.rid = p.rid'); - $query->condition('p.permission', $permission); - } + $roles = entity_load_multiple('user_role'); if ($membersonly) { - $query->condition('r.rid', DRUPAL_ANONYMOUS_RID, '!='); + unset($roles[DRUPAL_ANONYMOUS_RID]); + } + + if (!empty($permission)) { + $result = db_select('role_permission', 'p') + ->fields('p', array('rid')) + ->condition('p.rid', array_keys($roles)) + ->condition('p.permission', $permission) + ->execute()->fetchCol(); + $roles = array_intersect_key($roles, array_flip($result)); } - $roles = $query->execute()->fetchAllKeyed(); if (empty($permission)) { $user_roles[$cid] = $roles; - return $user_roles[$cid]; } return $roles; @@ -2020,87 +2039,7 @@ function user_roles($membersonly = FALSE, $permission = NULL) { * otherwise. */ function user_role_load($rid) { - return db_select('role', 'r') - ->fields('r') - ->condition('rid', $rid) - ->execute() - ->fetchObject(); -} - -/** - * Save a user role to the database. - * - * @param $role - * A role object to modify or add. - * - * @return - * Status constant indicating if role was created or updated. - * Failure to write the user role record will return FALSE. Otherwise - * SAVED_NEW or SAVED_UPDATED is returned depending on the operation - * performed. - */ -function user_role_save($role) { - if ($role->name) { - // 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; - } - - // Let modules modify the user role before it is saved to the database. - module_invoke_all('user_role_presave', $role); - - $exists = db_select('role', 'r') - ->fields('r', array('rid')) - ->condition('rid', $role->rid) - ->execute() - ->fetchAll(); - - if (empty($exists)) { - $status = drupal_write_record('role', $role); - module_invoke_all('user_role_insert', $role); - } - else { - $status = drupal_write_record('role', $role, 'rid'); - module_invoke_all('user_role_update', $role); - } - - // Clear the user access cache. - drupal_static_reset('user_access'); - drupal_static_reset('user_role_permissions'); - - return $status; -} - -/** - * Delete a user role from database. - * - * @param $role - * A string with the role ID. - */ -function user_role_delete($role) { - $role = user_role_load($role); - - db_delete('role') - ->condition('rid', $role->rid) - ->execute(); - db_delete('role_permission') - ->condition('rid', $role->rid) - ->execute(); - // Update the users who have this role set: - db_delete('users_roles') - ->condition('rid', $role->rid) - ->execute(); - - module_invoke_all('user_role_delete', $role); - - // Clear the user access cache. - drupal_static_reset('user_access'); - drupal_static_reset('user_role_permissions'); + return entity_load('user_role', $rid); } /** @@ -2108,7 +2047,7 @@ function user_role_delete($role) { */ function user_role_delete_access($role) { // Prevent the system-defined roles from being removed. - if ($role->rid == DRUPAL_ANONYMOUS_RID || $role->rid == DRUPAL_AUTHENTICATED_RID) { + if ($role->id() == DRUPAL_ANONYMOUS_RID || $role->id() == DRUPAL_AUTHENTICATED_RID) { return FALSE; } @@ -2247,7 +2186,7 @@ function user_user_operations($form = array(), $form_state = array()) { ); if (user_access('administer permissions')) { - $roles = user_roles(TRUE); + $roles = user_role_names(TRUE); unset($roles[DRUPAL_AUTHENTICATED_RID]); // Can't edit authenticated role. $add_roles = array(); @@ -2332,7 +2271,7 @@ function user_user_operations_block($accounts) { * Callback function for admin mass adding/deleting a user role. */ function user_multiple_role_edit($accounts, $operation, $rid) { - $role_name = db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchField(); + $role_name = entity_load('user_role', $rid)->label(); switch ($operation) { case 'add_role': @@ -2464,7 +2403,7 @@ function user_multiple_cancel_confirm_submit($form, &$form_state) { function user_filters() { // Regular filters $filters = array(); - $roles = user_roles(TRUE); + $roles = user_role_names(TRUE); unset($roles[DRUPAL_AUTHENTICATED_RID]); // Don't list authorized role. if (count($roles)) { $filters['role'] = array( diff --git a/core/modules/user/user.views.inc b/core/modules/user/user.views.inc index 9264381..868c3ab 100644 --- a/core/modules/user/user.views.inc +++ b/core/modules/user/user.views.inc @@ -351,22 +351,6 @@ function user_views_data() { ), ); - // role table - - $data['role']['table']['join'] = array( - // Directly links to users table. - 'users' => array( - 'left_table' => 'users_roles', - 'left_field' => 'rid', - 'field' => 'rid', - ), - // needed for many to one helper sometimes - 'users_roles' => array( - 'left_field' => 'rid', - 'field' => 'rid', - ), - ); - // permission table $data['role_permission']['table']['group'] = t('User'); $data['role_permission']['table']['join'] = array( diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php index c7771c0..f85ea98 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/filter/FilterPluginBase.php @@ -551,7 +551,7 @@ public function buildExposeForm(&$form, &$form_state) { '#default_value' => $this->options['expose']['remember'], ); - $role_options = array_map('check_plain', user_roles()); + $role_options = array_map('check_plain', user_role_names()); $form['expose']['remember_roles'] = array( '#type' => 'checkboxes', '#title' => t('User roles'), diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 48da6b1..6a574a0 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -387,18 +387,19 @@ function standard_install() { user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content', 'access comments', 'post comments', 'skip comment approval', $filtered_html_permission)); // Create a default role for site administrators, with all available permissions assigned. - $admin_role = new stdClass(); - $admin_role->rid = 'administrator'; - $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'))); + $admin_role = entity_create('user_role', array( + 'id' => 'administrator', + 'label' => 'Administrator', + 'weight' => 2, + )); + $admin_role->save(); + user_role_grant_permissions($admin_role->id(), array_keys(module_invoke_all('permission'))); // Set this as the administrator role. - $user_settings->set('admin_role', $admin_role->rid)->save(); + $user_settings->set('admin_role', $admin_role->id())->save(); // Assign user 1 the "administrator" role. db_insert('users_roles') - ->fields(array('uid' => 1, 'rid' => $admin_role->rid)) + ->fields(array('uid' => 1, 'rid' => $admin_role->id())) ->execute(); // Create a Home link in the main menu.