You are here

public function LoginHistoryController::report in Login History 8

Displays a report of user logins.

Parameters

\Drupal\user\UserInterface|null $user: (optional) The user to display for individual user reports.

Return value

array A render array.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

File

src/Controller/LoginHistoryController.php, line 82

Class

LoginHistoryController
Controller routines for Login history routes.

Namespace

Drupal\login_history\Controller

Code

public function report(UserInterface $user = NULL) {
  $header = [
    [
      'data' => $this
        ->t('Date'),
      'field' => 'lh.login',
      'sort' => 'desc',
    ],
    [
      'data' => $this
        ->t('Username'),
      'field' => 'ufd.name',
    ],
    [
      'data' => $this
        ->t('IP Address'),
      'field' => 'lh.hostname',
    ],
    [
      'data' => $this
        ->t('One-time login?'),
      'field' => 'lh.one_time',
    ],
    [
      'data' => $this
        ->t('User Agent'),
    ],
  ];
  $query = $this->database
    ->select('login_history', 'lh')
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->join('users', 'u', 'lh.uid = u.uid');
  $query
    ->join('users_field_data', 'ufd', 'u.uid = ufd.uid');
  if ($user) {
    $query
      ->condition('lh.uid', $user
      ->id());
  }
  $result = $query
    ->fields('lh')
    ->fields('u', [
    'uid',
  ])
    ->fields('ufd', [
    'name',
  ])
    ->orderByHeader($header)
    ->limit(50)
    ->execute()
    ->fetchAll();
  return $this
    ->generateReportTable($result, $header);
}