Skip to content

Commit

Permalink
Replaced deprecated functions for D11 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Apr 1, 2024
1 parent 48e541a commit fe46e74
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion drush.services.yml
Original file line number Diff line number Diff line change
@@ -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 }
37 changes: 13 additions & 24 deletions src/Drush/Commands/StanfordSamlAuthCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,19 @@

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;

/**
* A Drush commandfile.
*/
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.
*
Expand All @@ -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');
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/Form/RoleMappingSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion src/Form/SamlAuthCreateUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions stanford_samlauth.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions stanford_samlauth.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Migrate data from stanford_ssp module.
*/

use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;

/**
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit fe46e74

Please sign in to comment.