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 1d19efb..623c960 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 @@ -296,10 +296,10 @@ display: not: '0' plugin_id: boolean provider: views - rid: - id: rid + roles_target_id: + id: roles_target_id table: user__roles - field: rid + field: roles_target_id relationship: none group_type: group admin_label: '' @@ -558,7 +558,7 @@ display: user_bulk_form: '0' name: '0' status: '0' - rid: '0' + roles_target_id: '0' created: '0' access: '0' destination: true @@ -655,10 +655,10 @@ display: name: name mail: mail plugin_id: combine - rid: - id: rid + roles_target_id: + id: roles_target_id table: user__roles - field: rid + field: roles_target_id relationship: none group_type: group admin_label: '' @@ -667,11 +667,11 @@ display: group: 1 exposed: true expose: - operator_id: rid_op + operator_id: roles_target_id_op label: Role description: '' use_operator: false - operator: rid_op + operator: roles_target_id_op identifier: role required: false remember: false diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php index 5da648e..b487eeb 100644 --- a/core/modules/user/src/Entity/User.php +++ b/core/modules/user/src/Entity/User.php @@ -74,19 +74,16 @@ public function isNew() { /** * {@inheritdoc} */ - static function preCreate(EntityStorageInterface $storage, array &$values) { - parent::preCreate($storage, $values); - - // Users always have the authenticated user role. - $values['roles'][] = DRUPAL_AUTHENTICATED_RID; - } - - /** - * {@inheritdoc} - */ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); + // Make sure that the authenticated/anonymous roles are not persisted. + foreach ($this->get('roles') as $index => $item) { + if (in_array($item->target_id, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { + $this->get('roles')->offsetUnset($index); + } + } + // Update the user password if it has changed. if ($this->isNew() || ($this->pass->value && $this->pass->value != $this->original->pass->value)) { // Allow alternate password hashing schemes. @@ -160,8 +157,18 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti public function getRoles($exclude_locked_roles = FALSE) { $roles = array(); + // User with an ID always have the authenticated user role. + if (!$exclude_locked_roles) { + if ($this->isAuthenticated()) { + $roles[] = DRUPAL_AUTHENTICATED_RID; + } + else { + $roles[] = DRUPAL_ANONYMOUS_RID; + } + } + foreach ($this->get('roles') as $role) { - if (!($exclude_locked_roles && in_array($role->target_id, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)))) { + if ($role->target_id) { $roles[] = $role->target_id; } } diff --git a/core/modules/user/src/Plugin/views/field/Roles.php b/core/modules/user/src/Plugin/views/field/Roles.php index 07f6cae..3122ab0 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_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))); + $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_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/Tests/Views/HandlerFieldRoleTest.php b/core/modules/user/src/Tests/Views/HandlerFieldRoleTest.php index 6612ec4..c97624e 100644 --- a/core/modules/user/src/Tests/Views/HandlerFieldRoleTest.php +++ b/core/modules/user/src/Tests/Views/HandlerFieldRoleTest.php @@ -41,11 +41,13 @@ public function testRole() { $user->addRole($rolename_b); $user->save(); + debug(db_query('SELECT * FROM {user__roles}')->fetchAll()); + $view = Views::getView('test_views_handler_field_role'); $this->executeView($view); // The role field is populated during preRender. - $view->field['rid']->preRender($view->result); - $render = $view->field['rid']->advancedRender($view->result[0]); + $view->field['roles_target_id']->preRender($view->result); + $render = $view->field['roles_target_id']->advancedRender($view->result[0]); $this->assertEqual($rolename_b . $rolename_a, $render, 'View test_views_handler_field_role renders role assigned to user in the correct order.'); $this->assertFalse(strpos($render, $rolename_not_assigned), 'View test_views_handler_field_role does not render a role not assigned to a user.'); diff --git a/core/modules/user/src/UserViewsData.php b/core/modules/user/src/UserViewsData.php index c8f0510..30587d0 100644 --- a/core/modules/user/src/UserViewsData.php +++ b/core/modules/user/src/UserViewsData.php @@ -410,11 +410,11 @@ public function getViewsData() { $data['user__roles']['table']['join'] = array( 'users' => array( 'left_field' => 'uid', - 'field' => 'uid', + 'field' => 'entity_id', ), ); - $data['user__roles']['rid'] = array( + $data['user__roles']['roles_target_id'] = 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' => 'user__roles_rid', + 'id' => 'user__roles_target_id', 'name table' => 'role', 'name field' => 'name', 'empty field name' => t('No role'), @@ -444,7 +444,7 @@ public function getViewsData() { ), 'filter' => array( 'id' => 'user_permissions', - 'real field' => 'rid', + 'real field' => 'roles_target_id', ), ); 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 3e6e278..e900f72 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 @@ -82,10 +82,10 @@ display: format_username: '1' plugin_id: user_name provider: user - rid: - id: rid + roles_target_id: + id: roles_target_id table: user__roles - field: rid + field: roles_target_id relationship: none group_type: group admin_label: ''