/**
  * Access control for the subscription settings user page.
  *
  * The user is checked for both global permissions and permissions to edit
  * his own subscriptions.
  *
  * @param \Drupal\Core\Entity\EntityInterface $user
  *    The user object from the route.
  *
  * @return \Drupal\Core\Access\AccessResult
  *    An access result object carrying the result of the check.
  */
 public function access(EntityInterface $user)
 {
     if ($this->currentUser->hasPermission('manage all subscriptions')) {
         return AccessResult::allowed();
     } elseif (!$this->currentUser->isAnonymous() && $this->currentUser->id() == $user->id() && $this->currentUser->hasPermission('manage own subscriptions')) {
         return AccessResult::allowed();
     }
     return AccessResult::forbidden();
 }
 /**
  * Renders the main dashboard page.
  */
 public function page()
 {
     $user_id = $this->currentUser->id();
     $subscription_settings_url = Url::fromRoute('joinup_subscription.subscription_settings', ['user' => $user_id]);
     $links['subscription_settings'] = ['title' => $this->t('My subscriptions'), 'url' => $subscription_settings_url, 'attributes' => ['class' => ['button', 'button--small']]];
     $licences_url = Url::fromRoute('joinup_licence.overview');
     $links['licences'] = ['title' => $this->t('Licences overview'), 'url' => $licences_url, 'attributes' => ['class' => ['button', 'button--small']]];
     $links = array_filter($links, function ($link) {
         return $link['url']->access();
     });
     $links = ['#theme' => 'links', '#links' => $links];
     return $links;
 }
 /**
  * Index page.
  *
  * @return array
  */
 public function index()
 {
     $build = [];
     $build[] = ['#type' => 'markup', '#markup' => $this->t('Welcome <em>@user</em>', ['@user' => $this->current_user->getAccountName()]), '#prefix' => '<div>', '#suffix' => '</div>'];
     // Generate page number list.
     $list = [];
     for ($id = 1; $id <= 10; $id++) {
         $url = Url::fromRoute('pages_test.info', ['id' => $id]);
         $list[] = $this->l($this->t('page #@page_number', ['@page_number' => $id]), $url);
     }
     $build['page_links'] = array('#theme' => 'item_list', '#items' => $list, '#title' => $this->t('Page Links'));
     // Override title.
     // Though not recommended to override here.
     $build['#title'] = 'Pages Test Index';
     return $build;
 }
  /**
   * Perform the anonymous user redirection, if needed.
   *
   * This method is called whenever the KernelEvents::REQUEST event is
   * dispatched.
   *
   * @param GetResponseEvent $event
   */
  public function redirect(GetResponseEvent $event) {
    // Skip if maintenance mode is enabled.
    if ($this->state->get('system.maintenance_mode')) {
      return;
    }

    // Skip if running from the command-line.
    if (PHP_SAPI === 'cli') {
      return;
    }

    // Skip if no paths are configured for redirecting.
    if (!($paths = $this->paths()) || empty($paths['include'])) {
      return;
    }

    // Skip if the user is not anonymous.
    if (!$this->current_user->isAnonymous()) {
      return;
    }

    // Determine the current path and alias.
    $current = [
      'path' => $this->path_current->getPath(),
      'alias' => \Drupal::request()->getRequestUri(),
    ];
  
    // Ignore PHP file requests.
    if (substr($current['path'], -4) == '.php') {
      return;
    }

    // Ignore the user login page.
    if ($current['path'] == '/user/login') {
      return;
    }

    // Convert the path to the front page token, if needed.
    $current['path'] = ($current['path'] != '/') ? $current['path'] : '<front>';

    // Track if we should redirect.
    $redirect = FALSE;

    // Iterate the current path and alias.
    foreach ($current as &$check) {
      // Remove the leading slash.
      $check = substr($check, 1);

      // Check if there is a trailer slash.
      if (substr($check, -1) == '/') {
        // Remove it.
        $check = substr($check, 0, strlen($check) - 1);
      }

      // Redirect if the path is a match for included paths.
      if ($this->path_matcher->matchPath($check, implode("\n", $paths['include']))) {
        $redirect = TRUE;
      }
      // Do not redirect if the path is a match for excluded paths.
      if ($this->path_matcher->matchPath($check, implode("\n", $paths['exclude']))) {
        $redirect = FALSE;
        // Matching an excluded path is a hard-stop.
        break;
      }
    }

    // See if we're going to redirect.
    if ($redirect) {
      // See if we have a message to display.
      if ($message = $this->config_factory->get('anonymous_login.settings')->get('message')) {
        // @todo: translation?
        // @todo: This does not show after the redirect..
        drupal_set_message($message);
      }
            
      // Redirect to the login, keeping the requested alias as the destination.
      $response = new RedirectResponse('/user/login?destination=' . $current['alias']);
      $response->send();
      exit();
    }
  }