diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 698ae80..f877411 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -599,7 +599,7 @@ function block_form_user_profile_form_alter(&$form, &$form_state) { $account = $form_state['controller']->getEntity($form_state); $rids = array_keys($account->roles); $result = db_query("SELECT DISTINCT b.* FROM {block} b LEFT JOIN {block_role} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom <> 0 AND (r.rid IN (:rids) OR r.rid IS NULL) ORDER BY b.weight, b.module", array(':rids' => $rids)); - $account_data = user_data_get('block', $account->uid, 'block'); + $account_data = drupal_container()->get('user.data')->get('block', $account->uid, 'block'); $blocks = array(); foreach ($result as $block) { @@ -644,7 +644,7 @@ function block_field_extra_fields() { */ function block_user_update($account) { if (isset($account->block)) { - user_data_set('block', $account->uid, 'block', $account->block); + drupal_container()->get('user.data')->set('block', $account->uid, 'block', $account->block); } } @@ -807,7 +807,7 @@ function block_block_list_alter(&$blocks) { } if ($user->uid) { - $user_data = user_data_get('block', $user->uid, 'block'); + $user_data = drupal_container()->get('user.data')->get('block', $user->uid, 'block'); } foreach ($blocks as $key => $block) { diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index 5b191ec..d74a66a 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -144,7 +144,7 @@ function _contact_personal_tab_access($account) { // If the requested user has disabled their contact form, do not allow users // to contact them. - $account_data = user_data_get('contact', $account->uid, 'enabled'); + $account_data = drupal_container()->get('user.data')->get('contact', $account->uid, 'enabled'); if (isset($account_data) && empty($account_data)) { return FALSE; } @@ -293,7 +293,7 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) { '#collapsible' => TRUE, ); $account = $form_state['controller']->getEntity($form_state); - $account_data = user_data_get('contact', $account->uid, 'enabled'); + $account_data = drupal_container()->get('user.data')->get('contact', $account->uid, 'enabled'); $form['contact']['contact'] = array( '#type' => 'checkbox', '#title' => t('Personal contact form'), @@ -307,7 +307,7 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) { */ function contact_user_update($account) { if (isset($account->contact)) { - user_data_set('contact', $account->uid, 'enabled', (int) $account->contact); + drupal_container()->get('user.data')->set('contact', $account->uid, 'enabled', (int) $account->contact); } } diff --git a/core/modules/overlay/overlay.module b/core/modules/overlay/overlay.module index 0380849..76d90be 100644 --- a/core/modules/overlay/overlay.module +++ b/core/modules/overlay/overlay.module @@ -87,7 +87,7 @@ function overlay_theme() { function overlay_form_user_profile_form_alter(&$form, &$form_state) { $account = $form_state['controller']->getEntity($form_state); if (user_access('access overlay', $account)) { - $account_data = user_data_get('overlay', $account->uid, 'enabled'); + $account_data = drupal_container()->get('user.data')->get('overlay', $account->uid, 'enabled'); $form['overlay_control'] = array( '#type' => 'fieldset', '#title' => t('Administrative overlay'), @@ -108,7 +108,7 @@ function overlay_form_user_profile_form_alter(&$form, &$form_state) { */ function overlay_user_presave($account) { if (isset($account->overlay)) { - user_data_set('overlay', $account->uid, 'enabled', $account->overlay); + drupal_container()->get('user.data')->set('overlay', $account->uid, 'enabled', $account->overlay); } } @@ -127,7 +127,7 @@ function overlay_init() { // Only act if the user has access to the overlay and a mode was not already // set. Other modules can also enable the overlay directly for other uses. - $user_data = user_data_get('overlay', $user->uid, 'enabled'); + $user_data = drupal_container()->get('user.data')->get('overlay', $user->uid, 'enabled'); $use_overlay = !isset($user_data) || $user_data; if (empty($mode) && user_access('access overlay') && $use_overlay) { $current_path = current_path(); @@ -356,7 +356,7 @@ function overlay_user_dismiss_message() { throw new AccessDeniedHttpException(); } - user_data_set('overlay', $user->uid, 'message_dismissed', 1); + drupal_container()->get('user.data')->set('overlay', $user->uid, 'message_dismissed', 1); drupal_set_message(t('The message has been dismissed. You can change your overlay settings at any time by visiting your profile page.')); // Destination is normally given. Go to the user profile as a fallback. drupal_goto('user/' . $user->uid . '/edit'); @@ -383,7 +383,7 @@ function overlay_disable_message() { return $build; } - $user_data = user_data_get('overlay', $user->uid); + $user_data = drupal_container()->get('user.data')->get('overlay', $user->uid); if (empty($user_data['message_dismissed']) && (!isset($user_data['enabled']) || $user_data['enabled'])) { $build = array( '#theme' => 'overlay_disable_message', diff --git a/core/modules/user/lib/Drupal/user/UserBundle.php b/core/modules/user/lib/Drupal/user/UserBundle.php new file mode 100644 index 0000000..4e356b2 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/UserBundle.php @@ -0,0 +1,27 @@ +register('user.data', 'Drupal\user\UserData') + ->addArgument('database'); + } + +} diff --git a/core/modules/user/lib/Drupal/user/UserData.php b/core/modules/user/lib/Drupal/user/UserData.php new file mode 100644 index 0000000..13855d2 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/UserData.php @@ -0,0 +1,164 @@ +connection = $connection; + } + + /** + * Returns data stored for a user account. + * + * @param string $module + * The name of the module the data is associated with. + * @param integer $uid + * (optional) The user account ID the data is associated with. + * @param string $name + * (optional) The name of the data key. + * + * @return mixed|array + * The requested user account data, depending on the arguments passed: + * - For $module, $name, and $uid, the stored data is returned, or NULL if + * no data was found. + * - For $module and $uid, an associative array is returned that contains + * the stored data name/value pairs. + * - For $module and $name, an associative array is returned whose keys are + * user IDs and whose values contain the stored data. + * - For $module only, an associative array is returned that contains all + * existing data for $module in all user accounts, keyed first by user ID + * and $name second. + */ + public function get($module, $uid = NULL, $name = NULL) { + $query = $this->connection->select('users_data', 'ud') + ->fields('ud') + ->condition('module', $module); + if (isset($uid)) { + $query->condition('uid', $uid); + } + if (isset($name)) { + $query->condition('name', $name); + } + $result = $query->execute(); + // If $module, $uid, and $name was passed, return the value. + if (isset($name) && isset($uid)) { + $result = $result->fetchAllAssoc('uid'); + if (isset($result[$uid])) { + return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value; + } + return NULL; + } + // If $module and $uid was passed, return the name/value pairs. + elseif (isset($uid)) { + $return = array(); + foreach ($result as $record) { + $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value); + } + return $return; + } + // If $module and $name was passed, return the uid/value pairs. + elseif (isset($name)) { + $return = array(); + foreach ($result as $record) { + $return[$record->uid] = ($record->serialized ? unserialize($record->value) : $record->value); + } + return $return; + } + // If only $module was passed, return data keyed by uid and name. + else { + $return = array(); + foreach ($result as $record) { + $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value); + } + return $return; + } + } + + /** + * Stores data for a user account. + * + * @param string $module + * The name of the module the data is associated with. + * @param integer $uid + * The user account ID the data is associated with. + * @param string $name + * The name of the data key. + * @param mixed $value + * The value to store. Non-scalar values are serialized automatically. + * + * @return void + */ + public function set($module, $uid, $name, $value) { + $serialized = 0; + if (!is_scalar($value)) { + $value = serialize($value); + $serialized = 1; + } + $this->connection->merge('users_data') + ->key(array( + 'uid' => $uid, + 'module' => $module, + 'name' => $name, + )) + ->fields(array( + 'value' => $value, + 'serialized' => $serialized, + )) + ->execute(); + } + + /** + * Deletes data stored for a user account. + * + * @param string|array $module + * (optional) The name of the module the data is associated with. Can also + * be an array to delete the data of multiple modules. + * @param integer|array $uid + * (optional) The user account ID the data is associated with. If omitted, + * all data for $module is deleted. Can also be an array of IDs to delete + * the data of multiple user accounts. + * @param string $name + * (optional) The name of the data key. If omitted, all data associated with + * $module and $uid is deleted. + * + * @return void + */ + public function delete($module = NULL, $uid = NULL, $name = NULL) { + $query = $this->connection->delete('users_data'); + if (isset($module)) { + $query->condition('module', $module); + } + if (isset($uid)) { + $query->condition('uid', $uid); + } + if (isset($name)) { + $query->condition('name', $name); + } + $query->execute(); + } + +} diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index 2c92aac..ced445e 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -151,7 +151,7 @@ protected function preSave(EntityInterface $entity) { // Store account cancellation information. foreach (array('user_cancel_method', 'user_cancel_notify') as $key) { if (isset($entity->{$key})) { - user_data_set('user', $entity->id(), substr($key, 5), $entity->{$key}); + drupal_container()->get('user.data')->set('user', $entity->id(), substr($key, 5), $entity->{$key}); } } } @@ -228,11 +228,9 @@ protected function postDelete($entities) { db_delete('users_roles') ->condition('uid', array_keys($entities), 'IN') ->execute(); - db_delete('users_data') - ->condition('uid', array_keys($entities), 'IN') - ->execute(); db_delete('authmap') ->condition('uid', array_keys($entities), 'IN') ->execute(); + drupal_container()->get('user.data')->delete(NULL, array_keys($entities)); } } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 855396a..002301c 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2998,9 +2998,7 @@ function user_modules_uninstalled($modules) { ->condition('module', $modules, 'IN') ->execute(); // Remove any potentially orphan module data stored for users. - db_delete('users_data') - ->condition('module', $modules, 'IN') - ->execute(); + drupal_container()->get('user.data')->delete($modules); } /** @@ -3104,129 +3102,3 @@ function user_library_info() { return $libraries; } - -/** - * Returns data stored for a user account. - * - * @param string $module - * The name of the module the data is associated with. - * @param integer $uid - * (optional) The user account ID the data is associated with. - * @param string $name - * (optional) The name of the data key. - * - * @return mixed|array - * The requested user account data, depending on the arguments passed: - * - For $module, $name, and $uid, the stored data is returned, or NULL if no - * data was found. - * - For $module and $uid, an associative array is returned that contains the - * stored data name/value pairs. - * - For $module and $name, an associative array is returned whose keys are - * user IDs and whose values contain the stored data. - * - For $module only, an associative array is returned that contains all - * existing data for $module in all user accounts, keyed by first by user ID - * and $name second. - */ -function user_data_get($module, $uid = NULL, $name = NULL) { - $query = db_select('users_data', 'ud') - ->fields('ud') - ->condition('module', $module); - if (isset($uid)) { - $query->condition('uid', $uid); - } - if (isset($name)) { - $query->condition('name', $name); - } - $result = $query->execute(); - // If $module, $uid, and $name was passed, return the value. - if (isset($name) && isset($uid)) { - $result = $result->fetchAllAssoc('uid'); - if (isset($result[$uid])) { - return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value; - } - return NULL; - } - // If $module and $uid was passed, return the name/value pairs. - elseif (isset($uid)) { - $return = array(); - foreach ($result as $record) { - $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value); - } - return $return; - } - // If $module and $name was passed, return the uid/value pairs. - elseif (isset($name)) { - $return = array(); - foreach ($result as $record) { - $return[$record->uid] = ($record->serialized ? unserialize($record->value) : $record->value); - } - return $return; - } - // If only $module was passed, return data keyed by uid and name. - else { - $return = array(); - foreach ($result as $record) { - $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value); - } - return $return; - } -} - -/** - * Stores data for a user account. - * - * @param string $module - * The name of the module the data is associated with. - * @param integer $uid - * The user account ID the data is associated with. - * @param string $name - * The name of the data key. - * @param mixed $value - * The value to store. Non-scalar values are serialized automatically. - * - * @return void - */ -function user_data_set($module, $uid, $name, $value) { - $serialized = 0; - if (!is_scalar($value)) { - $value = serialize($value); - $serialized = 1; - } - db_merge('users_data') - ->key(array( - 'uid' => $uid, - 'module' => $module, - 'name' => $name, - )) - ->fields(array( - 'value' => $value, - 'serialized' => $serialized, - )) - ->execute(); -} - -/** - * Deletes data stored for a user account. - * - * @param string $module - * The name of the module the data is associated with. - * @param integer $uid - * (optional) The user account ID the data is associated with. If omitted, all - * data for $module is deleted. - * @param string $name - * (optional) The name of the data key. If omitted, all data associated with - * $module and $uid is deleted. - * - * @return void - */ -function user_data_del($module, $uid = NULL, $name = NULL) { - $query = db_delete('users_data') - ->condition('module', $module); - if (isset($uid)) { - $query->condition('uid', $uid); - } - if (isset($name)) { - $query->condition('name', $name); - } - $query->execute(); -} diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc index 1189f06..d45112d 100644 --- a/core/modules/user/user.pages.inc +++ b/core/modules/user/user.pages.inc @@ -388,7 +388,7 @@ function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') { $current = REQUEST_TIME; // Basic validation of arguments. - $account_data = user_data_get('user', $account->id()); + $account_data = drupal_container()->get('user.data')->get('user', $account->id()); if (isset($account_data['cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) { // Validate expiration and hashed password/login. if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {