diff --git a/core/lib/Drupal/Core/Session/SessionHandler.php b/core/lib/Drupal/Core/Session/SessionHandler.php index 7a446dd..b71f027 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.roles_value as rid FROM {user__roles} ur WHERE ur.entity_id = :uid", array( + $rids = $this->connection->query("SELECT ur.roles_target_id 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/user/src/Plugin/entity_reference/selection/UserSelection.php b/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php index cda2429..5705fef 100644 --- a/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php +++ b/core/modules/user/src/Plugin/entity_reference/selection/UserSelection.php @@ -88,6 +88,12 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $query->condition('name', $match, $match_operator); } + // Filter by role. + $handler_settings = $this->fieldDefinition->getSetting('handler_settings'); + if (!empty($handler_settings['filter']['role'])) { + $query->condition('roles', $handler_settings['filter']['role'], 'IN'); + } + // Adding the permission check is sadly insufficient for users: core // requires us to also know about the concept of 'blocked' and 'active'. if (!\Drupal::currentUser()->hasPermission('administer users')) { diff --git a/core/modules/user/src/Plugin/views/field/Roles.php b/core/modules/user/src/Plugin/views/field/Roles.php index b4d7448..07f6cae 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.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))); + $result = $this->database->query('SELECT u.entity_id as uid, u.roles_target_id 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/Tests/UserDeleteTest.php b/core/modules/user/src/Tests/UserDeleteTest.php index 747039e..84bacb1 100644 --- a/core/modules/user/src/Tests/UserDeleteTest.php +++ b/core/modules/user/src/Tests/UserDeleteTest.php @@ -28,10 +28,10 @@ function testUserDeleteMultiple() { $uids = array($user_a->id(), $user_b->id(), $user_c->id()); // These users should have a role - $query = db_select('users_roles', 'r'); + $query = db_select('user__roles', 'r'); $roles_created = $query - ->fields('r', array('uid')) - ->condition('uid', $uids) + ->fields('r', array('entity_id')) + ->condition('entity_id', $uids) ->countQuery() ->execute() ->fetchField(); @@ -42,10 +42,10 @@ function testUserDeleteMultiple() { // Delete the users. user_delete_multiple($uids); // Test if the roles assignments are deleted. - $query = db_select('users_roles', 'r'); + $query = db_select('user__roles', 'r'); $roles_after_deletion = $query - ->fields('r', array('uid')) - ->condition('uid', $uids) + ->fields('r', array('entity_id')) + ->condition('entity_id', $uids) ->countQuery() ->execute() ->fetchField(); diff --git a/core/modules/user/src/Tests/UserEntityReferenceTest.php b/core/modules/user/src/Tests/UserEntityReferenceTest.php index 025aedf..4ee33e9 100644 --- a/core/modules/user/src/Tests/UserEntityReferenceTest.php +++ b/core/modules/user/src/Tests/UserEntityReferenceTest.php @@ -78,10 +78,14 @@ function testUserSelectionByRole() { $user3->addRole($this->role2->id()); $user3->save(); + debug(db_query('SELECT * FROM {users}')->fetchAll()); + debug(db_query('SELECT * FROM {user__roles}')->fetchAll()); + /** @var \Drupal\entity_reference\EntityReferenceAutocomplete $autocomplete */ $autocomplete = \Drupal::service('entity_reference.autocomplete'); $matches = $autocomplete->getMatches($field_definition, 'user', 'user', 'NULL', '', 'aabb'); + debug($matches); $this->assertEqual(count($matches), 2); $users = array(); foreach ($matches as $match) { diff --git a/core/modules/user/src/Tests/UserRolesAssignmentTest.php b/core/modules/user/src/Tests/UserRolesAssignmentTest.php index 124a263..71c9482 100644 --- a/core/modules/user/src/Tests/UserRolesAssignmentTest.php +++ b/core/modules/user/src/Tests/UserRolesAssignmentTest.php @@ -88,10 +88,10 @@ function testCreateUserWithRole() { private function userLoadAndCheckRoleAssigned($account, $rid, $is_assigned = TRUE) { $account = user_load($account->id(), TRUE); if ($is_assigned) { - $this->assertTrue(array_search($rid, $account->getRoles()), 'The role is present in the user object.'); + $this->assertFalse(array_search($rid, $account->getRoles()) === FALSE, 'The role is present in the user object.'); } else { - $this->assertFalse(array_search($rid, $account->getRoles()), 'The role is not present in the user object.'); + $this->assertTrue(array_search($rid, $account->getRoles()) === FALSE, 'The role is not present in the user object.'); } } } diff --git a/core/modules/user/src/UserStorageSchema.php b/core/modules/user/src/UserStorageSchema.php index 48690f7..447469d 100644 --- a/core/modules/user/src/UserStorageSchema.php +++ b/core/modules/user/src/UserStorageSchema.php @@ -26,35 +26,6 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res 'user__name' => array('name', 'langcode'), ); - $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/tests/modules/user_test_views/test_views/views.view.test_field_permission.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_field_permission.yml index 79ce551..b94e779 100644 --- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_field_permission.yml +++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_field_permission.yml @@ -77,7 +77,7 @@ display: plugin_id: user permission: id: permission - table: users_roles + table: user__roles field: permission relationship: none group_type: group diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_filter_permission.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_filter_permission.yml index 8b62fc7..22909a2 100644 --- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_filter_permission.yml +++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_filter_permission.yml @@ -78,7 +78,7 @@ display: filters: permission: id: permission - table: users_roles + table: user__roles field: permission relationship: none group_type: group diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml index 7cf5f93..3e6e278 100644 --- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml +++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml @@ -84,7 +84,7 @@ display: provider: user rid: id: rid - table: users_roles + table: user__roles field: rid relationship: none group_type: group diff --git a/core/modules/user/tests/src/Unit/Views/Argument/RolesRidTest.php b/core/modules/user/tests/src/Unit/Views/Argument/RolesRidTest.php index 4e85059..d6566cc 100644 --- a/core/modules/user/tests/src/Unit/Views/Argument/RolesRidTest.php +++ b/core/modules/user/tests/src/Unit/Views/Argument/RolesRidTest.php @@ -69,7 +69,7 @@ public function testTitleQuery() { $container->set('entity.manager', $entity_manager); \Drupal::setContainer($container); - $roles_rid_argument = new RolesRid(array(), 'users_roles_rid', array(), $entity_manager); + $roles_rid_argument = new RolesRid(array(), 'user__roles_rid', array(), $entity_manager); $roles_rid_argument->value = array(); $titles = $roles_rid_argument->title_query();