diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index c2a0214..a1a135d 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -16,6 +16,7 @@ use Drupal\Core\Entity\Schema\ContentEntitySchemaHandler; use Drupal\Core\Entity\Sql\DefaultTableMapping; use Drupal\Core\Entity\Sql\SqlEntityStorageInterface; +use Drupal\Core\Field\FieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Language\LanguageInterface; @@ -1335,7 +1336,7 @@ protected function deleteFieldItemsRevision(EntityInterface $entity) { protected function usesDedicatedTable(FieldStorageDefinitionInterface $definition) { // Everything that is not provided by the entity type is stored in a // dedicated table. - return $definition->getProvider() != $this->entityType->getProvider() && !$definition->hasCustomStorage(); + return ($definition->getProvider() != $this->entityType->getProvider() && !$definition->hasCustomStorage()) || $definition->isMultiple(); } /** @@ -1934,7 +1935,7 @@ public static function _fieldIndexName(FieldStorageDefinitionInterface $storage_ * unique among all other fields. */ public static function _fieldColumnName(FieldStorageDefinitionInterface $storage_definition, $column) { - return in_array($column, FieldStorageConfig::getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; + return in_array($column, FieldDefinition::getReservedColumns()) ? $column : $storage_definition->getName() . '_' . $column; } } diff --git a/core/lib/Drupal/Core/Session/SessionHandler.php b/core/lib/Drupal/Core/Session/SessionHandler.php index 40d3c45..c74585e 100644 --- a/core/lib/Drupal/Core/Session/SessionHandler.php +++ b/core/lib/Drupal/Core/Session/SessionHandler.php @@ -111,7 +111,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 24a4de9..8dd3428 100644 --- a/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityUnitTestBase.php @@ -56,8 +56,7 @@ public 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 963303f..a78a2d5 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 307cfc2..54d8954 100644 --- a/core/modules/user/src/Entity/User.php +++ b/core/modules/user/src/Entity/User.php @@ -127,12 +127,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()); @@ -145,12 +139,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); - } - } } /** @@ -161,7 +149,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti $uids = array_keys($entities); \Drupal::service('user.data')->delete(NULL, $uids); - $storage->deleteUserRoles($uids); } /** @@ -518,7 +505,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { // @todo Convert this to entity_reference_field, see // https://drupal.org/node/2044859. $fields['roles'] = FieldDefinition::create('string') - ->setCustomStorage(TRUE) ->setLabel(t('Roles')) ->setCardinality(FieldDefinition::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 2c3fd93..ec95fa0 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..690de2c 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.target_id as rid FROM {user__roles} u WHERE u.entity_id IN (:uids) AND u.target_id 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 3a9d9f5..a6ea7ca 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -7,7 +7,6 @@ namespace Drupal\user; -use Drupal\Component\Uuid\UuidInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -68,25 +67,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. @@ -100,43 +80,6 @@ public function save(EntityInterface $entity) { /** * {@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') ->fields(array('login' => $account->getLastLoginTime())) @@ -169,35 +112,6 @@ public function getSchema() { 'user__name' => array('name'), ); - $schema['users_roles'] = array( - 'description' => 'Maps users to roles.', - 'fields' => array( - 'uid' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => TRUE, - 'default' => 0, - 'description' => 'Primary Key: {users}.uid for user.', - ), - 'rid' => array( - 'type' => 'varchar', - 'length' => 64, - 'not null' => TRUE, - 'description' => 'Primary Key: ID for the role.', - ), - ), - 'primary key' => array('uid', 'rid'), - 'indexes' => array( - 'rid' => array('rid'), - ), - 'foreign keys' => array( - 'user' => array( - 'table' => 'users', - 'columns' => array('uid' => 'uid'), - ), - ), - ); - return $schema; } diff --git a/core/modules/user/src/UserStorageInterface.php b/core/modules/user/src/UserStorageInterface.php index 37b0680..c3372ac 100644 --- a/core/modules/user/src/UserStorageInterface.php +++ b/core/modules/user/src/UserStorageInterface.php @@ -7,36 +7,12 @@ namespace Drupal\user; -use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Entity\EntityStorageInterface; - /** * Defines a common interface for user entity controller classes. */ 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/user.install b/core/modules/user/user.install index eca1692..339b10a 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -64,6 +64,14 @@ function user_schema() { * Implements hook_install(). */ function user_install() { + $manager = \Drupal::entityManager(); + $definition = $manager->getFieldStorageDefinitions('user')['roles']; + $manager->getStorage('user')->onFieldStorageDefinitionCreate($definition); + + $definition = $manager->getFieldDefinitions('user', 'user')['roles']; + $manager->getStorage('user')->onFieldDefinitionCreate($definition); + + $storage = \Drupal::entityManager()->getStorage('user'); // @todo Rely on the default value for langcode in // https://drupal.org/node/1966436 diff --git a/core/modules/user/user.views.inc b/core/modules/user/user.views.inc index b2e2773..2ea4112 100644 --- a/core/modules/user/user.views.inc +++ b/core/modules/user/user.views.inc @@ -321,17 +321,17 @@ function user_views_data() { // 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( @@ -343,7 +343,7 @@ function user_views_data() { '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'), @@ -352,7 +352,7 @@ function user_views_data() { ), ); - $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 30656be..b02f278 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(). @@ -39,9 +40,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(); // Create a Home link in the main menu. $menu_link = entity_create('menu_link', array(