Commit 3b305095 authored by webchick's avatar webchick

#347250 by catch, drewish, and Berdir: Add function for loading multiple users in one request.

parent 8a0d326a
......@@ -1283,7 +1283,7 @@ function comment_validate($edit) {
form_set_error('date', t('You have to specify a valid date.'));
}
}
if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
if (isset($edit['author']) && !$account = user_load_by_name($edit['author'])) {
form_set_error('author', t('You have to specify a valid author.'));
}
......@@ -1607,7 +1607,7 @@ function comment_form_add_preview($form, &$form_state) {
// Attach the user and time information.
if (!empty($edit['author'])) {
$account = user_load(array('name' => $edit['author']));
$account = user_load_by_name($edit['author']);
}
elseif ($user->uid && !isset($edit['is_anonymous'])) {
$account = $user;
......@@ -1687,7 +1687,7 @@ function _comment_form_submit(&$comment_values) {
$comment_values['timestamp'] = strtotime($comment_values['date']);
if (isset($comment_values['author'])) {
$account = user_load(array('name' => $comment_values['author']));
$account = user_load_by_name($comment_values['author']);
$comment_values['uid'] = $account->uid;
$comment_values['name'] = $comment_values['author'];
}
......
......@@ -188,7 +188,7 @@ class DBLogTestCase extends DrupalWebTestCase {
$this->drupalPost('admin/user/user/create', $edit, t('Create new account'));
$this->assertResponse(200);
// Retrieve user object.
$user = user_load(array('name' => $name)); //, 'pass' => $pass, 'status' => 1));
$user = user_load_by_name($name);
$this->assertTrue($user != null, t('User @name was loaded', array('@name' => $name)));
$user->pass_raw = $pass; // Needed by drupalLogin.
// Login user.
......
......@@ -984,7 +984,7 @@ function node_validate($node, $form = array()) {
if (user_access('administer nodes')) {
// Validate the "authored by" field.
if (!empty($node->name) && !($account = user_load(array('name' => $node->name)))) {
if (!empty($node->name) && !($account = user_load_by_name($node->name))) {
// The use of empty() is mandatory in the context of usernames
// as the empty string denotes the anonymous user. In case we
// are dealing with an anonymous user we set the user ID to 0.
......@@ -1035,7 +1035,7 @@ function node_submit($node) {
if (user_access('administer nodes')) {
// Populate the "authored by" field.
if ($account = user_load(array('name' => $node->name))) {
if ($account = user_load_by_name($node->name)) {
$node->uid = $account->uid;
}
else {
......
......@@ -353,7 +353,7 @@ function node_preview($node) {
if (isset($node->name)) {
// The use of isset() is mandatory in the context of user IDs, because
// user ID 0 denotes the anonymous user.
if ($user = user_load(array('name' => $node->name))) {
if ($user = user_load_by_name($node->name)) {
$node->uid = $user->uid;
$node->picture = $user->picture;
}
......@@ -362,7 +362,7 @@ function node_preview($node) {
}
}
elseif ($node->uid) {
$user = user_load(array('uid' => $node->uid));
$user = user_load($node->uid);
$node->name = $user->name;
$node->picture = $user->picture;
}
......
......@@ -202,13 +202,6 @@ function profile_block_view($delta = '') {
}
}
/**
* Implementation of hook_user_load().
*/
function profile_user_load(&$edit, &$user, $category = NULL) {
return profile_load_profile($user);
}
/**
* Implementation of hook_user_register().
*/
......@@ -270,11 +263,14 @@ function profile_user_cancel(&$edit, &$account, $method) {
}
}
function profile_load_profile(&$user) {
$result = db_query('SELECT f.name, f.type, v.value FROM {profile_field} f INNER JOIN {profile_value} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
while ($field = db_fetch_object($result)) {
if (empty($user->{$field->name})) {
$user->{$field->name} = _profile_field_serialize($field->type) ? unserialize($field->value) : $field->value;
/**
* Implementation of hook_user_load().
*/
function profile_user_load($users) {
$result = db_query('SELECT f.name, f.type, v.uid, v.value FROM {profile_field} f INNER JOIN {profile_value} v ON f.fid = v.fid WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
foreach ($result as $record) {
if (empty($users[$record->uid]->{$record->name})) {
$users[$record->uid]->{$record->name} = _profile_field_serialize($record->type) ? unserialize($record->value) : $record->value;
}
}
}
......@@ -341,7 +337,7 @@ function profile_view_field($user, $field) {
function profile_view_profile(&$user) {
profile_load_profile($user);
$user = user_load($user->uid);
// Show private fields to administrators and people viewing their own account.
if (user_access('administer users') || $GLOBALS['user']->uid == $user->uid) {
......
......@@ -55,11 +55,13 @@ function profile_browse() {
}
// Extract the affected users:
$result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_value} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.access != 0 AND u.status != 0 ORDER BY u.access DESC", 20, 0, NULL, $arguments);
$result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_value} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.access != 0 AND u.status != 0 ORDER BY u.access DESC", 20, 0, NULL, $arguments)->fetchAllAssoc('uid');
// Load the users.
$users = user_load_multiple(array_keys($result));
$content = '';
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
foreach ($users as $account) {
$profile = _profile_update_user_fields($fields, $account);
$content .= theme('profile_listing', $account, $profile);
}
......@@ -88,11 +90,10 @@ function profile_browse() {
}
// Extract the affected users:
$result = pager_query('SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 AND access != 0 ORDER BY access DESC', 20, 0, NULL);
$result = pager_query('SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 AND access != 0 ORDER BY access DESC', 20, 0, NULL)->fetchAllAssoc('uid');
$users = user_load_multiple(array_keys($result));
$content = '';
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
foreach ($users as $account) {
$profile = _profile_update_user_fields($fields, $account);
$content .= theme('profile_listing', $account, $profile);
}
......
......@@ -849,7 +849,7 @@ protected function setUp() {
// Log in with a clean $user.
$this->originalUser = $user;
drupal_save_session(FALSE);
$user = user_load(array('uid' => 1));
$user = user_load(1);
// Restore necessary variables.
variable_set('install_profile', 'default');
......
......@@ -434,7 +434,7 @@ class FileValidatorTest extends DrupalWebTestCase {
drupal_save_session(FALSE);
// Run these test as uid = 1.
$user = user_load(array('uid' => 1));
$user = user_load(1);
$file = new stdClass();
$file->filesize = 999999;
......
......@@ -40,7 +40,7 @@ function statistics_node_tracker() {
}
function statistics_user_tracker() {
if ($account = user_load(array('uid' => arg(1)))) {
if ($account = user_load(arg(1))) {
$header = array(
array('data' => t('Timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
......
......@@ -2013,7 +2013,7 @@ function system_send_email_action($object, $context) {
if (isset($node)) {
if (!isset($account)) {
$account = user_load(array('uid' => $node->uid));
$account = user_load($node->uid);
}
if ($recipient == '%author') {
$recipient = $account->mail;
......
......@@ -195,7 +195,7 @@ function _trigger_normalize_node_context($type, $node) {
// An action that works on users is being called in a node context.
// Load the user object of the node's author.
case 'user':
return user_load(array('uid' => $node->uid));
return user_load($node->uid);
}
}
......@@ -298,7 +298,7 @@ function _trigger_normalize_comment_context($type, $comment) {
// An action that works on users is being called in a comment context.
case 'user':
return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid));
return user_load(is_array($comment) ? $comment['uid'] : $comment->uid);
}
}
......
......@@ -121,7 +121,7 @@ function _update_cron_notify() {
if (!empty($notify_list)) {
$default_language = language_default();
foreach ($notify_list as $target) {
if ($target_user = user_load(array('mail' => $target))) {
if ($target_user = user_load_by_mail($target)) {
$target_language = user_preferred_language($target_user);
}
else {
......
......@@ -84,6 +84,25 @@ function hook_user($op, &$edit, &$account, $category = NULL) {
return $form;
}
}
/**
* Act on user objects when loaded from the database.
*
* Due to the static cache in user_load_multiple() you should not use this
* hook to modify the user properties returned by the {users} table itself
* since this may result in unreliable results when loading from cache.
*
* @param $users
* An array of user objects, indexed by uid.
*
* @see user_load_multiple()
* @see profile_user_load()
*/
function hook_user_load($users) {
$result = db_query('SELECT * FROM {my_table} WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
foreach ($result as $record) {
$users[$record->uid]->foo = $result->foo;
}
}
/**
* Act on user account cancellations.
......
This diff is collapsed.
......@@ -44,10 +44,12 @@ function user_pass() {
function user_pass_validate($form, &$form_state) {
$name = trim($form_state['values']['name']);
// Try to load by email.
$account = user_load(array('mail' => $name, 'status' => 1));
$users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
$account = reset($users);
if (!$account) {
// No success, try to load by name.
$account = user_load(array('name' => $name, 'status' => 1));
$users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
$account = reset($users);
}
if (isset($account->uid)) {
form_set_value(array('#parents' => array('account')), $account, $form_state);
......@@ -86,7 +88,8 @@ function user_pass_reset(&$form_state, $uid, $timestamp, $hashed_pass, $action =
$timeout = 86400;
$current = REQUEST_TIME;
// Some redundant checks for extra security ?
if ($timestamp < $current && $account = user_load(array('uid' => $uid, 'status' => 1)) ) {
$users = user_load_multiple(array($uid), array('status' => '1'));
if ($timestamp < $current && $account = reset($users)) {
// No time out for first time login.
if ($account->login && $current - $timestamp > $timeout) {
drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'));
......
......@@ -31,7 +31,8 @@ class UserRegistrationTestCase extends DrupalWebTestCase {
$this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), t('User registered successfully.'));
// Check database for created user.
$user = user_load($edit);
$users = user_load_multiple(array(), array('name' => $name, 'mail' => $mail));
$user = reset($users);
$this->assertTrue($user, t('User found in database.'));
$this->assertTrue($user->uid > 0, t('User has valid user id.'));
......@@ -74,7 +75,7 @@ class UserRegistrationTestCase extends DrupalWebTestCase {
// Make sure password changes are present in database.
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
$user = user_load(array('uid' => $user->uid));
$user = user_load($user->uid, TRUE);
$this->assertTrue(user_check_password($new_pass, $user), t('Correct password in database.'));
// Logout of user account.
......@@ -170,7 +171,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
$account = $this->drupalCreateUser(array());
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
......@@ -183,7 +184,8 @@ class UserCancelTestCase extends DrupalWebTestCase {
$timestamp = $account->login;
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertResponse(403, t('Bogus cancelling request rejected.'));
$this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, t('User account was not canceled.'));
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
......@@ -200,7 +202,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
......@@ -217,13 +219,14 @@ class UserCancelTestCase extends DrupalWebTestCase {
$bogus_timestamp = $timestamp + 60;
$this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Bogus cancelling request rejected.'));
$this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, t('User account was not canceled.'));
// Attempt expired account cancellation request confirmation.
$bogus_timestamp = $timestamp - 86400 - 60;
$this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Expired cancel account request rejected.'));
$this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
$this->assertTrue(reset(user_load_multiple(array($account->uid), array('status' => 1))), t('User account was not canceled.'));
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
......@@ -237,10 +240,11 @@ class UserCancelTestCase extends DrupalWebTestCase {
variable_set('user_cancel_method', 'user_cancel_block');
// Create a user.
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
$web_user = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($web_user);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($web_user->uid, TRUE);
// Attempt to cancel account.
$this->drupalGet('user/' . $account->uid . '/edit');
......@@ -251,12 +255,14 @@ class UserCancelTestCase extends DrupalWebTestCase {
// Confirm account cancellation.
$timestamp = time();
$this->drupalPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 0)), t('User has been blocked.'));
$account = user_load($account->uid, TRUE);
$this->assertTrue($account->status == 0, t('User has been blocked.'));
// Confirm user is logged out.
$this->assertNoText($account->name, t('Logged out.'));
......@@ -272,7 +278,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($account->uid, TRUE);
// Create a node with two revisions.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
......@@ -293,7 +299,8 @@ class UserCancelTestCase extends DrupalWebTestCase {
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 0)), t('User has been blocked.'));
$account = user_load($account->uid, TRUE);
$this->assertTrue($account->status == 0, t('User has been blocked.'));
// Confirm user's content has been unpublished.
$test_node = node_load($node->nid, NULL, TRUE);
......@@ -315,7 +322,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($account->uid, TRUE);
// Create a simple node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
......@@ -342,7 +349,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertFalse(user_load($account->uid), t('User is not found in the database.'));
$this->assertFalse(user_load($account->uid, TRUE), t('User is not found in the database.'));
// Confirm that user's content has been attributed to anonymous user.
$test_node = node_load($node->nid, NULL, TRUE);
......@@ -366,7 +373,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid);
$account = user_load($account->uid, TRUE);
// Create a simple node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
......@@ -398,7 +405,7 @@ class UserCancelTestCase extends DrupalWebTestCase {
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertFalse(user_load($account->uid), t('User is not found in the database.'));
$this->assertFalse(user_load($account->uid, TRUE), t('User is not found in the database.'));
// Confirm that user's content has been deleted.
$this->assertFalse(node_load($node->nid, NULL, TRUE), t('Node of the user has been deleted.'));
......@@ -472,13 +479,14 @@ class UserCancelTestCase extends DrupalWebTestCase {
$status = TRUE;
foreach ($users as $account) {
$status = $status && (strpos($this->content, t('%name has been deleted.', array('%name' => $account->name))) !== FALSE);
$status = $status && !user_load($account->uid);
$status = $status && !user_load($account->uid, TRUE);
}
$this->assertTrue($status, t('Users deleted and not found in the database.'));
// Ensure that admin account was not cancelled.
$this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
$this->assertTrue(user_load(array('uid' => $admin_user->uid, 'status' => 1)), t('Administrative user is found in the database and enabled.'));
$admin_user = user_load($admin_user->uid);
$this->assertTrue($admin_user->status == 1, t('Administrative user is found in the database and enabled.'));
}
}
......@@ -791,13 +799,13 @@ class UserAdminTestCase extends DrupalWebTestCase {
$this->assertText($user_c->name, t('Found user C on filtered by perm admin users page'));
// Test blocking of a user.
$account = user_load(array('name' => $user_b->name));
$account = user_load($user_b->uid);
$this->assertEqual($account->status, 1, 'User B not blocked');
$edit = array();
$edit['operation'] = 'block';
$edit['accounts['. $account->uid .']'] = TRUE;
$this->drupalPost('admin/user/user', $edit, t('Update'));
$account = user_load(array('name' => $user_b->name));
$account = user_load($user_b->uid, TRUE);
$this->assertEqual($account->status, 0, 'User B blocked');
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment