You are here

function smart_ip_user_login in Smart IP 8.2

Same name and namespace in other branches
  1. 7 smart_ip.module \smart_ip_user_login()

Implements hook_user_login().

Save user's geolocation upon login.

File

./smart_ip.module, line 75
Determines country, geo location (longitude/latitude), region, city and postal code of the user, based on IP address

Code

function smart_ip_user_login(AccountInterface $account) {

  // Now check to see if this user has "administer smart_ip" permission
  // and if debug mode set.  If both are TRUE, use debug information
  // instead of real information
  $has_access = \Drupal::currentUser()
    ->hasPermission('administer smart_ip');
  $debug = \Drupal::config('smart_ip.settings')
    ->get('debug_mode');
  if ($has_access && $debug) {
    $ip = \Drupal::config('smart_ip.settings')
      ->get('debug_mode_ip');
    $location = SmartIp::query($ip);
    if (!empty($location)) {
      drupal_set_message(t('Using debug IP: %ip / Country: %country / Region: %region / City: %city / Postal code: %zip / Longitude: %long / Latitude: %lat / Time zone: %time_zone', array(
        '%ip' => $location['ipAddress'],
        '%country' => $location['country'],
        '%region' => $location['region'],
        '%city' => $location['city'],
        '%zip' => $location['zip'],
        '%long' => $location['longitude'],
        '%lat' => $location['latitude'],
        '%time_zone' => $location['timeZone'],
      )));
      \Drupal::service('smart_ip.smart_ip_location')
        ->save();
    }
  }
  else {
    $roles = \Drupal::config('smart_ip.settings')
      ->get('roles_to_geolocate');
    foreach ($roles as $rid) {
      if (in_array($rid, $account
        ->getRoles())) {

        // Save logged in user's location
        SmartIp::query();
        \Drupal::service('smart_ip.smart_ip_location')
          ->save();
        break;
      }
    }
  }
}