diff --git a/login_history.info.yml b/login_history.info.yml index 37e2648..9dfba3d 100644 --- a/login_history.info.yml +++ b/login_history.info.yml @@ -1,5 +1,6 @@ name: Login History description: A history of the site's user logins. core: 8.x +core_version_requirement: ^8 || ^9 type: module configure: login_history.report diff --git a/login_history.module b/login_history.module index 624774b..2e04062 100644 --- a/login_history.module +++ b/login_history.module @@ -32,7 +32,7 @@ function login_history_user_login($account) { } // Now save the user's current login timestamp to login_history. - db_insert('login_history') + \Drupal::database()->insert('login_history') ->fields(array( 'uid' => $account->id(), 'login' => $account->getLastLoginTime(), @@ -60,7 +60,7 @@ function login_history_last_login(AccountInterface $account = NULL) { if ($account->isAnonymous()) { return; } - $last_login = db_query("SELECT login, hostname, one_time, user_agent + $last_login = \Drupal::database()->query("SELECT login, hostname, one_time, user_agent FROM {login_history} WHERE uid = :uid ORDER BY login DESC diff --git a/src/Controller/LoginHistoryController.php b/src/Controller/LoginHistoryController.php index 21afd31..a81a9aa 100644 --- a/src/Controller/LoginHistoryController.php +++ b/src/Controller/LoginHistoryController.php @@ -10,6 +10,7 @@ namespace Drupal\login_history\Controller; use Drupal\Component\Utility\Html; use Drupal\Core\Access\AccessResult; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Database\Connection; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\user\Entity\User; use Drupal\user\UserInterface; @@ -21,14 +22,24 @@ use Symfony\Component\HttpFoundation\Request; */ class LoginHistoryController extends ControllerBase { + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $database; + /** * Constructs a \Drupal\login_history\Controller\LoginHistoryController object. * * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter * The date formatter service. + * @param \Drupal\Core\Database\Connection $database + * The database connection. */ - public function __construct(DateFormatterInterface $date_formatter) { + public function __construct(DateFormatterInterface $date_formatter, Connection $database) { $this->dateFormatter = $date_formatter; + $this->database = $database; } /** @@ -36,7 +47,8 @@ class LoginHistoryController extends ControllerBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('date.formatter') + $container->get('date.formatter'), + $container->get('database') ); } @@ -58,7 +70,7 @@ class LoginHistoryController extends ControllerBase { array('data' => t('User Agent')), ); - $query = db_select('login_history', 'lh') + $query = $this->database->select('login_history', 'lh') ->extend('Drupal\Core\Database\Query\TableSortExtender') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); @@ -104,7 +116,7 @@ class LoginHistoryController extends ControllerBase { foreach ($history as $entry) { $rows[] = array( $this->dateFormatter->format($entry->login, 'small'), - $users[$entry->uid]->getUsername(), + $users[$entry->uid]->getAccountName(), $entry->hostname, empty($entry->one_time) ? t('Regular login') : t('One-time login'), $entry->user_agent,