diff --git a/src/Controller/LoginHistoryController.php b/src/Controller/LoginHistoryController.php index 740f15d..a798997 100644 --- a/src/Controller/LoginHistoryController.php +++ b/src/Controller/LoginHistoryController.php @@ -4,56 +4,29 @@ namespace Drupal\login_history\Controller; use Drupal\Core\Access\AccessResult; use Drupal\Core\Controller\ControllerBase; -use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\user\Entity\User; use Drupal\user\UserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +/** + * @file + * Contains \Drupal\login_history\Controller\LoginHistoryController. + */ + /** * Controller routines for Login history routes. - * - * @deprecated in login_history:8.x-1.1 and is removed from login_history:8.x-2.0. - * There is no replacement. - * @see https://www.drupal.org/project/login_history/issues/3185800 */ class LoginHistoryController extends ControllerBase { /** - * The date formatter. - * - * @var \Drupal\Core\Datetime\DateFormatterInterface - */ - protected $dateFormatter; - - /** - * The database connection. - * - * @var \Drupal\Core\Database\Connection - */ - protected $database; - - /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - - /** - * Constructs a LoginHistoryController object. + * Constructs a \Drupal\login_history\Controller\LoginHistoryController object. * * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter * The date formatter service. - * @param \Drupal\Core\Database\Connection|null $database - * The database connection. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entity_type_manager - * The entity type manager. */ - public function __construct(DateFormatterInterface $date_formatter, Connection $database = NULL, EntityTypeManagerInterface $entity_type_manager = NULL) { + public function __construct(DateFormatterInterface $date_formatter) { $this->dateFormatter = $date_formatter; - $this->database = $database ?: \Drupal::database(); - $this->entityTypeManager = $entity_type_manager ?: \Drupal::entityTypeManager(); } /** @@ -61,34 +34,29 @@ class LoginHistoryController extends ControllerBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('date.formatter'), - $container->get('database'), - $container->get('entity_type.manager') + $container->get('date.formatter') ); } /** * Displays a report of user logins. * - * @param \Drupal\user\UserInterface|null $user + * @param \Drupal\user\UserInterface $user * (optional) The user to display for individual user reports. * * @return array * A render array. - * - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException */ 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')], + ['data' => t('Date'), 'field' => 'lh.login', 'sort' => 'desc'], + ['data' => t('Username'), 'field' => 'ufd.name'], + ['data' => t('IP Address'), 'field' => 'lh.hostname'], + ['data' => t('One-time login?'), 'field' => 'lh.one_time'], + ['data' => t('User Agent')], ]; - $query = $this->database->select('login_history', 'lh') + $query = db_select('login_history', 'lh') ->extend('Drupal\Core\Database\Query\TableSortExtender') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); @@ -121,26 +89,22 @@ class LoginHistoryController extends ControllerBase { * * @return array * A table render array. - * - * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException - * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException */ - public function generateReportTable(array $history, array $header) { + function generateReportTable(array $history, array $header) { // Load all users first. $uids = []; foreach ($history as $entry) { $uids[] = $entry->uid; } - /** @var \Drupal\user\Entity\User[] $users */ - $users = $this->entityTypeManager->getStorage('user')->loadMultiple($uids); + $users = User::loadMultiple($uids); $rows = []; foreach ($history as $entry) { $rows[] = [ - $this->dateFormatter->format($entry->login, 'short'), - $users[$entry->uid]->getAccountName(), + $this->dateFormatter->format($entry->login, 'small'), + $users[$entry->uid]->getUsername(), $entry->hostname, - empty($entry->one_time) ? $this->t('Regular login') : $this->t('One-time login'), + empty($entry->one_time) ? t('Regular login') : t('One-time login'), $entry->user_agent, ]; } @@ -148,7 +112,7 @@ class LoginHistoryController extends ControllerBase { '#theme' => 'table', '#header' => $header, '#rows' => $rows, - '#empty' => $this->t('No login history available.'), + '#empty' => t('No login history available.'), ]; $output['pager'] = [ '#type' => 'pager', @@ -160,11 +124,8 @@ class LoginHistoryController extends ControllerBase { /** * Checks access for the user login report. * - * @param \Drupal\user\UserInterface|null $user + * @param \Drupal\user\UserInterface $user * The user to check access for. - * - * @return \Drupal\Core\Access\AccessResult - * Returns Allowed or Neutral. */ public function checkUserReportAccess(UserInterface $user = NULL) { // Allow access if the user is viewing their own report and has permission diff --git a/src/Plugin/Block/LastLoginBlock.php b/src/Plugin/Block/LastLoginBlock.php index 9c2dd6f..f2b7509 100644 --- a/src/Plugin/Block/LastLoginBlock.php +++ b/src/Plugin/Block/LastLoginBlock.php @@ -4,12 +4,13 @@ namespace Drupal\login_history\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockBase; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Session\AccountProxy; use Drupal\Core\Url; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\RequestStack; + +/** + * @file + * Contains \Drupal\login_history\Plugin\Block\LastLoginBlock. + */ /** * Provides a block with information about the user's last login. @@ -20,54 +21,7 @@ use Symfony\Component\HttpFoundation\RequestStack; * category = @Translation("User"), * ) */ -class LastLoginBlock extends BlockBase implements ContainerFactoryPluginInterface { - - /** - * The account proxy service. - * - * @var \Drupal\Core\Session\AccountProxy - */ - protected $currentUser; - - /** - * The account proxy service. - * - * @var \Symfony\Component\HttpFoundation\RequestStack - */ - protected $requestStack; - - /** - * Creates a LastLoginBlock instance. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Session\AccountProxy $currentUser - * The account proxy service. - * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack - * The request stack service. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxy $currentUser, RequestStack $requestStack) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->currentUser = $currentUser; - $this->requestStack = $requestStack; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('current_user'), - $container->get('request_stack') - ); - } +class LastLoginBlock extends BlockBase { /** * {@inheritdoc} @@ -81,30 +35,17 @@ class LastLoginBlock extends BlockBase implements ContainerFactoryPluginInterfac */ public function build() { $build = []; - - $last_login = FALSE; - if (!$this->currentUser->isAnonymous()) { - // Get the previous login information. - $last_login = \Drupal::database()->select('login_history', 'lh') - ->fields('lh', ['login', 'hostname', 'one_time', 'user_agent']) - ->condition('uid', $this->currentUser->id()) - ->orderBy('login', 'DESC') - ->range(1, 1) - ->execute() - ->fetch(); - } - - if ($last_login) { - $request = $this->requestStack->getCurrentRequest(); - $hostname = $last_login->hostname == $request->getClientIP() ? $this->t('this IP address') : $last_login->hostname; - $user_agent = $last_login->user_agent == $request->server->get('HTTP_USER_AGENT') ? $this->t('this browser') : $last_login->user_agent; - $build['last_login']['#markup'] = '

' . $this->t('You last logged in from @hostname using @user_agent.', - ['@hostname' => $hostname, '@user_agent' => $user_agent]) . '

'; - if ($this->currentUser->hasPermission('view own login history')) { + if ($last_login = login_history_last_login()) { + $request = \Drupal::request(); + $hostname = $last_login->hostname == $request->getClientIP() ? t('this IP address') : $last_login->hostname; + $user_agent = $last_login->user_agent == $request->server->get('HTTP_USER_AGENT') ? t('this browser') : $last_login->user_agent; + $build['last_login']['#markup'] = '

' . t('You last logged in from @hostname using @user_agent.', ['@hostname' => $hostname, '@user_agent' => $user_agent]) . '

'; + $user = \Drupal::currentUser(); + if ($user->hasPermission('view own login history')) { $build['view_report'] = [ '#type' => 'more_link', '#title' => $this->t('View your login history'), - '#url' => Url::fromRoute('login_history.user_report', ['user' => $this->currentUser->id()]), + '#url' => Url::fromRoute('login_history.user_report', ['user' => $user->id()]), ]; } }