drupal/core/modules/user/user.install

604 lines
18 KiB
Plaintext

<?php
/**
* @file
* Install, update and uninstall functions for the user module.
*/
/**
* Implements hook_schema().
*/
function user_schema() {
// The table name here is plural, despite Drupal table naming standards,
// because "user" is a reserved word in many databases.
$schema['users'] = array(
'description' => 'Stores user data.',
'fields' => array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique user ID.',
'default' => 0,
),
'uuid' => array(
'description' => 'Unique Key: Universally unique identifier for this entity.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'name' => array(
'type' => 'varchar',
'length' => 60,
'not null' => TRUE,
'default' => '',
'description' => 'Unique user name.',
),
'langcode' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => "The {language}.langcode of the user's profile.",
),
'pass' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => "User's password (hashed).",
),
'mail' => array(
'type' => 'varchar',
'length' => 254,
'not null' => FALSE,
'default' => '',
'description' => "User's e-mail address.",
),
'theme' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "User's default theme.",
),
'signature' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "User's signature.",
),
'signature_format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'description' => 'The {filter_format}.format of the signature.',
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp for when user was created.',
),
'access' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp for previous time user accessed the site.',
),
'login' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => "Timestamp for user's last login.",
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'Whether the user is active(1) or blocked(0).',
),
'timezone' => array(
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'description' => "User's time zone.",
),
'preferred_langcode' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => 'The {language}.langcode that the user prefers for receiving emails and viewing the site.',
),
'preferred_admin_langcode' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => 'The {language}.langcode that the user prefers for viewing administration pages.',
),
'picture' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => "Foreign key: {file_managed}.fid of user's picture.",
),
'init' => array(
'type' => 'varchar',
'length' => 254,
'not null' => FALSE,
'default' => '',
'description' => 'E-mail address used for initial account creation.',
),
'data' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'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.',
),
),
'indexes' => array(
'access' => array('access'),
'created' => array('created'),
'mail' => array('mail'),
'picture' => array('picture'),
),
'unique keys' => array(
'uuid' => array('uuid'),
'name' => array('name'),
),
'primary key' => array('uid'),
'foreign keys' => array(
'signature_format' => array(
'table' => 'filter_format',
'columns' => array('signature_format' => 'format'),
),
),
);
$schema['authmap'] = array(
'description' => 'Stores distributed authentication mapping.',
'fields' => array(
'aid' => array(
'description' => 'Primary Key: Unique authmap ID.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "User's {users}.uid.",
),
'authname' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Unique authentication name.',
),
'module' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Module which is controlling the authentication.',
),
),
'unique keys' => array(
'authname' => array('authname'),
),
'primary key' => array('aid'),
'foreign keys' => array(
'user' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
$schema['role'] = array(
'description' => 'Stores user roles.',
'fields' => array(
'rid' => array(
'type' => 'varchar',
// The role ID is often used as part of a compound index; at least MySQL
// has a maximum index length of 1000 characters (333 on utf8), so we
// limit the maximum length.
'length' => 64,
'not null' => TRUE,
'description' => 'Primary Key: Unique role ID.',
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Role label.',
'translatable' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The weight of this role in listings and the user interface.',
),
),
'primary key' => array('rid'),
'indexes' => array(
'name_weight' => array('name', 'weight'),
),
);
$schema['role_permission'] = array(
'description' => 'Stores the permissions assigned to user roles.',
'fields' => array(
'rid' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Foreign Key: {role}.rid.',
),
'permission' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'A single permission granted to the role identified by rid.',
),
'module' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "The module declaring the permission.",
),
),
'primary key' => array('rid', 'permission'),
'indexes' => array(
'permission' => array('permission'),
),
'foreign keys' => array(
'role' => array(
'table' => 'role',
'columns' => array('rid' => 'rid'),
),
),
);
$schema['users_roles'] = array(
'description' => 'Maps users to roles.',
'fields' => array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Primary Key: {users}.uid for user.',
),
'rid' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Primary Key: {role}.rid for role.',
),
),
'primary key' => array('uid', 'rid'),
'indexes' => array(
'rid' => array('rid'),
),
'foreign keys' => array(
'user' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
'role' => array(
'table' => 'role',
'columns' => array('rid' => 'rid'),
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function user_install() {
// Insert a row for the anonymous user.
db_insert('users')
->fields(array(
'uid' => 0,
'name' => '',
'mail' => '',
))
->execute();
// We need some placeholders here as name and mail are uniques and data is
// presumed to be a serialized array. This will be changed by the settings
// form in the installer.
db_insert('users')
->fields(array(
'uid' => 1,
'name' => 'placeholder-for-uid-1',
'mail' => 'placeholder-for-uid-1',
'created' => REQUEST_TIME,
'status' => 1,
'data' => NULL,
))
->execute();
// Insert built-in roles.
db_insert('role')
->fields(array('rid', 'name', 'weight'))
->values(array(DRUPAL_ANONYMOUS_RID, 'Anonymous user', 0))
->values(array(DRUPAL_AUTHENTICATED_RID, 'Authenticated user', 1))
->execute();
}
/**
* @addtogroup updates-7.x-to-8.x
* @{
*/
/**
* The 'Member for' extra field has moved one level up in the array.
*/
function user_update_8000() {
$settings = field_bundle_settings('user', 'user');
if (isset($settings['extra_fields']['display']['summary'])) {
$settings['extra_fields']['display']['member_for'] = $settings['extra_fields']['display']['summary'];
unset($settings['extra_fields']['display']['summary']);
field_bundle_settings('user', 'user', $settings);
}
}
/**
* Splits {users}.language field to langcode and preferred_langcode.
*
* @see http://drupal.org/node/1454538
*/
function user_update_8001() {
// The former language field is the language preference of the user. Rename
// this to preferred_langcode in order to distinguish it from the langcode
// field common to all entity types, used for identifying the language of the
// entity itself.
$preferred_langcode_field = array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => 'The {language}.langcode that the user prefers for receiving emails and viewing the site.',
);
db_change_field('users', 'language', 'preferred_langcode', $preferred_langcode_field);
// Add the langcode field.
$langcode_field = array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => "The {language}.langcode of the user's profile.",
);
db_add_field('users', 'langcode', $langcode_field);
// Since distinguishing the language of the user entity from the user's
// preferred language is a new feature in Drupal 8, assume that for updated
// sites, existing user entities are in the user's preferred language.
db_update('users')->expression('langcode', 'preferred_langcode')->execute();
}
/**
* Replace serial role IDs with machine name strings.
*/
function user_update_8002() {
// Change serial rid columns into strings.
$column = array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Primary Key: Unique role ID.',
);
db_change_field('role', 'rid', 'rid', $column);
$column['description'] = 'Foreign Key: {role}.rid.';
db_change_field('role_permission', 'rid', 'rid', $column);
$column['description'] = 'Primary Key: {role}.rid for role.';
db_change_field('users_roles', 'rid', 'rid', $column);
// Enlarge the role name (label) column.
$column = array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Role label.',
'translatable' => TRUE,
);
db_change_field('role', 'name', 'name', $column);
// Remove unique index.
db_drop_unique_key('role', 'name');
// Rename the built-in serial role IDs into the hardcoded machine names.
db_update('role')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', 1)
->execute();
db_update('role')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', 2)
->execute();
db_update('role_permission')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', 1)
->execute();
db_update('role_permission')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', 2)
->execute();
db_update('users_roles')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', 1)
->execute();
db_update('users_roles')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', 2)
->execute();
}
/**
* Create a UUID column for users.
*/
function user_update_8003() {
$spec = array(
'description' => 'Unique Key: Universally unique identifier for this entity.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
);
$keys = array(
'unique keys' => array(
'uuid' => array('uuid'),
),
);
// Account for sites having the contributed UUID module installed.
if (db_field_exists('users', 'uuid')) {
db_change_field('users', 'uuid', 'uuid', $spec, $keys);
}
else {
db_add_field('users', 'uuid', $spec, $keys);
}
}
/**
* Moves account settings from variable to config.
*
* @ingroup config_upgrade
*/
function user_update_8004() {
update_variables_to_config('user.settings', array(
'anonymous' => 'anonymous',
'user_admin_role' => 'admin_role',
'user_register' => 'register',
'user_signatures' => 'signatures',
'user_mail_status_activated_notify' => 'notify.status_activated',
'user_mail_status_blocked_notify' => 'notify.status_blocked',
'user_mail_status_cancelled_notify' => 'notify.status_cancelled',
'user_email_verification' => 'verify_mail',
));
// Convert the user.settings:register numeric value to text value.
$map = array(
'0' => 'admin_only',
'1' => 'visitors',
'2' => 'visitors_admin_approval',
);
$config = config('user.settings');
$user_register = $config->get('register');
if (is_numeric($user_register) && isset($map[$user_register])) {
$config->set('register', $map[$user_register])->save();
}
}
/**
* Creates a preferred_admin_langcode column.
*/
function user_update_8005() {
$spec = array(
'description' => 'The {language}.langcode that the user prefers for viewing administration pages.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
);
db_add_field('users', 'preferred_admin_langcode', $spec);
}
/**
* Moves user mail settings from variable to config.
*
* @ingroup config_upgrade
*/
function user_update_8006() {
update_variables_to_config('user.mail', array(
'register_admin_created_subject' => 'register_admin_created.subject',
'register_admin_created_body' => 'register_admin_created.body',
'register_pending_approval_subject' => 'register_pending_approval.subject',
'register_pending_approval_body' => 'register_pending_approval.body',
'register_no_approval_required_subject' => 'register_no_approval_required.subject',
'register_no_approval_required_body' => 'register_no_approval_required.body',
'password_reset_subject' => 'password_reset.subject',
'password_reset_body' => 'password_reset.body',
'status_activated_subject' => 'status_activated.subject',
'status_activated_body' => 'status_activated.body',
'status_blocked_subject' => 'status_blocked.subject',
'status_blocked_body' => 'status_blocked.body',
'cancel_confirm_subject' => 'cancel_confirm.subject',
'cancel_confirm_body' => 'cancel_confirm.body',
'status_canceled_subject' => 'status_canceled.subject',
'status_canceled_body' => 'status_canceled.body',
));
}
/**
* Moves login flood settings from variable to config.
*
* @ingroup config_upgrade
*/
function user_update_8007() {
update_variables_to_config('user.flood', array(
'user_failed_login_identifier_uid_only' => 'uid_only',
'user_failed_login_ip_limit' => 'ip_limit',
'user_failed_login_ip_window' => 'ip_window',
'user_failed_login_user_limit' => 'user_limit',
'user_failed_login_user_window' => 'user_window',
));
}
/**
* Make *id fields unsigned.
*/
function user_update_8008() {
db_change_field('authmap', 'uid', 'uid',
array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "User's {users}.uid.",
)
);
}
/**
* Generate an UUID for all users.
*/
function user_update_8009(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
// The first user id is 0, so it needs to start with -1.
$sandbox['last'] = -1;
$sandbox['max'] = db_query('SELECT COUNT(uid) FROM {users} WHERE uuid IS NULL')->fetchField();
}
$uids = db_query_range('SELECT uid FROM {users} WHERE uid > :uid AND uuid IS NULL ORDER BY uid ASC', 0, 10, array(':uid' => $sandbox['last']))->fetchCol();
update_add_uuids($sandbox, 'users', 'uid', $uids);
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}
/**
* @} End of "addtogroup updates-7.x-to-8.x".
*/