ip_login.install in IP Login 7.3
Same filename and directory in other branches
Install file of the IP Login module.
File
ip_login.installView source
<?php
/**
* @file
* Install file of the IP Login module.
*/
/**
* Implementation of hook_install().
*/
function ip_login_install() {
drupal_set_message(st('IP Login settings are available under <a href="@link">Administer > Configuration > People > IP Login settings</a>.', array(
'@link' => 'admin/config/people/ip_login',
)));
// Clear field info cache in case field_ipaddress
// is being installed at the same time
field_info_cache_clear();
// Install field_ip_login
// Check if our field is not already created.
if (!field_info_field('field_ip_login') && module_exists('field_ipaddress')) {
$field = array(
'field_name' => 'field_ip_login',
'type' => 'field_ipaddress',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_ip_login',
'entity_type' => 'user',
'bundle' => 'user',
'label' => 'IP address',
'description' => ip_login_help_ranges(),
'required' => FALSE,
'settings' => array(
'user_register_form' => 0,
),
'widget' => array(
'type' => 'field_ipaddress_shorthand',
),
);
field_create_instance($instance);
}
else {
drupal_set_message(t('A problem occurred when creating the default field_ip_login: field_ipaddress field type not available.
Please create field_ip_login manually on the user account, type: field_ipaddress.'), 'warning');
}
variable_set('ip_login_enabled', 1);
}
/**
* Create field_ip_login on user entity.
* Migrate data from ip_login_user to new field
*/
function ip_login_update_7300(&$sandbox) {
if (!module_exists('field_ipaddress')) {
throw new DrupalUpdateException(t('IP Login 7.3.x requires the module field_ipaddress to be enabled.', array(
'field_ipaddress' => l('field_ip_address', 'http://drupal.org/project/field_ipaddress', array(
'external' => TRUE,
)),
)));
return;
}
// Create the IP Address field
// field_ip_login
// on the User entity
if (!isset($sandbox['field_created'])) {
// Check if our field is not already created.
if (!field_info_field('field_ip_login')) {
$field = array(
'field_name' => 'field_ip_login',
'type' => 'field_ipaddress',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_ip_login',
'entity_type' => 'user',
'bundle' => 'user',
'label' => 'IP address',
'description' => ip_login_help_ranges(),
'required' => FALSE,
'settings' => array(
'user_register_form' => 0,
),
'widget' => array(
'type' => 'field_ipaddress_shorthand',
),
);
field_create_instance($instance);
$sandbox['field_created'] = TRUE;
}
}
// Migrate the data
// from ip_login_user
// to field_ip_login data tables
if (db_table_exists('ip_login_user')) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_uid'] = 0;
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {ip_login_user}')
->fetchField();
}
// Select 10 users to process
$result = db_select('ip_login_user', 'u')
->fields('u', array(
'uid',
'ip_match',
))
->condition('u.uid', $sandbox['current_uid'], '>')
->range(0, 10)
->orderBy('u.uid', 'ASC')
->execute();
foreach ($result as $row) {
$sandbox['current_uid'] = $row->uid;
$sandbox['progress']++;
if (empty($row->ip_match)) {
continue;
}
// Try shorthand match
$value = _field_ipaddress_shorthand2long(trim($row->ip_match));
// Try CIDR match
if (!$value && strpos($row->ip_match, '/') !== FALSE) {
$value = _field_ipaddress_cidr2long(trim($row->ip_match));
}
if ($value) {
$account = user_load($row->uid);
$language = empty($account->language) ? LANGUAGE_NONE : $account->language;
if (empty($account->field_ip_login[$language][0])) {
$edit = array(
'field_ip_login' => array(
$language => array(
0 => $value,
),
),
);
user_save($account, $edit);
}
}
else {
watchdog('ip_login', 'Invalid IP range or address @range for user id (@uid). IP address match _NOT_ migrated for this user.', array(
'@range' => $row->ip_match,
'@uid' => $row->uid,
));
$sandbox['errors'] = TRUE;
}
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
// Tell user about watchdog log if any errors
if ($sandbox['#finished'] && !empty($sandbox['#errors'])) {
drupal_set_message('Some errors were encountered during IP address migration. Check the watchdog log for more details.');
}
elseif ($sandbox['#finished']) {
return t('IP Login field migration completed successfully.');
}
}
}
Functions
Name![]() |
Description |
---|---|
ip_login_install | Implementation of hook_install(). |
ip_login_update_7300 | Create field_ip_login on user entity. Migrate data from ip_login_user to new field |