diff -u b/src/Plugin/monitoring/SensorPlugin/UserIntegritySensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/UserIntegritySensorPlugin.php --- b/src/Plugin/monitoring/SensorPlugin/UserIntegritySensorPlugin.php +++ b/src/Plugin/monitoring/SensorPlugin/UserIntegritySensorPlugin.php @@ -34,6 +34,13 @@ protected $configurableValueType = FALSE; /** + * The max number of users to list in the verbose output. + * + * @var int + */ + protected $list_size =500; + + /** * {@inheritdoc} */ public function runSensor(SensorResultInterface $sensor_result) { @@ -114,22 +121,22 @@ return []; } + $query = \Drupal::entityQuery('user') + ->sort('access', 'DESC') + ->range(0, $this->list_size); + // The authenticated role is not persisted and it could have restrict access // so we load every user. if (in_array('authenticated', $role_ids)) { - - // The conditions apply to all users. - $users = User::loadMultiple(); - - // We don't need the default user with uid = 0. - unset($users[0]); - return $users; + $uids = $query->condition('uid', '0', '<>') + ->execute(); + } + else { + // Load all users with the roles. + $uids = $query->condition('roles', $role_ids, 'IN') + ->execute(); } - // Load all users with the roles. - $uids = \Drupal::entityQuery('user') - ->condition('roles', $role_ids, 'IN') - ->execute(); return User::loadMultiple($uids); } @@ -211,6 +218,7 @@ $processed_users[$id]['mail'] = $user->getEmail(); $processed_users[$id]['password'] = hash('sha256', $user->getPassword()); $processed_users[$id]['changed'] = $user->getChangedTime(); + $processed_users[$id]['last_accessed'] = ($user->getLastAccessedTime() != 0) ? $user->getLastAccessedTime() : null; } return $processed_users; } @@ -234,6 +242,7 @@ // Create rows for new users. foreach ($new_users_id as $id) { $time_stamp = $current_users[$id]['changed']; + $last_accessed = $current_users[$id]['last_accessed']; $user_name = array( '#theme' => 'username', '#account' => User::load($id), @@ -244,6 +253,7 @@ $row['expected_value'] = ['#markup' => '']; $row['time'] = ['#markup' => date("Y-m-d H:i:s", $time_stamp)]; + $row['last_accessed'] = ['#markup' => isset($last_accessed) ? date("Y-m-d H:i:s", $last_accessed) : 0]; $rows[] = $row; } @@ -254,6 +264,7 @@ $changes = $this->getUserChanges($current_users[$id], $expected_users[$id]); foreach ($changes as $key => $value) { $time_stamp = $current_users[$id]['changed']; + $last_accessed = $current_users[$id]['last_accessed']; $user_name = array( '#theme' => 'username', '#account' => User::load($id), @@ -264,10 +275,17 @@ $row['expected_value'] = ['#markup' => $value['expected_value']]; $row['time'] = ['#markup' => date("Y-m-d H:i:s", $time_stamp)]; + $row['last_accessed'] = ['#markup' => date("Y-m-d H:i:s", $last_accessed)]; $rows[] = $row; } } + // Show query. + $output['message'] = array( + '#type' => 'item', + '#title' => t('Table shows new and changed users who have privileged access.'), + ); + if (count($rows) > 0) { $header = []; $header['user'] = t('User'); @@ -275,12 +293,19 @@ $header['current_value'] = t('Current value'); $header['expected_value'] = t('Expected value'); $header['time'] = t('Changed on'); + $header['last_accessed'] = t('Last accessed'); $output['users'] = array( '#type' => 'table', '#header' => $header, ) + $rows; } + else { + $output['result'] = [ + '#type' => 'item', + '#markup' => t('No matching users were found.'), + ]; + } return $output; } diff -u b/src/Tests/MonitoringCoreWebTest.php b/src/Tests/MonitoringCoreWebTest.php --- b/src/Tests/MonitoringCoreWebTest.php +++ b/src/Tests/MonitoringCoreWebTest.php @@ -205,12 +205,11 @@ // Create an authenticated user and test that the sensor counter increments. $test_user_third = $this->drupalCreateUser(array(), 'test_user_3'); - $this->drupalLogin($test_user_third); \Drupal::keyValue('monitoring.users')->deleteAll(); $result = $this->runSensor('user_integrity'); $this->assertEqual($result->getMessage(), '4 privileged user(s), Anonymous user has privileged access, Authenticated user has privileged access, Self registration possible.'); - $test_user_third->setUsername('changed'); + $test_user_third->setUsername('changed2'); $test_user_third->save(); // Check sensor message for user changes.