You are here

public function CasAttributesListController::content in CAS Attributes 2.x

Same name and namespace in other branches
  1. 8 src/Controller/CasAttributesListController.php \Drupal\cas_attributes\Controller\CasAttributesListController::content()

Lists all currently loaded CAS attributes.

1 string reference to 'CasAttributesListController::content'
cas_attributes.routing.yml in ./cas_attributes.routing.yml
cas_attributes.routing.yml

File

src/Controller/CasAttributesListController.php, line 68

Class

CasAttributesListController
Class CasAttributesListController.

Namespace

Drupal\cas_attributes\Controller

Code

public function content() {
  $build = [
    '#cache' => [
      'contexts' => [
        'session',
      ],
    ],
  ];
  $session = $this->requestStack
    ->getCurrentRequest()
    ->getSession();
  if (!$this->settings
    ->get('sitewide_token_support')) {
    $this->messenger
      ->addError($this
      ->t('You must <a href="@link">enable sitewide token support</a> to view the list of available attributes for the currently logged in user. Note that enabling that feature is not required to define user field mappings, but it is required if you want to use this page.', [
      '@link' => Url::fromRoute('cas_attributes.settings')
        ->toString(),
    ]));
  }
  else {
    if (!$session
      ->get('is_cas_user')) {
      $this->messenger
        ->addError($this
        ->t('You must login through CAS view available CAS attributes.'));
    }
    else {
      $attributes = $this->requestStack
        ->getCurrentRequest()
        ->getSession()
        ->get('cas_attributes', []);
      $table = [
        '#type' => 'table',
        '#header' => [
          'Name',
          'Token',
          'Value',
        ],
        '#empty' => $this
          ->t('There are no CAS attributes associated with your session.'),
        '#caption' => $this
          ->t('This table contains all attributes returned from your CAS server for the currently logged in user.'),
      ];
      $row = 0;
      foreach ($attributes as $attrName => $attrValue) {
        $table[$row] = [
          'name' => [
            '#plain_text' => $attrName,
          ],
          'token' => [
            '#plain_text' => '[cas:attribute:' . mb_strtolower($attrName . ']'),
          ],
          'value' => [
            '#plain_text' => var_export($attrValue, TRUE),
          ],
        ];
        $row++;
      }
      $build['table'] = $table;
    }
  }
  return $build;
}