public function UserIntegritySensorPlugin::resultVerbose in Monitoring 8
Provide additional info about sensor call.
This method is only executed on request. It is guaranteed that runSensor() is executed before this method.
Parameters
\Drupal\monitoring\Result\SensorResultInterface $result: Sensor result.
Return value
array Sensor call verbose info as render array.
Overrides ExtendedInfoSensorPluginInterface::resultVerbose
File
- src/
Plugin/ monitoring/ SensorPlugin/ UserIntegritySensorPlugin.php, line 244 - Contains \Drupal\monitoring\Plugin\monitoring\SensorPlugin\UserIntegritySensorPlugin.
Class
- UserIntegritySensorPlugin
- Monitors user data changes.
Namespace
Drupal\monitoring\Plugin\monitoring\SensorPluginCode
public function resultVerbose(SensorResultInterface $result) {
$output = [];
// Load all the old user data.
$expected_users = \Drupal::keyValue('monitoring.users')
->getAll();
// Get available roles with restricted permission.
$role_ids = $this
->getRestrictedRoles();
// Process the current user data.
$current_users = $this
->processUsers($this
->loadCurrentUsers($role_ids));
$new_users_id = array_diff(array_keys($current_users), array_keys($expected_users));
$deleted_users = array_diff_key($expected_users, $current_users);
// Verbose output for new users.
$rows = [];
foreach ($new_users_id as $id) {
$time_stamp = $current_users[$id]['created'];
$last_accessed = $current_users[$id]['last_accessed'];
// Do this for all, and delete drupal render.
$user_name = [
'data' => [
'#theme' => 'username',
'#account' => User::load($id),
],
];
$rows[] = [
'user' => $user_name,
'roles' => [
'data' => [
'#markup' => $current_users[$id]['roles'],
],
],
'created' => [
'data' => [
'#markup' => \Drupal::service('date.formatter')
->format($time_stamp, 'short'),
],
],
'last_accessed' => [
'data' => [
'#markup' => $last_accessed != 0 ? \Drupal::service('date.formatter')
->format($last_accessed, 'short') : t('never'),
],
],
];
}
if (count($rows) > 0) {
$header = [
'user' => t('User'),
'roles' => t('Roles'),
'created' => t('Created'),
'last_accessed' => t('Last accessed'),
];
$output['new_table'] = [
'#type' => 'verbose_table_result',
'#title' => t('New users with privileged access'),
'#header' => $header,
'#rows' => $rows,
];
}
// Verbose output for users with changes.
$rows = [];
$old_user_ids = array_keys($expected_users);
foreach ($old_user_ids as $id) {
$changes = [];
if (isset($current_users[$id])) {
$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 = [
'data' => [
'#theme' => 'username',
'#account' => User::load($id),
],
];
$rows[] = [
'user' => $user_name,
'field' => [
'data' => [
'#markup' => $key,
],
],
'current_value' => [
'data' => [
'#markup' => $value['current_value'],
],
],
'expected_value' => [
'data' => [
'#markup' => $value['expected_value'],
],
],
'changed' => [
'data' => [
'#markup' => \Drupal::service('date.formatter')
->format($time_stamp, 'short'),
],
],
'last_accessed' => [
'data' => [
'#markup' => $last_accessed != 0 ? \Drupal::service('date.formatter')
->format($last_accessed, 'short') : t('never'),
],
],
];
}
}
if (count($rows) > 0) {
$header = [
'user' => t('User'),
'Field' => t('Field'),
'current_value' => t('Current value'),
'expected_value' => t('Expected value'),
'changed' => t('Changed'),
'last_accessed' => t('Last accessed'),
];
$output['changes_table'] = [
'#type' => 'verbose_table_result',
'#title' => t('Changed users with privileged access'),
'#header' => $header,
'#rows' => $rows,
];
}
// Verbose output for all privileged users.
$rows = [];
foreach ($current_users as $user) {
$created = $user['created'];
$user_name = [
'data' => [
'#theme' => 'username',
'#account' => User::load($user['id']),
],
];
$rows[] = [
'user' => $user_name,
'roles' => [
'data' => [
'#markup' => $user['roles'],
],
],
'created' => [
'data' => [
'#markup' => \Drupal::service('date.formatter')
->format($created, 'short'),
],
],
'last_accessed' => [
'data' => [
'#markup' => $user['last_accessed'] != 0 ? \Drupal::service('date.formatter')
->format($user['last_accessed'], 'short') : t('never'),
],
],
];
}
if (count($rows) > 0) {
$header = [
'user' => t('User'),
'roles' => t('Roles'),
'created' => t('Created'),
'last_accessed' => t('Last accessed'),
];
$output['users_privileged'] = [
'#type' => 'verbose_table_result',
'#title' => t('All users with privileged access'),
'#header' => $header,
'#rows' => $rows,
];
}
// Verbose output for deleted users.
$rows = [];
foreach ($deleted_users as $user) {
$rows[] = [
'user' => [
'data' => [
'#markup' => $user['name'],
],
],
'roles' => [
'data' => [
'#markup' => $user['roles'],
],
],
'created' => [
'data' => [
'#markup' => \Drupal::service('date.formatter')
->format($user['created'], 'short'),
],
],
'last_accessed' => [
'data' => [
'#markup' => $user['last_accessed'] != 0 ? \Drupal::service('date.formatter')
->format($user['last_accessed'], 'short') : t('never'),
],
],
];
}
if (count($rows) > 0) {
$header = [
'user' => t('User'),
'roles' => t('Roles'),
'created' => t('Created'),
'last_accessed' => t('Last accessed'),
];
$output['deleted_users'] = [
'#type' => 'verbose_table_result',
'#title' => t('Deleted users with privileged access'),
'#header' => $header,
'#rows' => $rows,
];
}
// Show roles list with the permissions that are restricted for each.
$roles_list = [];
foreach (Role::loadMultiple($role_ids) as $role) {
if (!$role
->isAdmin()) {
$restricted_permissions = array_intersect($this->restrictedPermissions, $role
->getPermissions());
$roles_list[] = $role
->label() . ': ' . implode(", ", $restricted_permissions);
}
}
$output['roles_list'] = [
'#type' => 'fieldset',
'#title' => t('List of roles with restricted permissions'),
[
'#type' => 'item',
'#markup' => !empty($roles_list) ? implode('<br>', $roles_list) : t('None'),
],
];
return $output;
}