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 @@ -22,7 +22,7 @@ * @SensorPlugin( * id = "user_integrity", * label = @Translation("Privileged user integrity"), - * description = @Translation("Monitors name and e-mail changes of users with access to restricted permissions."), + * description = @Translation("Monitors name and e-mail changes of users with access to restricted permissions. Checks if authenticated or anonymous users have privileged access."), * addable = FALSE * ) */ @@ -78,16 +78,25 @@ // Check anonymous and authenticated users with restricted permissions and // show a message. $user_register = \Drupal::config('user.settings')->get('register'); + + // Check if authenticated or anonymous users have restrict access perms. $role_ids = array_intersect($role_ids, ['authenticated', 'anonymous']); + $role_labels = []; foreach (Role::loadMultiple($role_ids) as $role) { - if ($role->id() == 'authenticated' && $user_register == USER_REGISTER_ADMINISTRATORS_ONLY) { - $sensor_result->addStatusMessage($role->label() . ' has privileged access but not for registration'); - $sensor_result->setStatus(SensorResultInterface::STATUS_WARNING); - } - else { - $sensor_result->addStatusMessage($role->label() . ' has privileged access'); - $sensor_result->setStatus(SensorResultInterface::STATUS_CRITICAL); - } + $role_labels[$role->id()] = $role->label(); + $sensor_result->addStatusMessage($role->label() . ' has privileged access'); + $sensor_result->setStatus(SensorResultInterface::STATUS_WARNING); + } + + // Further escalate if the restricted access is for anonymous. + if (!empty($role_labels['anonymous'])) { + $sensor_result->setStatus(SensorResultInterface::STATUS_CRITICAL); + } + + // Check if self registration is possible. + if (!empty($role_labels['authenticated']) && $user_register != USER_REGISTER_ADMINISTRATORS_ONLY) { + $sensor_result->addStatusMessage('Self registration possible.'); + $sensor_result->setStatus(SensorResultInterface::STATUS_CRITICAL); } } @@ -104,6 +113,20 @@ if (!$role_ids) { return []; } + + // 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; + } + + // Load all users with the roles. $uids = \Drupal::entityQuery('user') ->condition('roles', $role_ids, 'IN') ->execute(); diff -u b/src/Tests/MonitoringCoreWebTest.php b/src/Tests/MonitoringCoreWebTest.php --- b/src/Tests/MonitoringCoreWebTest.php +++ b/src/Tests/MonitoringCoreWebTest.php @@ -185,20 +185,45 @@ // Add permissions to authenticated user with no privilege of registration. \Drupal::configFactory()->getEditable('user.settings')->set('register', 'admin_only')->save(); user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, array('administer account settings')); + \Drupal::keyValue('monitoring.users')->deleteAll(); $result = $this->runSensor('user_integrity'); $this->assertTrue($result->isWarning()); - $this->assertEqual($result->getMessage(), '2 privileged user(s), Authenticated user has privileged access but not for registration'); + + // Count users included admin. + $this->assertEqual($result->getMessage(), '3 privileged user(s), Authenticated user has privileged access'); // Add permissions to anonymous user and check the sensor. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array('administer account settings')); $result = $this->runSensor('user_integrity'); - $this->assertEqual($result->getMessage(), '2 privileged user(s), Anonymous user has privileged access, Authenticated user has privileged access but not for registration'); + $this->assertEqual($result->getMessage(), '3 privileged user(s), Anonymous user has privileged access, Authenticated user has privileged access'); - // Authenticated user with privilege of registration. + // Authenticated user with privilege of register. \Drupal::configFactory()->getEditable('user.settings')->set('register', 'visitors')->save(); $result = $this->runSensor('user_integrity'); $this->assertTrue($result->isCritical()); - $this->assertEqual($result->getMessage(), '2 privileged user(s), Anonymous user has privileged access, Authenticated user has privileged access'); + $this->assertEqual($result->getMessage(), '3 privileged user(s), Anonymous user has privileged access, Authenticated user has privileged access, Self registration possible.'); + + // 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->save(); + + // Check sensor message for user changes. + $result = $this->runSensor('user_integrity'); + $this->assertEqual($result->getMessage(), '4 privileged user(s), 1 changed user(s), Anonymous user has privileged access, Authenticated user has privileged access, Self registration possible.'); + + // Check sensor message with permissions revoked. + user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, array('administer account settings')); + user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, array('administer account settings')); + \Drupal::keyValue('monitoring.users')->deleteAll(); + $result = $this->runSensor('user_integrity'); + $this->assertEqual($result->getMessage(), '2 privileged user(s)'); + } /** only in patch2: unchanged: --- a/config/install/monitoring.sensor_config.user_integrity.yml +++ b/config/install/monitoring.sensor_config.user_integrity.yml @@ -1,6 +1,6 @@ id: user_integrity label: 'Privileged user integrity' -description: 'Monitors name and e-mail changes of users with access to restricted permissions.' +description: 'Monitors name and e-mail changes of users with access to restricted permissions. Checks if authenticated or anonymous users have privileged access' category: 'User activity' plugin_id: user_integrity value_type: 'no_value'