You are here

public function OauthController::authPage in Media: Acquia DAM 8

Builds the auth page for a given user.

Route: /user/{$user}/acquiadam.

Parameters

\Drupal\user\UserInterface $user: The User object.

Return value

array Array with HTML markup message.

1 string reference to 'OauthController::authPage'
media_acquiadam.routing.yml in ./media_acquiadam.routing.yml
media_acquiadam.routing.yml

File

src/Controller/OauthController.php, line 106

Class

OauthController
Controller routines for acquiadam routes.

Namespace

Drupal\media_acquiadam\Controller

Code

public function authPage(UserInterface $user) {

  // Users cannot access other users' auth forms.
  if ($user
    ->id() !== $this
    ->currentUser()
    ->id()) {
    throw new NotFoundHttpException();
  }
  $result = [];

  /** @var \Drupal\Core\Config\Config $media_acquiadam_settings */
  $media_acquiadam_settings = $this
    ->config('media_acquiadam.settings');
  $redirect_url = sprintf('/user/%d/acquiadam', $this
    ->currentUser()
    ->id());
  $access_token = $this->userData
    ->get('media_acquiadam', $this->currentUser
    ->id(), 'acquiadam_access_token');
  $refresh_token = $this->userData
    ->get('media_acquiadam', $this->currentUser
    ->id(), 'acquiadam_refresh_token');
  $access_token_expiration = $this->userData
    ->get('media_acquiadam', $this->currentUser
    ->id(), 'acquiadam_access_token_expiration');
  $is_expired = empty($access_token) || $access_token_expiration <= time();
  $is_authenticated = !$is_expired || $is_expired && !empty($refresh_token);
  $is_no_credentials = !$media_acquiadam_settings
    ->get('secret') || !$media_acquiadam_settings
    ->get('client_id');
  if ($is_no_credentials) {
    $result[] = [
      '#markup' => '<p>' . $this
        ->t('The Acquia DAM module is not fully configured.') . '</p>',
    ];
    if ($this
      ->currentUser()
      ->hasPermission('administer site configuration')) {
      $config_url = $this->urlGenerator
        ->generateFromRoute('media_acquiadam.config', [], [
        'query' => [
          'destination' => $redirect_url,
        ],
      ]);
      $result[] = [
        '#markup' => '<p>' . $this
          ->t('Please <a href="@configure">configure</a> the Acquia DAM module to continue.', [
          '@configure' => $config_url,
        ]) . '</p>',
      ];
    }
    else {
      $result[] = [
        '#markup' => '<p>' . $this
          ->t('Please contact a site administrator to continue.') . '</p>',
      ];
    }
  }
  elseif ($is_expired) {
    $this->userData
      ->delete('media_acquiadam', $this->currentUser
      ->id(), 'acquiadam_access_token');
    $this->userData
      ->delete('media_acquiadam', $this->currentUser
      ->id(), 'acquiadam_access_token_expiration');
    $this->userData
      ->delete('media_acquiadam', $this->currentUser
      ->id(), 'acquiadam_refresh_token');
    $link = Link::createFromRoute('Authenticate', 'media_acquiadam.auth_start', [
      'auth_finish_redirect' => $redirect_url,
    ]);
    $result[] = [
      '#markup' => '<p>' . $this
        ->t('You are <strong>not</strong> authenticated with Acquia DAM.') . '</p>',
    ];
    $result[] = [
      '#markup' => '<p>' . $link
        ->toString() . '</p>',
    ];
  }
  elseif ($is_authenticated) {
    $logout_link = Link::createFromRoute('Logout from DAM', 'media_acquiadam.logout', [
      'auth_finish_redirect' => $redirect_url,
    ]);
    $reauthenticate_link = Link::createFromRoute('Reauthenticate', 'media_acquiadam.auth_start', [
      'auth_finish_redirect' => $redirect_url,
    ]);
    $result[] = [
      '#markup' => '<p>' . $this
        ->t('You are authenticated with Acquia DAM.') . '</p>',
    ];
    $result[] = [
      '#markup' => '<p>' . $this
        ->t('Your authentication expires on @date.', [
        '@date' => $this->dateFormatter
          ->format($access_token_expiration),
      ]) . '</p>',
    ];
    $result[] = [
      '#markup' => '<p>' . $reauthenticate_link
        ->toString() . ' | ' . $logout_link
        ->toString() . '</p>',
    ];
  }
  return !empty($result) ? $result : NULL;
}