function ip2country_user_login in IP-based Determination of a Visitor's Country 8
Same name and namespace in other branches
- 7 ip2country.module \ip2country_user_login()
Implements hook_user_login().
Detects IP and determines country upon user login.
File
- ./
ip2country.module, line 90 - Determination of user's Country based on IP.
Code
function ip2country_user_login($account) {
// Successful login. First determine user's country based on IP.
$ip = \Drupal::request()
->getClientIp();
$country_lookup = \Drupal::service('ip2country.lookup');
$country_code = $country_lookup
->getCountry($ip);
$ip2country_config = \Drupal::config('ip2country.settings');
// Now check to see if this user has "administer ip2country" permission
// and if debug mode set. If both are TRUE, use debug information
// instead of real information.
if (\Drupal::currentUser()
->hasPermission('administer ip2country') && $ip2country_config
->get('debug')) {
$type = $ip2country_config
->get('test_type');
if ($type == 0) {
// Debug Country entered.
$country_code = $ip2country_config
->get('test_country');
}
else {
// Debug IP entered.
$ip = $ip2country_config
->get('test_ip_address');
$country_code = $country_lookup
->getCountry($ip);
}
\Drupal::messenger()
->addMessage(t('Using DEBUG value for Country - @country', [
'@country' => $country_code,
]));
}
// Finally, save country, if it has been determined.
if ($country_code) {
// Store the ISO country code in the user.data service object.
\Drupal::service('user.data')
->set('ip2country', $account
->id(), 'country_iso_code_2', $country_code);
}
}