2007-10-05 16:07:22 +00:00
<?php
// $Id$
/**
* Implementation of hook_schema().
*/
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,
2008-11-15 13:01:11 +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'),
);
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
),
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'),
);
$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(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique user ID.',
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(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => "Path to the user's uploaded 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'),
);
$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,
2008-11-15 13:01:11 +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'),
),
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) {
$ret = array('#finished' => 0);
// 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'])) {
db_change_field($ret, 'users', 'pass', 'pass', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
$sandbox['user_from'] = 0;
$sandbox['user_count'] = db_result(db_query("SELECT COUNT(uid) FROM {users}"));
}
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;
$result = db_query_range("SELECT uid, pass FROM {users} WHERE uid > 0 ORDER BY uid", $sandbox['user_from'], $count);
while ($account = db_fetch_array($result)) {
$has_rows = TRUE;
$new_hash = user_hash_password($account['pass'], $hash_count_log2);
if ($new_hash) {
// Indicate an updated password.
2008-04-14 17:48:46 +00:00
$new_hash = 'U' . $new_hash;
2008-03-31 20:50:05 +00:00
db_query("UPDATE {users} SET pass = '%s' WHERE uid = %d", $new_hash, $account['uid']);
}
}
$ret['#finished'] = $sandbox['user_from']/$sandbox['user_count'];
$sandbox['user_from'] += $count;
if (!$has_rows) {
$ret['#finished'] = 1;
2008-04-14 17:48:46 +00:00
$ret[] = array('success' => TRUE, 'query' => "UPDATE {users} SET pass = 'U' . user_hash_password(pass) WHERE uid > 0");
2008-03-31 20:50:05 +00:00
}
}
return $ret;
}
2008-04-16 11:26:29 +00:00
/**
* Remove the 'threshold', 'mode' and 'sort' columns from the {user} table.
*
* These fields were previously used to store per-user comment settings.
*/
function user_update_7001() {
$ret = array();
db_drop_field($ret, 'users', 'threshold');
db_drop_field($ret, 'users', 'mode');
db_drop_field($ret, 'users', 'sort');
return $ret;
}
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) {
$ret = array('#finished' => 0);
// Multi-part update.
if (!isset($sandbox['user_from'])) {
db_change_field($ret, 'users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE));
$sandbox['user_from'] = 0;
$sandbox['user_count'] = db_result(db_query("SELECT COUNT(uid) FROM {users}"));
$sandbox['user_not_migrated'] = 0;
}
else {
$timezones = system_time_zones();
// Update this many per page load.
$count = 10000;
$contributed_date_module = db_column_exists('users', 'timezone_name');
$contributed_event_module = db_column_exists('users', 'timezone_id');
$results = db_query_range("SELECT uid FROM {users} ORDER BY uid", array(), $sandbox['user_from'], $count);
foreach ($results as $account) {
$timezone = NULL;
// If the contributed Date module has created a users.timezone_name
// column, use this data to set each user's time zone.
if ($contributed_date_module) {
$date_timezone = db_query("SELECT timezone_name FROM {users} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField();
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 {
$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();
$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) {
db_query("UPDATE {users} SET timezone = :timezone WHERE uid = :uid", array(':timezone' => $timezone, ':uid' => $account->uid));
}
else {
$sandbox['user_not_migrated']++;
db_query("UPDATE {users} SET timezone = NULL WHERE uid = :uid", array(':uid' => $account->uid));
}
$sandbox['user_from']++;
}
$ret['#finished'] = $sandbox['user_from'] / $sandbox['user_count'];
if ($sandbox['user_from'] == $sandbox['user_count']) {
$ret[] = array('success' => TRUE, 'query' => "Migrate user time zones.");
if ($sandbox['user_not_migrated'] > 0) {
variable_set('empty_timezone_message', 1);
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/settings/date-time') . ' to choose whether to remind users at login to set the correct time zone.', 'warning');
}
}
}
return $ret;
}
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.
*/