diff --git a/drush.services.yml b/drush.services.yml index f34646a..c74521e 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -1,6 +1,6 @@ services: stanford_samlauth.commands: class: Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthCommands - arguments: [ '@externalauth.authmap', '@form_builder', '@config.factory' ] + arguments: [ '@externalauth.authmap', '@form_builder', '@config.factory', '@entity_type.manager' ] tags: - { name: drush.command } diff --git a/src/Drush/Commands/StanfordSamlAuthCommands.php b/src/Drush/Commands/StanfordSamlAuthCommands.php index 4c3ba7d..df6df79 100644 --- a/src/Drush/Commands/StanfordSamlAuthCommands.php +++ b/src/Drush/Commands/StanfordSamlAuthCommands.php @@ -4,10 +4,12 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\externalauth\AuthmapInterface; use Drupal\stanford_samlauth\Form\SamlAuthCreateUserForm; +use Drupal\user\RoleInterface; use Drush\Commands\DrushCommands; /** @@ -15,20 +17,6 @@ */ class StanfordSamlAuthCommands extends DrushCommands { - /** - * External authmap service. - * - * @var \Drupal\externalauth\AuthmapInterface - */ - protected $authmap; - - /** - * Form builder service. - * - * @var \Drupal\Core\Form\FormBuilderInterface - */ - protected $formBuilder; - /** * Config object of SAML settings. * @@ -46,16 +34,16 @@ class StanfordSamlAuthCommands extends DrushCommands { /** * StanfordSspCommands constructor. * - * @param \Drupal\externalauth\AuthmapInterface $auth_map + * @param \Drupal\externalauth\AuthmapInterface $authMap * Authmap service. - * @param \Drupal\Core\Form\FormBuilderInterface $form_builder + * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder * Form builder service. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * Config factory service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * Entity type manager service. */ - public function __construct(AuthmapInterface $auth_map, FormBuilderInterface $form_builder, ConfigFactoryInterface $config_factory) { - $this->authmap = $auth_map; - $this->formBuilder = $form_builder; + public function __construct(protected AuthmapInterface $authMap, protected FormBuilderInterface $formBuilder, ConfigFactoryInterface $config_factory, protected EntityTypeManagerInterface $entityTypeManager) { $this->samlConfig = $config_factory->getEditable('samlauth.authentication'); $this->stanfordConfig = $config_factory->getEditable('stanford_samlauth.settings'); } @@ -73,10 +61,9 @@ public function __construct(AuthmapInterface $auth_map, FormBuilderInterface $fo */ public function entitlementRole(string $entitlement, string $role_id) { $role_id = Html::escape($role_id); - $existing_roles = user_roles(TRUE); - + $role = $this->entityTypeManager->getStorage('user_role')->load($role_id); // Validate the role exists. - if (!isset($existing_roles[$role_id])) { + if (!$role) { $this->logger->error(dt('No role exists with the ID "%role_id".', ['%role_id' => $role_id])); return; } @@ -133,8 +120,10 @@ public function addUser(string $sunetid, array $options = [ // Build the roles array and make sure to only add the ones that exist. $options['roles'] = array_filter(explode(',', $options['roles'] ?: '')); - $existing_roles = array_keys(user_roles(TRUE)); - $options['roles'] = array_intersect($existing_roles, $options['roles']); + $existing_roles = $this->entityTypeManager->getStorage('user_role')->loadMultiple(); + unset($existing_roles[RoleInterface::AUTHENTICATED_ID], $existing_roles[RoleInterface::ANONYMOUS_ID]); + + $options['roles'] = array_intersect(array_keys($existing_roles), $options['roles']); $options['sunetid'] = $sunetid; $options = array_filter($options); diff --git a/src/Form/RoleMappingSettingsForm.php b/src/Form/RoleMappingSettingsForm.php index fbbfee7..2b26313 100644 --- a/src/Form/RoleMappingSettingsForm.php +++ b/src/Form/RoleMappingSettingsForm.php @@ -84,14 +84,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#header' => $this->getRoleHeaders(), '#attributes' => ['id' => 'role-mapping-table'], ]; - + $roles = $this->entityTypeManager->getStorage('user_role')->loadMultiple(); + unset($roles[RoleInterface::AUTHENTICATED_ID], $roles[RoleInterface::ANONYMOUS_ID]); $form['user_info']['role_mapping']['add']['role'] = [ '#type' => 'select', '#title' => $this->t('Add Role'), - '#options' => user_role_names(TRUE), + '#options' => array_map(fn(RoleInterface $role) => $role->label(), $roles), ]; - unset($form['user_info']['role_mapping']['add']['role']['#options'][RoleInterface::AUTHENTICATED_ID]); - unset($form['user_info']['role_mapping']['add']['role']['#options'][RoleInterface::ANONYMOUS_ID]); $form['user_info']['role_mapping']['add']['attribute'] = [ '#type' => 'textfield', diff --git a/src/Form/SamlAuthCreateUserForm.php b/src/Form/SamlAuthCreateUserForm.php index b98273d..a324d3a 100644 --- a/src/Form/SamlAuthCreateUserForm.php +++ b/src/Form/SamlAuthCreateUserForm.php @@ -8,7 +8,9 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Password\PasswordGeneratorInterface; use Drupal\externalauth\AuthmapInterface; +use Drupal\user\Entity\Role; use Drupal\user\Entity\User; +use Drupal\user\RoleInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -123,7 +125,9 @@ protected static function getAvailableRoles(): array { $role_delegation = \Drupal::service('delegatable_roles'); return $role_delegation->getAssignableRoles(\Drupal::currentUser()); } - return user_role_names(TRUE); + $roles = Role::loadMultiple(); + unset($roles[RoleInterface::AUTHENTICATED_ID], $roles[RoleInterface::ANONYMOUS_ID]); + return array_map(fn(RoleInterface $role) => $role->label(), $roles); } /** diff --git a/stanford_samlauth.info.yml b/stanford_samlauth.info.yml index 633aede..ccc3ff0 100644 --- a/stanford_samlauth.info.yml +++ b/stanford_samlauth.info.yml @@ -2,8 +2,8 @@ name: Stanford SAML Authentication type: module description: Adds enhancements to SAML Authentication module. package: Stanford -core_version_requirement: ^9 || ^10 -version: 1.0.4 +core_version_requirement: ^9 || ^10 || ^11 +version: 1.0.5 dependencies: - drupal:path_alias - autologout:autologout diff --git a/stanford_samlauth.install b/stanford_samlauth.install index bb88f2f..57db61d 100644 --- a/stanford_samlauth.install +++ b/stanford_samlauth.install @@ -5,6 +5,7 @@ * Migrate data from stanford_ssp module. */ +use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; /** @@ -33,9 +34,9 @@ function stanford_samlauth_install() { return; } - $roles = user_roles(TRUE); - unset($roles[RoleInterface::AUTHENTICATED_ID]); - $role_ids = array_combine(array_keys($roles), array_keys($roles)); + $role_ids = array_keys(Role::loadMultiple()); + $role_ids = array_combine($role_ids, $role_ids); + unset($role_ids[RoleInterface::AUTHENTICATED_ID]); // Set the SamlAuth settings. $samlauth_config diff --git a/tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php b/tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php index 26a4db7..f8ad478 100644 --- a/tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php +++ b/tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php @@ -38,7 +38,9 @@ public function setup(): void { $authmap = \Drupal::service('externalauth.authmap'); $form_builder = \Drupal::formBuilder(); $config_factory = \Drupal::configFactory(); - $this->commandObject = new StanfordSamlAuthCommands($authmap, $form_builder, $config_factory); + $entity_type_manager = \Drupal::entityTypeManager(); + + $this->commandObject = new StanfordSamlAuthCommands($authmap, $form_builder, $config_factory, $entity_type_manager); $this->commandObject->setLogger(\Drupal::logger('stanford_samlauth')); $this->commandObject->setOutput($this->createMock(OutputInterface::class)); diff --git a/tests/src/Kernel/EventSubscriber/StanfordSamlAuthSubscriberTest.php b/tests/src/Kernel/EventSubscriber/StanfordSamlAuthSubscriberTest.php index 586a5a9..d1b8ac2 100644 --- a/tests/src/Kernel/EventSubscriber/StanfordSamlAuthSubscriberTest.php +++ b/tests/src/Kernel/EventSubscriber/StanfordSamlAuthSubscriberTest.php @@ -172,7 +172,7 @@ public function testKernelRequest() { $kernel = $this->container->get('kernel'); $request = Request::create('/admin/people/create'); - $event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + $event = new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST); \Drupal::service('event_dispatcher') ->dispatch($event, KernelEvents::REQUEST);