You are here

public static function SmartIp::updateUserLocation in Smart IP 8.3

Same name and namespace in other branches
  1. 8.4 src/SmartIp.php \Drupal\smart_ip\SmartIp::updateUserLocation()
  2. 8.2 src/SmartIp.php \Drupal\smart_ip\SmartIp::updateUserLocation()

Update user's location only if the IP address stored in session is not the same as the IP address detected by the server or debug mode IP.

2 calls to SmartIp::updateUserLocation()
GeolocateUserSubscriber::geolocateUser in src/EventSubscriber/GeolocateUserSubscriber.php
Initiate user geolocation.
SmartIpLocation::getData in src/SmartIpLocation.php
Gets all the Smart IP location data.

File

src/SmartIp.php, line 217
Contains \Drupal\smart_ip\SmartIp.

Class

SmartIp
Smart IP static basic methods wrapper.

Namespace

Drupal\smart_ip

Code

public static function updateUserLocation() {

  // Check if the user permitted to share location.
  $shareLocation = self::getSession('smart_ip_user_share_location_permitted', TRUE);
  if ($shareLocation) {
    $config = \Drupal::config('smart_ip.settings');
    $inDebugMode = FALSE;
    $dontGeolocate = TRUE;
    $debugModeIp = '';
    $user = \Drupal::currentUser();
    $userRoles = $user
      ->getRoles();
    $rolesDebug = $config
      ->get('roles_in_debug_mode');
    $rolesDebugIp = $config
      ->get('roles_in_debug_mode_ip');
    $rolesGeolocate = $config
      ->get('roles_to_geolocate');
    foreach ($userRoles as $userRole) {
      if (isset($rolesGeolocate[$userRole]) && $rolesGeolocate[$userRole]) {

        // This user role is in the list of "Roles to Geolocate".
        $dontGeolocate = FALSE;
      }
      if ($userRole != 'authenticated' && isset($rolesDebug[$userRole]) && $rolesDebug[$userRole]) {

        // Prioritize other roles than 'authenticated'.
        $inDebugMode = TRUE;
        $debugModeIp = $rolesDebugIp[$userRole];
        break;
      }
    }
    if (!$dontGeolocate) {
      if ($inDebugMode) {

        // Use debug information instead of real information.
        $ip = $debugModeIp;
      }
      elseif ($user
        ->isAuthenticated() && isset($rolesDebug['authenticated']) && $rolesDebug['authenticated']) {

        // The 'authenticated' role should be the last to check if it is in
        // debug mode then use debug information instead of real information.
        $ip = $rolesDebugIp['authenticated'];
      }
      else {
        $ip = \Drupal::request()
          ->getClientIp();
      }
      $smartIpSession = self::getSession('smart_ip');
      if (!isset($smartIpSession['location']['ipAddress']) || $smartIpSession['location']['ipAddress'] != $ip) {

        /** @var \Drupal\smart_ip\SmartIpLocation $location */
        $location = \Drupal::service('smart_ip.smart_ip_location');
        $location
          ->delete();
        $result = self::query($ip);
        self::userLocationFallback($result);
        $location
          ->setData($result);
        $location
          ->save();
      }
    }
  }
}