You are here

function background_process_service_access in Background Process 8

Same name and namespace in other branches
  1. 6 background_process.module \background_process_service_access()
  2. 7.2 background_process.module \background_process_service_access()
  3. 7 background_process.module \background_process_service_access()

Implements to Access handler for service call.

1 call to background_process_service_access()
_background_process_queue in ./background_process.module
Worker callback for processing queued function call.

File

./background_process.module, line 213
This module implements a framework for calling funtions in the background.

Code

function background_process_service_access($handle, $token) {

  // Setup service.
  ignore_user_abort(TRUE);

  // Damn those slashes!
  $handle = rawurldecode($handle);
  $token = rawurldecode($token);

  // Ensure no session!
  drupal_save_session(FALSE);
  unset($_SESSION);
  $process = background_process_get_process($handle);
  if (!$process) {
    \Drupal::logger('bg_process')
      ->notice('Unknown process: %handle', [
      '%handle' => $handle,
    ]);
    return FALSE;
  }
  if ($token !== $process->token) {
    \Drupal::logger('bg_process')
      ->notice('Invalid token: %token for handle: %handle', [
      '%token' => $token,
      '%handle' => $handle,
    ]);
    return FALSE;
  }

  // Login as the user that requested the call.
  $user = \Drupal::currentUser();
  if ($process->uid) {
    $load_user = \Drupal::entityManager()
      ->getStorage('user')
      ->load($process->uid);
    if (!$load_user) {

      // Invalid user!
      \Drupal::logger('bg_process')
        ->notice('Invalid user: %uid for handle: %handle', [
        '%uid' => $process->uid,
        '%handle' => $handle,
      ]);
      return FALSE;
    }
    $user = $load_user;
  }
  else {
    $user = drupal_anonymous_user();
  }
  return TRUE;
}