diff --git a/core/includes/session.inc b/core/includes/session.inc index 839ead8..7aa9ffd 100644 --- a/core/includes/session.inc +++ b/core/includes/session.inc @@ -113,8 +113,8 @@ function _drupal_session_read($sid) { // active user. if ($values && $values['uid'] > 0 && $values['status'] == 1) { // Add roles element to $user. - $rids = db_query("SELECT ur.rid FROM {users_roles} ur WHERE ur.uid = :uid", array(':uid' => $values['uid']))->fetchCol(); - $values['roles'] = array_merge(array(DRUPAL_AUTHENTICATED_RID), $rids); + $rids = Drupal::entityManager()->getStorageController('user')->getUserRoles(array($values['uid'])); + $values['roles'] = $rids[$values['uid']]; $user = new UserSession($values); } elseif ($values) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php index c01a2e2..f8bfde6 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php @@ -506,7 +506,7 @@ protected function assertBundleOrder($order) { * * The tags and metadata should propagate to the SQL query object. */ - function testMetaData() { + public function testMetaData() { $query = \Drupal::entityQuery('entity_test_mulrev'); $query ->addTag('efq_metadata_test') @@ -516,4 +516,5 @@ function testMetaData() { global $efq_test_metadata; $this->assertEqual($efq_test_metadata, 'bar', 'Tag and metadata propagated to the SQL query object.'); } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/UserRidQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/UserRidQueryTest.php new file mode 100644 index 0000000..2303e17 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/UserRidQueryTest.php @@ -0,0 +1,54 @@ + 'User role entity query', + 'description' => 'Tests the special Entity Query service for user roles.', + 'group' => 'User', + ); + } + + /** + * Test the special user role queries. + */ + public function testUserRoleQuery() { + $perms = array('cancel account', 'change own username'); + $roles = array(); + $accounts = array(); + foreach ($perms as $perm) { + $accounts[$perm] = $this->createUser(array(), array($perm)); + foreach ($accounts[$perm]->getRoles() as $role) { + if ($role != DRUPAL_AUTHENTICATED_RID) { + $roles[$perm] = $role; + } + } + } + $this->assertEqual(count($roles), 2); + foreach ($perms as $perm) { + $results = \Drupal::entityQuery('user') + ->condition('rid', $roles[$perm]) + ->execute(); + $this->assertEqual(count($results), 1); + $this->assertEqual(reset($results), $accounts[$perm]->id()); + } + + // Test a query with an IN condition for both roles. + $results = \Drupal::entityQuery('user') + ->condition('rid', $roles) + ->execute(); + $this->assertEqual(count($results), 2); + } + +} diff --git a/core/modules/user/lib/Drupal/user/Entity/Query/Sql/QueryFactory.php b/core/modules/user/lib/Drupal/user/Entity/Query/Sql/QueryFactory.php new file mode 100644 index 0000000..e0cd265 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Entity/Query/Sql/QueryFactory.php @@ -0,0 +1,17 @@ +database = $database; + $this->storageController = $storage_controller; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container->get('database')); + return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity.manager')->getStorageController('user')); } /** @@ -78,10 +79,15 @@ 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))); - foreach ($result as $role) { - $this->items[$role->uid][$role->rid]['role'] = check_plain($roles[$role->rid]->label()); - $this->items[$role->uid][$role->rid]['rid'] = $role->rid; + $users_rids = $this->storageController->getUserRoles($uids); + foreach ($users_rids as $uid => $rids) { + foreach ($rids as $rid) { + // Don't list anonymous/authenticated user roles. + if (!in_array($rid, array(DRUPAL_AUTHENTICATED_RID, DRUPAL_ANONYMOUS_RID))) { + $this->items[$uid][$rid]['role'] = String::checkPlain($roles[$rid]->label()); + $this->items[$uid][$rid]['rid'] = $rid; + } + } } // Sort the roles for each user by role weight. $ordered_roles = array_flip(array_keys($roles)); diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index 0694019..5f0f562 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -80,18 +80,10 @@ public static function createInstance(ContainerInterface $container, EntityTypeI * {@inheritdoc} */ function postLoad(array &$queried_users) { - foreach ($queried_users as $key => $record) { - $queried_users[$key]->roles = array(); - if ($record->uid) { - $queried_users[$record->uid]->roles[] = DRUPAL_AUTHENTICATED_RID; - } - else { - $queried_users[$record->uid]->roles[] = DRUPAL_ANONYMOUS_RID; - } - } - // Add any additional roles from the database. - $this->addRoles($queried_users); + foreach ($this->getUserRoles(array_keys($queried_users)) as $uid => $rids) { + $queried_users[$uid]->roles = $rids; + } // Call the default postLoad() method. This will add fields and call // hook_user_load(). @@ -128,22 +120,40 @@ public function saveRoles(UserInterface $account) { /** * {@inheritdoc} */ - public function addRoles(array $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 getUserRoles(array $uids, array $rids = NULL) { + $roles_by_uid = array(); + foreach ($uids as $uid) { + $roles_by_uid[$uid] = array(); + if ($uid) { + $roles_by_uid[$uid] = array(DRUPAL_AUTHENTICATED_RID); + } + else { + $roles_by_uid[$uid] = array(DRUPAL_ANONYMOUS_RID); + } + } + $result = $this->database->query('SELECT rid, uid FROM {users_roles} WHERE uid IN (:uids)', array(':uids' => array_keys($roles_by_uid))); + foreach ($result as $record) { + $roles_by_uid[$record->uid][] = $record->rid; + } + return $roles_by_uid; + } + + /** + * {@inheritdoc} + */ + public function getQueryServiceName() { + return 'user.entity.query.sql'; + } + /** * {@inheritdoc} */ diff --git a/core/modules/user/lib/Drupal/user/UserStorageControllerInterface.php b/core/modules/user/lib/Drupal/user/UserStorageControllerInterface.php index 5718f93..cfdd999 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageControllerInterface.php +++ b/core/modules/user/lib/Drupal/user/UserStorageControllerInterface.php @@ -16,13 +16,6 @@ interface UserStorageControllerInterface { /** - * 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 @@ -42,4 +35,16 @@ public function deleteUserRoles(array $uids); * @param \Drupal\user\UserInterface $account */ public function updateLastLoginTimestamp(UserInterface $account); + + /** + * Returns role IDs of the provided users. + * + * @param array $uids + * User ID's for which roles should be returned. + * + * @return array + * An array of role ids per user, keyed by the user id. + */ + public function getUserRoles(array $uids); + } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 23d8706..d32e4e5 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -33,3 +33,6 @@ services: user.permissions_hash: class: Drupal\user\PermissionsHash arguments: ['@private_key', '@cache.cache'] + user.entity.query.sql: + class: Drupal\user\Entity\Query\Sql\QueryFactory + arguments: ['@database'] diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewPageControllerTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewPageControllerTest.php new file mode 100644 index 0000000..7e7afe1 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/ViewPageControllerTest.php @@ -0,0 +1,94 @@ + 'View page controller test', + 'description' => 'Tests views page controller.', + 'group' => 'Views' + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installSchema('system', array('router', 'menu_router')); + + $this->pageController = new ViewPageController($this->container->get('entity.manager')->getStorageController('view'), new ViewExecutableFactory()); + } + + /** + * Tests the page controller. + */ + public function testPageController() { + $this->assertTrue($this->pageController instanceof ViewPageController, 'Ensure the right class is stored in the container'); + + // Pass in a non existent view. + $random_view_id = $this->randomName(); + + $request = new Request(); + $request->attributes->set('view_id', $random_view_id); + $request->attributes->set('display_id', 'default'); + try { + $this->pageController->handle($request); + $this->fail('No exception thrown on non-existing view.'); + } + + catch (NotFoundHttpException $e) { + $this->pass('Exception thrown when view was not found'); + } + + $request->attributes->set('view_id', 'test_page_view'); + $output = $this->pageController->handle($request); + $this->assertTrue(is_array($output)); + $this->assertEqual($output['#view']->storage->id, 'test_page_view', 'The right view was executed.'); + + $request->attributes->set('display_id', 'page_1'); + $output = $this->pageController->handle($request); + $this->assertTrue($output instanceof Response, 'Ensure the page display returns a response object.'); + } + +} diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index aa0cb4a..a0065ed 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -41,9 +41,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->addRole('administrator'); + $user->save(); // Create a Home link in the main menu. $menu_link = entity_create('menu_link', array(