security_questions.install in Security Questions 6.2
Same filename and directory in other branches
Install file for security_questions.
File
security_questions.installView source
<?php
/**
* @file
* Install file for security_questions.
*/
/**
* Implements hook_schema().
*/
function security_questions_schema() {
$schema = array();
$schema['security_questions_answers'] = array(
'description' => 'Contains users security question answers.',
'fields' => array(
'uid' => array(
'description' => 'The user ID.',
'type' => 'int',
'not null' => TRUE,
),
'sqid' => array(
'description' => 'The security question ID',
'type' => 'int',
),
'answer' => array(
'description' => 'The answer to the users question',
'type' => 'varchar',
'length' => '100',
),
),
'primary key' => array(
'uid',
'sqid',
),
);
$schema['security_questions'] = array(
'description' => 'Contains possible security questions',
'fields' => array(
'sqid' => array(
'description' => 'The security question ID',
'type' => 'serial',
),
'question' => array(
'description' => 'The text of the question',
'type' => 'varchar',
'length' => '500',
),
'uid' => array(
'description' => '0 for questions available system-wide, or the owning uid for custom per-user questions.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'machine_name' => array(
'description' => 'An optional machine-readable name for this question, to support exportable configuration.',
'type' => 'varchar',
'length' => 255,
),
),
'primary key' => array(
'sqid',
),
'indexes' => array(
'uid' => array(
'uid',
),
),
);
$schema['security_questions_incorrect'] = array(
'description' => 'Tracks incorrect answer attempts by IP.',
'fields' => array(
'aid' => array(
'description' => 'Unique attempt ID.',
'type' => 'serial',
'not null' => TRUE,
),
'sqid' => array(
'description' => 'The security question ID.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'uid' => array(
'description' => 'The user ID.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'The IP address of the visitor that attempted to answer the question as the user.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'timestamp' => array(
'description' => 'Timestamp of the failed attempt.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'expiration' => array(
'description' => 'Expiration timestamp. Expired attempts are purged on cron run.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'aid',
),
'indexes' => array(
'uid_ip' => array(
'uid',
'ip',
),
'expire' => array(
'expiration',
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function security_questions_install() {
// Create tables
drupal_install_schema('security_questions');
// Also, set up default questions to demonstrate usage.
// We can't use security_questions_add_question() here, as we do in the D7
// version, because the module and its newly installed schema are not
// available during hook_install() under D6.
db_query("INSERT INTO {security_questions} (sqid, question) VALUES\n (1, \"What is your mother's maiden name?\"),\n (2, \"What was your highschool mascot?\"),\n (3, \"What is your favorite hobby?\"),\n (4, \"What was your childhood nickname?\"),\n (5, \"What is the name of your favorite childhood friend?\"),\n (6, \"What is your oldest sibling's middle name?\"),\n (7, \"What is the middle name of your oldest child?\"),\n (8, \"What was the last name of your third grade teacher?\"),\n (9, \"In what city does your nearest sibling live?\")");
}
/**
* Implements hook_uninstall().
*/
function security_questions_uninstall() {
drupal_uninstall_schema('security_questions');
variable_del('security_questions_cookie');
variable_del('security_questions_cookie_expire');
variable_del('security_questions_flood_expire');
variable_del('security_questions_number_required');
variable_del('security_questions_password_reset');
variable_del('security_questions_protection_mode');
variable_del('security_questions_user_login');
variable_del('security_questions_user_questions');
}
/**
* Rebuild the menu to detect form function moves.
*/
function security_questions_update_6200() {
menu_rebuild();
}
/**
* Add the security_questions_incorrect table.
*/
function security_questions_update_6201() {
$schema['security_questions_incorrect'] = array(
'description' => 'Tracks incorrect answer attempts by IP.',
'fields' => array(
'aid' => array(
'description' => 'Unique attempt ID.',
'type' => 'serial',
'not null' => TRUE,
),
'security_question_id' => array(
'description' => 'The security question ID.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'uid' => array(
'description' => 'The user ID.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ip' => array(
'description' => 'The IP address of the visitor that attempted to answer the question as the user.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'timestamp' => array(
'description' => 'Timestamp of the failed attempt.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'expiration' => array(
'description' => 'Expiration timestamp. Expired attempts are purged on cron run.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'aid',
),
'indexes' => array(
'uid_ip' => array(
'uid',
'ip',
),
'expire' => array(
'expiration',
),
),
);
$return = array();
db_create_table($return, 'security_questions_incorrect', $schema['security_questions_incorrect']);
}
/**
* Convert the schema to support the new API.
*/
function security_questions_update_6202() {
$ret = array();
// Serial fields must be keys in MySQL, so need to add a temporary index on
// {security_questions}.security_question_id before we can change it.
db_add_index($ret, 'security_questions', 'temp', array(
'security_question_id',
));
// Convert the questions table.
db_drop_primary_key($ret, 'security_questions');
db_change_field($ret, 'security_questions', 'security_question_id', 'sqid', array(
'description' => 'The security question ID',
'type' => 'serial',
), array(
'primary key' => array(
'sqid',
),
));
db_change_field($ret, 'security_questions', 'security_question', 'question', array(
'description' => 'The text of the question',
'type' => 'varchar',
'length' => '500',
));
db_change_field($ret, 'security_questions', 'uid', 'uid', array(
'description' => '0 for questions available system-wide, or the owning uid for custom per-user questions.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
));
db_query('UPDATE {security_questions} SET uid = 0 WHERE admin = 1');
db_drop_field($ret, 'security_questions', 'admin');
db_add_field($ret, 'security_questions', 'machine_name', array(
'description' => 'An optional machine-readable name for this question, to support exportable configuration.',
'type' => 'varchar',
'length' => 255,
));
db_add_index($ret, 'security_questions', 'uid', array(
'uid',
));
// Drop our temporary index.
db_drop_index($ret, 'security_questions', 'temp');
// Rename security_question_id to sqid in related tables.
db_drop_primary_key($ret, 'security_questions_answers');
db_change_field($ret, 'security_questions_answers', 'security_question_id', 'sqid', array(
'description' => 'The security question ID',
'type' => 'int',
), array(
'primary key' => array(
'uid',
'sqid',
),
));
db_change_field($ret, 'security_questions_answers', 'user_answer', 'answer', array(
'description' => 'The answer to the users question',
'type' => 'varchar',
'length' => '100',
));
db_change_field($ret, 'security_questions_incorrect', 'security_question_id', 'sqid', array(
'description' => 'The security question ID.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
));
}
/**
* Convert the cookie expiration variable to a UNIX time value.
*/
function security_questions_update_6203() {
$expire = variable_get('security_questions_cookie_expire', '+1 week');
if (!empty($expire) && !is_numeric($expire)) {
$expire = strtotime($expire) - time();
variable_set('security_questions_cookie_expire', $expire);
}
}
Functions
Name![]() |
Description |
---|---|
security_questions_install | Implements hook_install(). |
security_questions_schema | Implements hook_schema(). |
security_questions_uninstall | Implements hook_uninstall(). |
security_questions_update_6200 | Rebuild the menu to detect form function moves. |
security_questions_update_6201 | Add the security_questions_incorrect table. |
security_questions_update_6202 | Convert the schema to support the new API. |
security_questions_update_6203 | Convert the cookie expiration variable to a UNIX time value. |