You are here

public function DefaultController::backgroundProcessServiceAccess in Background Process 8

Implements Background Process Service Access.

1 string reference to 'DefaultController::backgroundProcessServiceAccess'
background_process.routing.yml in ./background_process.routing.yml
background_process.routing.yml

File

src/Controller/DefaultController.php, line 20

Class

DefaultController
Implements Default controller for the background_process module.

Namespace

Drupal\background_process\Controller

Code

public function backgroundProcessServiceAccess($handle, $token, AccountInterface $account) {

  // Setup service.
  ignore_user_abort(TRUE);
  $handle = rawurldecode($handle);
  $token = rawurldecode($token);

  // Ensure no session!
  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 = new AnonymousUserSession();
  }
  return AccessResult::allowedIf($account
    ->hasPermission(TRUE));
}