Loading login_tracker.links.menu.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line login_tracker.set_admin: title: 'Login tracker settings' parent: user.admin_index description: 'Configure settings related to tracking of user logins.' weight: 0 route_name: login_tracker.settings_form login_tracker.module +24 −0 Original line number Diff line number Diff line Loading @@ -49,3 +49,27 @@ function login_tracker_user_login($account) { ->fields($fields) ->execute(); } /** * Implements hook_cron(). */ function login_tracker_cron() { $settings = \Drupal::state()->get('login_tracker_settings', []); if (!empty($settings['purge_interval'])) { $query = \Drupal::database()->delete('login_tracker'); $query->condition('login_timestamp', time() - $settings['purge_interval'], '<='); $results = $query->execute(); if ($results) { \Drupal::logger('login_tracker')->notice( t( '@logs records have been purged with success from table @table', ['@logs' => $results, '@table' => 'login_tracker'] ) ); } else { \Drupal::logger('login_tracker')->notice(t('Nothing to purge from table @table', ['@table' => 'login_tracker'])); } } } login_tracker.permissions.yml +3 −0 Original line number Diff line number Diff line excluded_from_login_tracking: title: 'Excluded from login tracking' description: 'Assign this permission to roles, and any logins by users of that role will not be tracked.' manage_login_tracker_settings: title: 'Manage settings for login tracking' description: 'Users with this permission will be able to manage settings for login tracking.' login_tracker.routing.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line login_tracker.settings_form: path: '/admin/config/login-tracker' defaults: _form: '\Drupal\login_tracker\Form\LoginTrackerSettings' _title: 'Login Tracker settings' requirements: _permission: 'manage_login_tracker_settings' src/Form/LoginTrackerSettings.php 0 → 100644 +96 −0 Original line number Diff line number Diff line <?php namespace Drupal\login_tracker\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\State\StateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Settings form for Logger settings. */ class LoginTrackerSettings extends FormBase { /** * The state store. * * @var \Drupal\Core\State\StateInterface */ protected $state; /** * Constructs a new SystemStateEdit object. * * @param \Drupal\Core\State\StateInterface $state * The state service. */ public function __construct(StateInterface $state) { $this->state = $state; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('state') ); } /** * {@inheritdoc} */ public function getFormId() { return 'login_tracker_settings_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $settings = $this->state->get('login_tracker_settings', []); $options = [ 0 => $this->t('Never'), 3600 => $this->t('1 hour'), 86400 => $this->t('1 day'), 604800 => $this->t('1 week'), 2678400 => $this->t('1 month'), 5097600 => $this->t('2 months'), 10364400 => $this->t('4 months'), 15634800 => $this->t('6 months'), ]; $form['purge_interval'] = [ '#type' => 'select', '#title' => $this->t('Periodically purge records older than'), '#description' => $this->t('Records older than this interval will be deleted via Cron.'), '#default_value' => $settings['purge_interval'] ?? 0, '#options' => $options, '#required' => TRUE, ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Save settings'), ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->messenger()->addMessage($this->t('The configuration has been saved'), 'status'); $values = $form_state->getValues(); $new_values['purge_interval'] = $values['purge_interval']; // Set State. $this->state->set('login_tracker_settings', $new_values); } } Loading
login_tracker.links.menu.yml 0 → 100644 +6 −0 Original line number Diff line number Diff line login_tracker.set_admin: title: 'Login tracker settings' parent: user.admin_index description: 'Configure settings related to tracking of user logins.' weight: 0 route_name: login_tracker.settings_form
login_tracker.module +24 −0 Original line number Diff line number Diff line Loading @@ -49,3 +49,27 @@ function login_tracker_user_login($account) { ->fields($fields) ->execute(); } /** * Implements hook_cron(). */ function login_tracker_cron() { $settings = \Drupal::state()->get('login_tracker_settings', []); if (!empty($settings['purge_interval'])) { $query = \Drupal::database()->delete('login_tracker'); $query->condition('login_timestamp', time() - $settings['purge_interval'], '<='); $results = $query->execute(); if ($results) { \Drupal::logger('login_tracker')->notice( t( '@logs records have been purged with success from table @table', ['@logs' => $results, '@table' => 'login_tracker'] ) ); } else { \Drupal::logger('login_tracker')->notice(t('Nothing to purge from table @table', ['@table' => 'login_tracker'])); } } }
login_tracker.permissions.yml +3 −0 Original line number Diff line number Diff line excluded_from_login_tracking: title: 'Excluded from login tracking' description: 'Assign this permission to roles, and any logins by users of that role will not be tracked.' manage_login_tracker_settings: title: 'Manage settings for login tracking' description: 'Users with this permission will be able to manage settings for login tracking.'
login_tracker.routing.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line login_tracker.settings_form: path: '/admin/config/login-tracker' defaults: _form: '\Drupal\login_tracker\Form\LoginTrackerSettings' _title: 'Login Tracker settings' requirements: _permission: 'manage_login_tracker_settings'
src/Form/LoginTrackerSettings.php 0 → 100644 +96 −0 Original line number Diff line number Diff line <?php namespace Drupal\login_tracker\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\State\StateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Settings form for Logger settings. */ class LoginTrackerSettings extends FormBase { /** * The state store. * * @var \Drupal\Core\State\StateInterface */ protected $state; /** * Constructs a new SystemStateEdit object. * * @param \Drupal\Core\State\StateInterface $state * The state service. */ public function __construct(StateInterface $state) { $this->state = $state; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('state') ); } /** * {@inheritdoc} */ public function getFormId() { return 'login_tracker_settings_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $settings = $this->state->get('login_tracker_settings', []); $options = [ 0 => $this->t('Never'), 3600 => $this->t('1 hour'), 86400 => $this->t('1 day'), 604800 => $this->t('1 week'), 2678400 => $this->t('1 month'), 5097600 => $this->t('2 months'), 10364400 => $this->t('4 months'), 15634800 => $this->t('6 months'), ]; $form['purge_interval'] = [ '#type' => 'select', '#title' => $this->t('Periodically purge records older than'), '#description' => $this->t('Records older than this interval will be deleted via Cron.'), '#default_value' => $settings['purge_interval'] ?? 0, '#options' => $options, '#required' => TRUE, ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Save settings'), ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->messenger()->addMessage($this->t('The configuration has been saved'), 'status'); $values = $form_state->getValues(); $new_values['purge_interval'] = $values['purge_interval']; // Set State. $this->state->set('login_tracker_settings', $new_values); } }