2007-10-05 16:07:22 +00:00
<?php
// $Id$
2009-05-13 19:42:18 +00:00
/**
* @file
* Install, update and uninstall functions for the user module.
*/
2007-10-05 16:07:22 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_schema().
2007-10-05 16:07:22 +00:00
*/
function user_schema() {
$schema['authmap'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores distributed authentication mapping.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'aid' => array(
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique authmap ID.',
2007-10-10 11:39:35 +00:00
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2009-02-26 07:30:29 +00:00
'description' => "User's {users}.uid.",
2007-10-10 11:39:35 +00:00
),
'authname' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Unique authentication name.',
2007-10-10 11:39:35 +00:00
),
'module' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Module which is controlling the authentication.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
2008-03-15 12:31:29 +00:00
'unique keys' => array(
'authname' => array('authname'),
),
2007-10-05 16:07:22 +00:00
'primary key' => array('aid'),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
'uid' => array('users' => 'uid'),
),
2007-10-05 16:07:22 +00:00
);
2008-05-07 19:34:24 +00:00
$schema['role_permission'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores the permissions assigned to user roles.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'rid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Foreign Key: {role}.rid.',
2007-10-10 11:39:35 +00:00
),
2008-05-07 19:34:24 +00:00
'permission' => array(
'type' => 'varchar',
'length' => 64,
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
2008-05-07 19:34:24 +00:00
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'A single permission granted to the role identified by rid.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
2008-05-07 19:34:24 +00:00
'primary key' => array('rid', 'permission'),
2008-03-15 12:31:29 +00:00
'indexes' => array(
2008-05-07 19:34:24 +00:00
'permission' => array('permission'),
2008-03-15 12:31:29 +00:00
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
'rid' => array('role' => 'rid'),
),
2007-10-05 16:07:22 +00:00
);
$schema['role'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores user roles.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'rid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique role ID.',
2007-10-10 11:39:35 +00:00
),
'name' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Unique role name.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
2008-03-15 12:31:29 +00:00
'unique keys' => array(
'name' => array('name'),
),
2007-10-05 16:07:22 +00:00
'primary key' => array('rid'),
);
2009-02-26 07:30:29 +00:00
$schema['users'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores user data.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'uid' => array(
2009-10-18 06:56:24 +00:00
'type' => 'int',
2007-10-10 11:39:35 +00:00
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique user ID.',
2009-10-18 06:56:24 +00:00
'default' => 0,
2007-10-10 11:39:35 +00:00
),
'name' => array(
'type' => 'varchar',
'length' => 60,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Unique user name.',
2007-10-10 11:39:35 +00:00
),
'pass' => array(
'type' => 'varchar',
2008-03-31 20:50:05 +00:00
'length' => 128,
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "User's password (hashed).",
2007-10-10 11:39:35 +00:00
),
'mail' => array(
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "User's email address.",
2007-10-10 11:39:35 +00:00
),
'theme' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "User's default theme.",
2007-10-10 11:39:35 +00:00
),
'signature' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "User's signature.",
2007-10-10 11:39:35 +00:00
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Timestamp for when user was created.',
2007-10-10 11:39:35 +00:00
),
'access' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Timestamp for previous time user accessed the site.',
2007-10-10 11:39:35 +00:00
),
'login' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => "Timestamp for user's last login.",
2007-10-10 11:39:35 +00:00
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2008-11-15 13:01:11 +00:00
'description' => 'Whether the user is active(1) or blocked(0).',
2007-10-10 11:39:35 +00:00
),
'timezone' => array(
'type' => 'varchar',
2008-11-20 06:56:17 +00:00
'length' => 32,
2007-10-10 11:39:35 +00:00
'not null' => FALSE,
2008-11-20 06:56:17 +00:00
'description' => "User's time zone.",
2007-10-10 11:39:35 +00:00
),
'language' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "User's default language.",
2007-10-10 11:39:35 +00:00
),
'picture' => array(
2009-01-20 03:10:00 +00:00
'type' => 'int',
2007-10-10 11:39:35 +00:00
'not null' => TRUE,
2009-01-20 03:10:00 +00:00
'default' => 0,
2009-08-17 19:14:42 +00:00
'description' => "Foreign key: {file}.fid of user's picture.",
2007-10-10 11:39:35 +00:00
),
'init' => array(
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Email address used for initial account creation.',
2007-10-10 11:39:35 +00:00
),
'data' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
2008-02-18 16:53:37 +00:00
'serialize' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'indexes' => array(
2007-10-10 11:39:35 +00:00
'access' => array('access'),
2008-01-08 07:46:41 +00:00
'created' => array('created'),
'mail' => array('mail'),
2007-10-05 16:07:22 +00:00
),
2007-12-18 12:59:22 +00:00
'unique keys' => array(
'name' => array('name'),
),
2007-10-05 16:07:22 +00:00
'primary key' => array('uid'),
);
2009-02-26 07:30:29 +00:00
$schema['users_roles'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Maps users to roles.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-02-26 07:30:29 +00:00
'description' => 'Primary Key: {users}.uid for user.',
2007-10-10 11:39:35 +00:00
),
'rid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: {role}.rid for role.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'primary key' => array('uid', 'rid'),
2007-12-18 12:59:22 +00:00
'indexes' => array(
'rid' => array('rid'),
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
'uid' => array('users' => 'uid'),
'rid' => array('role' => 'rid'),
),
2007-10-05 16:07:22 +00:00
);
return $schema;
}
2008-03-31 20:50:05 +00:00
/**
* @defgroup user-updates-6.x-to-7.x User updates from 6.x to 7.x
* @{
*/
/**
* Increase the length of the password field to accommodate better hashes.
*
* Also re-hashes all current passwords to improve security. This may be a
* lengthy process, and is performed batch-wise.
*/
function user_update_7000(&$sandbox) {
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = 0;
2008-03-31 20:50:05 +00:00
// Lower than DRUPAL_HASH_COUNT to make the update run at a reasonable speed.
$hash_count_log2 = 11;
// Multi-part update.
if (!isset($sandbox['user_from'])) {
2009-09-29 15:13:57 +00:00
db_change_field('users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
2008-03-31 20:50:05 +00:00
$sandbox['user_from'] = 0;
2009-04-13 12:14:57 +00:00
$sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
2008-03-31 20:50:05 +00:00
}
else {
2008-09-20 20:22:25 +00:00
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
2008-03-31 20:50:05 +00:00
// Hash again all current hashed passwords.
$has_rows = FALSE;
// Update this many per page load.
$count = 1000;
2009-09-18 00:04:24 +00:00
$result = db_query_range("SELECT uid, pass FROM {users} WHERE uid > 0 ORDER BY uid", $sandbox['user_from'], $count);
2009-04-13 12:14:57 +00:00
foreach ($result as $account) {
2009-03-25 16:31:18 +00:00
$has_rows = TRUE;
2009-04-13 12:14:57 +00:00
$new_hash = user_hash_password($account->pass, $hash_count_log2);
2009-03-25 16:31:18 +00:00
if ($new_hash) {
// Indicate an updated password.
$new_hash = 'U' . $new_hash;
2009-04-30 16:10:10 +00:00
db_update('users')
->fields(array('pass' => $new_hash))
->condition('uid', $account->uid)
->execute();
2009-03-25 16:31:18 +00:00
}
2008-03-31 20:50:05 +00:00
}
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = $sandbox['user_from']/$sandbox['user_count'];
2008-03-31 20:50:05 +00:00
$sandbox['user_from'] += $count;
if (!$has_rows) {
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = 1;
return t('User passwords rehashed to improve security');
2008-03-31 20:50:05 +00:00
}
}
}
2008-04-16 11:26:29 +00:00
/**
2009-02-26 07:30:29 +00:00
* Remove the 'threshold', 'mode' and 'sort' columns from the {users} table.
2008-04-16 11:26:29 +00:00
*
* These fields were previously used to store per-user comment settings.
*/
function user_update_7001() {
2009-09-29 15:13:57 +00:00
db_drop_field('users', 'threshold');
db_drop_field('users', 'mode');
db_drop_field('users', 'sort');
2008-04-16 11:26:29 +00:00
}
2008-11-20 06:56:17 +00:00
/**
* Convert user time zones from time zone offsets to time zone names.
*/
function user_update_7002(&$sandbox) {
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = 0;
2008-11-20 06:56:17 +00:00
// Multi-part update.
if (!isset($sandbox['user_from'])) {
2009-09-29 15:13:57 +00:00
db_change_field('users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE));
2008-11-20 06:56:17 +00:00
$sandbox['user_from'] = 0;
2009-04-13 12:14:57 +00:00
$sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField();
2008-11-20 06:56:17 +00:00
$sandbox['user_not_migrated'] = 0;
}
else {
$timezones = system_time_zones();
// Update this many per page load.
$count = 10000;
2009-02-26 07:30:29 +00:00
$contributed_date_module = db_column_exists('users', 'timezone_name');
$contributed_event_module = db_column_exists('users', 'timezone_id');
2008-11-20 06:56:17 +00:00
2009-09-18 00:04:24 +00:00
$results = db_query_range("SELECT uid FROM {users} ORDER BY uid", $sandbox['user_from'], $count);
2008-11-20 06:56:17 +00:00
foreach ($results as $account) {
$timezone = NULL;
2009-02-26 07:30:29 +00:00
// If the contributed Date module has created a users.timezone_name
2008-11-20 06:56:17 +00:00
// column, use this data to set each user's time zone.
if ($contributed_date_module) {
2009-02-26 07:30:29 +00:00
$date_timezone = db_query("SELECT timezone_name FROM {users} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField();
2008-11-20 06:56:17 +00:00
if (isset($timezones[$date_timezone])) {
$timezone = $date_timezone;
}
}
// If the contributed Event module has stored user time zone information
// use that information to update the user accounts.
if (!$timezone && $contributed_event_module) {
try {
2009-02-26 07:30:29 +00:00
$event_timezone = db_query("SELECT t.name FROM {users} u LEFT JOIN {event_timezones} t ON u.timezone_id = t.timezone WHERE u.uid = :uid", array(':uid' => $account->uid))->fetchField();
2008-11-20 06:56:17 +00:00
$event_timezone = str_replace(' ', '_', $event_timezone);
if (isset($timezones[$event_timezone])) {
$timezone = $event_timezone;
}
}
catch (PDOException $e) {
// Ignore error if event_timezones table does not exist or unexpected
// schema found.
}
}
if ($timezone) {
2009-12-08 06:52:38 +00:00
db_update('users')
->fields(array('timezone' => $timezone))
->condition('uid', $account->uid)
->execute();
2008-11-20 06:56:17 +00:00
}
else {
$sandbox['user_not_migrated']++;
2009-09-29 15:13:57 +00:00
db_update('users')
2009-12-08 06:52:38 +00:00
->fields(array('timezone' => NULL))
2009-09-29 15:13:57 +00:00
->condition('uid', $account->uid)
->execute();
2008-11-20 06:56:17 +00:00
}
$sandbox['user_from']++;
}
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = $sandbox['user_from'] / $sandbox['user_count'];
2008-11-20 06:56:17 +00:00
if ($sandbox['user_from'] == $sandbox['user_count']) {
if ($sandbox['user_not_migrated'] > 0) {
variable_set('empty_timezone_message', 1);
2009-08-21 14:28:52 +00:00
drupal_set_message('Some user time zones have been emptied and need to be set to the correct values. Use the new ' . l('time zone options', 'admin/config/regional/settings') . ' to choose whether to remind users at login to set the correct time zone.', 'warning');
2008-11-20 06:56:17 +00:00
}
2009-09-29 15:13:57 +00:00
return t('Migrated user time zones');
2008-11-20 06:56:17 +00:00
}
}
}
2009-01-08 08:42:13 +00:00
/**
* Update user settings for cancelling user accounts.
*
* Prior to 7.x, users were not able to cancel their accounts. When
* administrators deleted an account, all contents were assigned to uid 0,
* which is the same as the 'user_cancel_reassign' method now.
*/
function user_update_7003() {
// Set the default account cancellation method.
variable_set('user_cancel_method', 'user_cancel_reassign');
// Re-assign notification setting.
if ($setting = variable_get('user_mail_status_deleted_notify', FALSE)) {
variable_set('user_mail_status_canceled_notify', $setting);
variable_del('user_mail_status_deleted_notify');
}
// Re-assign "Account deleted" mail strings to "Account canceled" mail.
if ($setting = variable_get('user_mail_status_deleted_subject', FALSE)) {
variable_set('user_mail_status_canceled_subject', $setting);
variable_del('user_mail_status_deleted_subject');
}
if ($setting = variable_get('user_mail_status_deleted_body', FALSE)) {
variable_set('user_mail_status_canceled_body', $setting);
variable_del('user_mail_status_deleted_body');
}
}
2009-01-20 03:10:00 +00:00
/**
2009-08-17 19:14:42 +00:00
* Add the user's pictures to the {file} table and make them managed files.
2009-01-20 03:10:00 +00:00
*/
function user_update_7004(&$sandbox) {
$picture_field = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
2009-08-17 19:14:42 +00:00
'description' => t("Foriegn key: {file}.fid of user's picture."),
2009-01-20 03:10:00 +00:00
);
if (!isset($sandbox['progress'])) {
// Check that the field hasn't been updated in an aborted run of this
// update.
2009-02-26 07:30:29 +00:00
if (!db_column_exists('users', 'picture_fid')) {
2009-01-20 03:10:00 +00:00
// Add a new field for the fid.
2009-09-29 15:13:57 +00:00
db_add_field('users', 'picture_fid', $picture_field);
2009-01-20 03:10:00 +00:00
}
// Initialize batch update information.
$sandbox['progress'] = 0;
$sandbox['last_user_processed'] = -1;
2009-08-17 19:14:42 +00:00
$sandbox['max'] = db_query("SELECT COUNT(*) FROM {user} WHERE picture <> ''")->fetchField();
2009-01-20 03:10:00 +00:00
}
2009-08-17 19:14:42 +00:00
// As a batch operation move the photos into the {file} table and update the
2009-02-26 07:30:29 +00:00
// {users} records.
2009-01-20 03:10:00 +00:00
$limit = 500;
2009-09-18 00:04:24 +00:00
$result = db_query_range("SELECT uid, picture FROM {user} WHERE picture <> '' AND uid > :uid ORDER BY uid", 0, $limit, array(':uid' => $sandbox['last_user_processed']));
2009-01-20 03:10:00 +00:00
foreach ($result as $user) {
// Don't bother adding files that don't exist.
if (!file_exists($user->picture)) {
continue;
}
// Check if the file already exists.
2009-08-17 19:14:42 +00:00
$files = file_load_multiple(array(), array('uri' => $user->picture));
2009-01-20 03:10:00 +00:00
if (count($files)) {
$file = reset($files);
}
else {
// Create a file object.
$file = new stdClass();
2009-08-17 19:14:42 +00:00
$file->uri = $user->picture;
$file->filename = basename($file->uri);
$file->filemime = file_get_mimetype($file->uri);
2009-01-20 03:10:00 +00:00
$file->uid = $user->uid;
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
}
2009-02-26 07:30:29 +00:00
db_update('users')
2009-01-20 03:10:00 +00:00
->fields(array('picture_fid' => $file->fid))
->condition('uid', $user->uid)
->execute();
// Update our progress information for the batch update.
$sandbox['progress']++;
$sandbox['last_user_processed'] = $user->uid;
}
// Indicate our current progress to the batch update system. If there's no
// max value then there's nothing to update and we're finished.
2009-09-29 15:13:57 +00:00
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
2009-01-20 03:10:00 +00:00
// When we're finished, drop the old picture field and rename the new one to
// replace it.
2009-09-29 15:13:57 +00:00
if (isset($sandbox['#finished']) && $sandbox['#finished'] == 1) {
db_drop_field('user', 'picture');
db_change_field('user', 'picture_fid', 'picture', $picture_field);
2009-01-20 03:10:00 +00:00
}
}
2008-03-31 20:50:05 +00:00
/**
* @} End of "defgroup user-updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/