diff --git a/drush.services.yml b/drush.services.yml index f51add8..06403cb 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -1,6 +1,8 @@ services: privileged_users.command: class: \Drupal\user_audit\Commands\ListPrivilegedUsersCommands + arguments: + - '@entity_type.manager' tags: - { name: drush.command } failed_logins.command: @@ -10,4 +12,4 @@ services: verification_required.command: class: \Drupal\user_audit\Commands\RequireVerification tags: - - { name: drush.command } \ No newline at end of file + - { name: drush.command } diff --git a/src/Commands/ListPrivilegedUsersCommands.php b/src/Commands/ListPrivilegedUsersCommands.php index b7c5564..d1634f0 100644 --- a/src/Commands/ListPrivilegedUsersCommands.php +++ b/src/Commands/ListPrivilegedUsersCommands.php @@ -3,10 +3,10 @@ namespace Drupal\user_audit\Commands; use Drush\Commands\DrushCommands; -use Drupal\user\Entity\Role; use Consolidation\OutputFormatters\FormatterManager; use Consolidation\OutputFormatters\Options\FormatterOptions; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; +use Drupal\Core\Entity\EntityTypeManagerInterface; /** * Drush command to provide a list of privileged users. @@ -15,6 +15,22 @@ use Consolidation\OutputFormatters\StructuredData\RowsOfFields; */ class ListPrivilegedUsersCommands extends DrushCommands { + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The EntityTypeManager. + */ + public function __construct(EntityTypeManagerInterface $entity_type_manager) { + $this->entityTypeManager = $entity_type_manager; + } + /** * List privileged users. * @@ -66,7 +82,7 @@ class ListPrivilegedUsersCommands extends DrushCommands { */ private function getUsersByRoles(array $roles) { $users = []; - $user_storage = \Drupal::service('entity_type.manager')->getStorage('user'); + $user_storage = $this->entityTypeManager->getStorage('user'); $ids = $user_storage->getQuery() ->condition('roles', $roles, 'IN') ->execute(); @@ -89,7 +105,7 @@ class ListPrivilegedUsersCommands extends DrushCommands { */ private function listRolesByPermission($perm, FormatterManager $formatManager, FormatterOptions $options) { $perms = explode(',', $perm); - $allRoles = Role::loadMultiple(); + $allRoles = $this->entityTypeManager->getStorage('user_role')->loadMultiple(); foreach ($perms as $p) { $roles = []; $permission = strtolower(trim($p)); diff --git a/src/Plugin/SiteAuditCheck/PrivilegedUserCheck.php b/src/Plugin/SiteAuditCheck/PrivilegedUserCheck.php index e8b078a..fdb9432 100644 --- a/src/Plugin/SiteAuditCheck/PrivilegedUserCheck.php +++ b/src/Plugin/SiteAuditCheck/PrivilegedUserCheck.php @@ -3,7 +3,9 @@ namespace Drupal\user_audit\Plugin\SiteAuditCheck; use Drupal\site_audit\Plugin\SiteAuditCheckBase; -use Drupal\user\Entity\Role; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Entity\EntityManagerInterface; /** * Check to very that site requires email verification. @@ -15,7 +17,43 @@ use Drupal\user\Entity\Role; * report = "user_audit" * ) */ -class PrivilegedUserCheck extends SiteAuditCheckBase { +class PrivilegedUserCheck extends SiteAuditCheckBase implements ContainerFactoryPluginInterface { + + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * Constructs a privileged user role plugin + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager + * The entity manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_type_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->$entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager') + ); + } /** * {@inheritdoc} @@ -72,7 +110,7 @@ class PrivilegedUserCheck extends SiteAuditCheckBase { * Checks which roles can edit users. */ private function getRoles() { - $allRoles = Role::loadMultiple(); + $allRoles = $this->$entityTypeManager->getStorage('user_role')->loadMultiple(); $roles = []; foreach ($allRoles as $roleObj) { if ($roleObj->hasPermission('administer users')) {