diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index adeb43d..a0f07ae 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -198,6 +198,15 @@ public function onEntityTypeCreate(EntityTypeInterface $entity_type) { $schema_handler->createTable($table_name, $table_schema); } } + + // Explicitly create the tables for base fields with dedicated tables. + $table_mapping = $this->storage->getTableMapping(); + foreach ($this->entityManager->getFieldStorageDefinitions($this->entityType->id()) as $field_storage_definition) { + if ($field_storage_definition->isBaseField() && $table_mapping->requiresDedicatedTableStorage($field_storage_definition)) { + $this->onFieldStorageDefinitionCreate($field_storage_definition); + } + } + $this->saveEntitySchemaData($entity_type, $schema); } diff --git a/core/lib/Drupal/Core/Session/SessionHandler.php b/core/lib/Drupal/Core/Session/SessionHandler.php index c8d69fb..7a446dd 100644 --- a/core/lib/Drupal/Core/Session/SessionHandler.php +++ b/core/lib/Drupal/Core/Session/SessionHandler.php @@ -121,7 +121,7 @@ public function read($sid) { // active user. if ($values && $values['uid'] > 0 && $values['status'] == 1) { // Add roles element to $user. - $rids = $this->connection->query("SELECT ur.rid FROM {users_roles} ur WHERE ur.uid = :uid", array( + $rids = $this->connection->query("SELECT ur.roles_value as rid FROM {user__roles} ur WHERE ur.entity_id = :uid", array( ':uid' => $values['uid'], ))->fetchCol(); $values['roles'] = array_merge(array(DRUPAL_AUTHENTICATED_RID), $rids); diff --git a/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php index 243f48d..ce557c4 100644 --- a/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php @@ -56,8 +56,7 @@ protected function setUp() { * @param array $values * (optional) The values used to create the entity. * @param array $permissions - * (optional) Array of permission names to assign to user. The - * users_roles tables must be installed before this can be used. + * (optional) Array of permission names to assign to user. * * @return \Drupal\user\Entity\User * The created user entity. diff --git a/core/modules/user/config/install/views.view.user_admin_people.yml b/core/modules/user/config/install/views.view.user_admin_people.yml index ee79c86..1d19efb 100644 --- a/core/modules/user/config/install/views.view.user_admin_people.yml +++ b/core/modules/user/config/install/views.view.user_admin_people.yml @@ -298,7 +298,7 @@ display: provider: views rid: id: rid - table: users_roles + table: user__roles field: rid relationship: none group_type: group @@ -657,7 +657,7 @@ display: plugin_id: combine rid: id: rid - table: users_roles + table: user__roles field: rid relationship: none group_type: group @@ -698,7 +698,7 @@ display: provider: user permission: id: permission - table: users_roles + table: user__roles field: permission relationship: none group_type: group diff --git a/core/modules/user/config/schema/user.views.schema.yml b/core/modules/user/config/schema/user.views.schema.yml index fc8a7fd..b879285 100644 --- a/core/modules/user/config/schema/user.views.schema.yml +++ b/core/modules/user/config/schema/user.views.schema.yml @@ -23,7 +23,7 @@ views.argument.user_uid: type: views.argument.numeric label: 'User ID' -views.argument.users_roles_rid: +views.argument.user__roles_rid: type: views.argument.many_to_one label: 'Role ID' diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php index eb46586..b5a8dde 100644 --- a/core/modules/user/src/Entity/User.php +++ b/core/modules/user/src/Entity/User.php @@ -130,12 +130,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { } } - // Update user roles if changed. - if ($this->getRoles() != $this->original->getRoles()) { - $storage->deleteUserRoles(array($this->id())); - $storage->saveRoles($this); - } - // If the user was blocked, delete the user's sessions to force a logout. if ($this->original->status->value != $this->status->value && $this->status->value == 0) { $session_manager->delete($this->id()); @@ -148,12 +142,6 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { _user_mail_notify($op, $this); } } - else { - // Save user roles. - if (count($this->getRoles()) > 1) { - $storage->saveRoles($this); - } - } } /** @@ -164,7 +152,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti $uids = array_keys($entities); \Drupal::service('user.data')->delete(NULL, $uids); - $storage->deleteUserRoles($uids); } /** @@ -534,7 +521,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { // @todo Convert this to entity_reference_field, see // https://drupal.org/node/2044859. $fields['roles'] = BaseFieldDefinition::create('string') - ->setCustomStorage(TRUE) ->setLabel(t('Roles')) ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) ->setDescription(t('The roles the user has.')); diff --git a/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php b/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php index 3820628..cda2429 100644 --- a/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php +++ b/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php @@ -131,16 +131,5 @@ public function entityQueryAlter(SelectInterface $query) { } } } - - // Add the filter by role option. - if (!empty($this->fieldDefinition->getSetting('handler_settings')['filter'])) { - $filter_settings = $this->fieldDefinition->getSetting('handler_settings')['filter']; - if ($filter_settings['type'] == 'role') { - $tables = $query->getTables(); - $base_table = $tables['base_table']['alias']; - $query->join('users_roles', 'ur', $base_table . '.uid = ur.uid'); - $query->condition('ur.rid', $filter_settings['role']); - } - } } } diff --git a/core/modules/user/src/Plugin/views/argument/RolesRid.php b/core/modules/user/src/Plugin/views/argument/RolesRid.php index 8c7fc7f..bbc5ab9 100644 --- a/core/modules/user/src/Plugin/views/argument/RolesRid.php +++ b/core/modules/user/src/Plugin/views/argument/RolesRid.php @@ -17,7 +17,7 @@ * * @ingroup views_argument_handlers * - * @ViewsArgument("users_roles_rid") + * @ViewsArgument("user__roles_rid") */ class RolesRid extends ManyToOne { diff --git a/core/modules/user/src/Plugin/views/field/Roles.php b/core/modules/user/src/Plugin/views/field/Roles.php index 57506a8..b4d7448 100644 --- a/core/modules/user/src/Plugin/views/field/Roles.php +++ b/core/modules/user/src/Plugin/views/field/Roles.php @@ -79,7 +79,7 @@ public function preRender(&$values) { if ($uids) { $roles = user_roles(); - $result = $this->database->query('SELECT u.uid, u.rid FROM {users_roles} u WHERE u.uid IN (:uids) AND u.rid IN (:rids)', array(':uids' => $uids, ':rids' => array_keys($roles))); + $result = $this->database->query('SELECT u.entity_id as uid, u.roles_value as rid FROM {user__roles} u WHERE u.entity_id IN (:uids) AND u.roles_value IN (:rids)', array(':uids' => $uids, ':rids' => array_keys($roles))); foreach ($result as $role) { $this->items[$role->uid][$role->rid]['role'] = String::checkPlain($roles[$role->rid]->label()); $this->items[$role->uid][$role->rid]['rid'] = $role->rid; diff --git a/core/modules/user/src/RoleStorage.php b/core/modules/user/src/RoleStorage.php index 2afd00e..a3f6d0f 100644 --- a/core/modules/user/src/RoleStorage.php +++ b/core/modules/user/src/RoleStorage.php @@ -34,8 +34,8 @@ public function isPermissionInRoles($permission, array $rids) { */ public function deleteRoleReferences(array $rids) { // Remove the role from all users. - db_delete('users_roles') - ->condition('rid', $rids) + db_delete('user__roles') + ->condition('target_id', $rids) ->execute(); } diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php index 9aa844e..638dfc7 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -68,25 +68,6 @@ public static function createInstance(ContainerInterface $container, EntityTypeI /** * {@inheritdoc} */ - function mapFromStorageRecords(array $records) { - foreach ($records as $record) { - $record->roles = array(); - if ($record->uid) { - $record->roles[] = DRUPAL_AUTHENTICATED_RID; - } - else { - $record->roles[] = DRUPAL_ANONYMOUS_RID; - } - } - - // Add any additional roles from the database. - $this->addRoles($records); - return parent::mapFromStorageRecords($records); - } - - /** - * {@inheritdoc} - */ public function save(EntityInterface $entity) { // The anonymous user account is saved with the fixed user ID of 0. // Therefore we need to check for NULL explicitly. @@ -108,43 +89,6 @@ protected function isColumnSerial($table_name, $schema_name) { /** * {@inheritdoc} */ - public function saveRoles(UserInterface $account) { - $query = $this->database->insert('users_roles')->fields(array('uid', 'rid')); - foreach ($account->getRoles() as $rid) { - if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { - $query->values(array( - 'uid' => $account->id(), - 'rid' => $rid, - )); - } - } - $query->execute(); - } - - /** - * {@inheritdoc} - */ - public function addRoles(array $users) { - if ($users) { - $result = $this->database->query('SELECT rid, uid FROM {users_roles} WHERE uid IN (:uids)', array(':uids' => array_keys($users))); - foreach ($result as $record) { - $users[$record->uid]->roles[] = $record->rid; - } - } - } - - /** - * {@inheritdoc} - */ - public function deleteUserRoles(array $uids) { - $this->database->delete('users_roles') - ->condition('uid', $uids) - ->execute(); - } - - /** - * {@inheritdoc} - */ public function updateLastLoginTimestamp(UserInterface $account) { $this->database->update('users_field_data') ->fields(array('login' => $account->getLastLoginTime())) diff --git a/core/modules/user/src/UserStorageInterface.php b/core/modules/user/src/UserStorageInterface.php index a73b58b..2a114c5 100644 --- a/core/modules/user/src/UserStorageInterface.php +++ b/core/modules/user/src/UserStorageInterface.php @@ -15,27 +15,6 @@ interface UserStorageInterface { /** - * Add any roles from the storage to the user. - * - * @param array $users - */ - public function addRoles(array $users); - - /** - * Save the user's roles. - * - * @param \Drupal\user\UserInterface $account - */ - public function saveRoles(UserInterface $account); - - /** - * Remove the roles of a user. - * - * @param array $uids - */ - public function deleteUserRoles(array $uids); - - /** * Update the last login timestamp of the user. * * @param \Drupal\user\UserInterface $account diff --git a/core/modules/user/src/UserViewsData.php b/core/modules/user/src/UserViewsData.php index 0d5e544..c8f0510 100644 --- a/core/modules/user/src/UserViewsData.php +++ b/core/modules/user/src/UserViewsData.php @@ -404,17 +404,17 @@ public function getViewsData() { // Define the base group of this table. Fields that don't have a group defined // will go into this field by default. - $data['users_roles']['table']['group'] = t('User'); + $data['user__roles']['table']['group'] = t('User'); // Explain how this table joins to others. - $data['users_roles']['table']['join'] = array( + $data['user__roles']['table']['join'] = array( 'users' => array( 'left_field' => 'uid', 'field' => 'uid', ), ); - $data['users_roles']['rid'] = array( + $data['user__roles']['rid'] = array( 'title' => t('Roles'), 'help' => t('Roles that a user belongs to.'), 'field' => array( @@ -426,7 +426,7 @@ public function getViewsData() { 'allow empty' => TRUE, ), 'argument' => array( - 'id' => 'users_roles_rid', + 'id' => 'user__roles_rid', 'name table' => 'role', 'name field' => 'name', 'empty field name' => t('No role'), @@ -435,7 +435,7 @@ public function getViewsData() { ), ); - $data['users_roles']['permission'] = array( + $data['user__roles']['permission'] = array( 'title' => t('Permission'), 'help' => t('The user permissions.'), 'field' => array( diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index c2b5582..fdca17c 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -5,6 +5,7 @@ */ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\user\Entity\User; /** * Implements hook_install(). @@ -45,9 +46,9 @@ function standard_install() { $user_settings->set('admin_role', 'administrator')->save(); // Assign user 1 the "administrator" role. - db_insert('users_roles') - ->fields(array('uid' => 1, 'rid' => 'administrator')) - ->execute(); + $user = User::load(1); + $user->roles[] = 'administrator'; + $user->save(); // Enable the Contact link in the footer menu. /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */