You are here

public function DeveloperStatusWarningSubscriber::onRespond in Apigee Edge 8

Display's a warning message if developer's status is inactive.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

src/EventSubscriber/DeveloperStatusWarningSubscriber.php, line 98

Class

DeveloperStatusWarningSubscriber
Displays a warning message on developer app pages if app owner is inactive.

Namespace

Drupal\apigee_edge\EventSubscriber

Code

public function onRespond(FilterResponseEvent $event) {

  // Anonymous user's does not have access to these routes.
  if ($this->currentUser
    ->isAuthenticated() && strpos($this->routeMatch
    ->getRouteName(), 'entity.developer_app.') === 0) {
    $developer_storage = $this->entityTypeManager
      ->getStorage('developer');

    /** @var \Drupal\apigee_edge\Entity\DeveloperInterface|NULL $developer */
    $developer = NULL;

    /** @var \Drupal\Core\Session\AccountInterface|NULL $account */
    $account = NULL;

    /** @var \Drupal\apigee_edge\Entity\DeveloperAppInterface $app */
    $app = $this->routeMatch
      ->getParameter('developer_app') ?? $this->routeMatch
      ->getParameter('app');
    if ($app) {

      /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
      $developer = $developer_storage
        ->load($app
        ->getDeveloperId());
      $account = $developer
        ->getOwner();
    }
    elseif ($this->routeMatch
      ->getRouteName() === 'entity.developer_app.collection_by_developer') {

      /** @var \Drupal\Core\Session\AccountInterface $account */
      $account = $this->routeMatch
        ->getParameter('user');
      $developer = $developer_storage
        ->load($account
        ->getEmail());
    }

    // If we could figure out the developer from the route and its status
    // is inactive.
    if ($developer && $developer
      ->getStatus() === DeveloperInterface::STATUS_INACTIVE) {
      if ($this->currentUser
        ->getEmail() === $developer
        ->getEmail()) {
        $message = $this
          ->t('Your developer account has inactive status so you will not be able to use your credentials until your account gets activated. Please contact support for further assistance.');
      }
      else {

        // It could happen that the app's owner (developer) does not have
        // a Drupal user yet. (The two system is out of sync.)
        if ($account) {
          $message = $this
            ->t('The developer account of <a href=":url">@username</a> has inactive status so this user has invalid credentials until the account gets activated.', [
            ':url' => Url::fromRoute('entity.user.edit_form', [
              'user' => $account
                ->id(),
            ])
              ->toString(),
            '@username' => $account
              ->getDisplayName(),
          ]);
        }
        else {
          $message = $this
            ->t("The @developer developer has inactive status so it has invalid credentials until its account gets activated.", [
            '@developer' => $developer
              ->label(),
          ]);
        }
      }
      $this->messenger
        ->addWarning($message);
    }
  }
}