Commit 3b3f0966 authored by Dries's avatar Dries

- Patch #721436 by catch, chx, moshe weitzman: remove magical fairy saving of...

- Patch #721436 by catch, chx, moshe weitzman: remove magical fairy saving of cruft from user_save().
parent 883a8701
...@@ -167,13 +167,6 @@ function contact_load($cid) { ...@@ -167,13 +167,6 @@ function contact_load($cid) {
->fetchAssoc(); ->fetchAssoc();
} }
/**
* Implements hook_user_insert().
*/
function contact_user_insert(&$edit, $account, $category) {
$edit['contact'] = variable_get('contact_default_status', 1);
}
/** /**
* Implements hook_mail(). * Implements hook_mail().
*/ */
...@@ -240,6 +233,13 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) { ...@@ -240,6 +233,13 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) {
} }
} }
/**
* Implements hook_user_presave().
*/
function contact_user_presave(&$edit, $account, $category) {
$edit['data']['contact'] = isset($edit['contact']) ? $edit['contact'] : variable_get('contact_default_status', 1);
}
/** /**
* Implement of hook_form_FORM_ID_alter(). * Implement of hook_form_FORM_ID_alter().
* *
......
...@@ -198,10 +198,8 @@ function hook_user_categories() { ...@@ -198,10 +198,8 @@ function hook_user_categories() {
* *
* This hook is primarily intended for modules that want to store properties in * This hook is primarily intended for modules that want to store properties in
* the serialized {users}.data column, which is automatically loaded whenever a * the serialized {users}.data column, which is automatically loaded whenever a
* user account object is loaded, and the module needs to prepare the stored * user account object is loaded, modules may add to $edit['data'] in order
* data in some way. * to have their data serialized on save.
* The module should save its custom additions to the user object into the
* database and set the saved fields to NULL in $edit.
* *
* @param &$edit * @param &$edit
* The array of form values submitted by the user. * The array of form values submitted by the user.
...@@ -216,9 +214,7 @@ function hook_user_categories() { ...@@ -216,9 +214,7 @@ function hook_user_categories() {
function hook_user_presave(&$edit, $account, $category) { function hook_user_presave(&$edit, $account, $category) {
// Make sure that our form value 'mymodule_foo' is stored as 'mymodule_bar'. // Make sure that our form value 'mymodule_foo' is stored as 'mymodule_bar'.
if (isset($edit['mymodule_foo'])) { if (isset($edit['mymodule_foo'])) {
$edit['mymodule_bar'] = $edit['mymodule_foo']; $edit['data']['my_module_foo'] = $edit['my_module_foo'];
// Inform user_save() to ignore the value of our property.
$edit['mymodule_foo'] = NULL;
} }
} }
...@@ -226,7 +222,7 @@ function hook_user_presave(&$edit, $account, $category) { ...@@ -226,7 +222,7 @@ function hook_user_presave(&$edit, $account, $category) {
* A user account was created. * A user account was created.
* *
* The module should save its custom additions to the user object into the * The module should save its custom additions to the user object into the
* database and set the saved fields to NULL in $edit. * database.
* *
* @param &$edit * @param &$edit
* The array of form values submitted by the user. * The array of form values submitted by the user.
...@@ -245,8 +241,6 @@ function hook_user_insert(&$edit, $account, $category) { ...@@ -245,8 +241,6 @@ function hook_user_insert(&$edit, $account, $category) {
'uid' => $account->uid, 'uid' => $account->uid,
)) ))
->execute(); ->execute();
// Inform user_save() to ignore the value of our property.
$edit['myfield'] = NULL;
} }
/** /**
......
...@@ -345,7 +345,6 @@ function user_save($account, $edit = array(), $category = 'account') { ...@@ -345,7 +345,6 @@ function user_save($account, $edit = array(), $category = 'account') {
$transaction = db_transaction(); $transaction = db_transaction();
try { try {
$table = drupal_get_schema('users'); $table = drupal_get_schema('users');
$user_fields = $table['fields'];
if (!empty($edit['pass'])) { if (!empty($edit['pass'])) {
// Allow alternate password hashing schemes. // Allow alternate password hashing schemes.
...@@ -369,37 +368,22 @@ function user_save($account, $edit = array(), $category = 'account') { ...@@ -369,37 +368,22 @@ function user_save($account, $edit = array(), $category = 'account') {
if (!isset($account->is_new)) { if (!isset($account->is_new)) {
$account->is_new = empty($account->uid); $account->is_new = empty($account->uid);
} }
// Prepopulate $edit['data'] with the current value of $account->data.
// Modules can add to or remove from this array in hook_user_presave().
if (!empty($account->data)) {
$data = unserialize($account->data);
foreach ($data as $key => $value) {
$edit['data'][$key] = $value;
}
}
user_module_invoke('presave', $edit, $account, $category); user_module_invoke('presave', $edit, $account, $category);
if (is_object($account) && !$account->is_new) { if (is_object($account) && !$account->is_new) {
$data = unserialize(db_query('SELECT data FROM {users} WHERE uid = :uid', array(':uid' => $account->uid))->fetchField());
// Consider users edited by an administrator as logged in, if they haven't // Consider users edited by an administrator as logged in, if they haven't
// already, so anonymous users can view the profile (if allowed). // already, so anonymous users can view the profile (if allowed).
if (empty($edit['access']) && empty($account->access) && user_access('administer users')) { if (empty($edit['access']) && empty($account->access) && user_access('administer users')) {
$edit['access'] = REQUEST_TIME; $edit['access'] = REQUEST_TIME;
} }
// Find the fields attached to this user.
$field_names = array();
list(, , $bundle) = entity_extract_ids('user', (object) $edit);
foreach (field_info_instances('user', $bundle) as $instance) {
$field = field_info_field_by_id($instance['field_id']);
$field_names[] = $field['field_name'];
}
foreach ($edit as $key => $value) {
// Form fields that don't pertain to the users, user_roles, or
// Field API are automatically serialized into the users.data
// column.
if (!in_array($key, array('roles', 'is_new', 'current_pass_required_values', 'current_pass')) && empty($user_fields[$key]) && empty($field_names[$key])) {
if ($value === NULL) {
unset($data[$key]);
}
else {
$data[$key] = $value;
}
}
}
// Process picture uploads. // Process picture uploads.
if (!empty($edit['picture']->fid)) { if (!empty($edit['picture']->fid)) {
...@@ -422,7 +406,6 @@ function user_save($account, $edit = array(), $category = 'account') { ...@@ -422,7 +406,6 @@ function user_save($account, $edit = array(), $category = 'account') {
} }
$edit['picture'] = empty($edit['picture']->fid) ? 0 : $edit['picture']->fid; $edit['picture'] = empty($edit['picture']->fid) ? 0 : $edit['picture']->fid;
$edit['data'] = $data;
// Do not allow 'uid' to be changed. // Do not allow 'uid' to be changed.
$edit['uid'] = $account->uid; $edit['uid'] = $account->uid;
// Save changes to the user table. // Save changes to the user table.
...@@ -522,22 +505,6 @@ function user_save($account, $edit = array(), $category = 'account') { ...@@ -522,22 +505,6 @@ function user_save($account, $edit = array(), $category = 'account') {
user_module_invoke('insert', $edit, $user, $category); user_module_invoke('insert', $edit, $user, $category);
entity_invoke('insert', 'user', $user); entity_invoke('insert', 'user', $user);
// Note, we wait with saving the data column to prevent module-handled
// fields from being saved there.
$data = array();
foreach ($edit as $key => $value) {
// Form fields that don't pertain to the users, user_roles, or
// Field API are automatically serialized into the user.data
// column.
if ((!in_array($key, array('roles', 'is_new'))) && (empty($user_fields[$key]) && empty($field_form[$key])) && ($value !== NULL)) {
$data[$key] = $value;
}
}
if (!empty($data)) {
$data_array = array('uid' => $user->uid, 'data' => $data);
drupal_write_record('users', $data_array, 'uid');
}
// Save user roles. // Save user roles.
if (isset($edit['roles']) && is_array($edit['roles'])) { if (isset($edit['roles']) && is_array($edit['roles'])) {
$query = db_insert('users_roles')->fields(array('uid', 'rid')); $query = db_insert('users_roles')->fields(array('uid', 'rid'));
...@@ -1156,15 +1123,18 @@ function user_user_presave(&$edit, $account, $category) { ...@@ -1156,15 +1123,18 @@ function user_user_presave(&$edit, $account, $category) {
elseif (!empty($edit['picture_delete'])) { elseif (!empty($edit['picture_delete'])) {
$edit['picture'] = NULL; $edit['picture'] = NULL;
} }
// Remove these values so they don't end up serialized in the data field.
$edit['picture_upload'] = NULL;
$edit['picture_delete'] = NULL;
// Prepare user roles. // Prepare user roles.
if (isset($edit['roles'])) { if (isset($edit['roles'])) {
$edit['roles'] = array_filter($edit['roles']); $edit['roles'] = array_filter($edit['roles']);
} }
} }
// Move account cancellation information into $user->data.
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($edit[$key])) {
$edit['data'][$key] = $edit[$key];
}
}
} }
/** /**
......
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