From 3c8230d1da6bb54c4a039f04f44c82dbb8ac6754 Mon Sep 17 00:00:00 2001 From: James Hall Date: Tue, 30 Jan 2024 12:19:10 +0000 Subject: [PATCH 1/2] Allow Drupal to alter attributes via event subscribers. --- src/DrupalHelper.php | 12 ++++-- src/Event/SetAttributesEvent.php | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/Event/SetAttributesEvent.php diff --git a/src/DrupalHelper.php b/src/DrupalHelper.php index 8488c55..460d43b 100644 --- a/src/DrupalHelper.php +++ b/src/DrupalHelper.php @@ -3,6 +3,7 @@ namespace SimpleSAML\Module\drupalauth; use Drupal\Core\DrupalKernel; +use SimpleSAML\Module\drupalauth\Event\SetAttributesEvent; use Symfony\Component\HttpFoundation\Request; class DrupalHelper @@ -38,7 +39,7 @@ public function getAttributes($drupaluser, $requested_attributes): array $forbiddenAttributes = $this->forbiddenAttributes; if (empty($requested_attributes)) { - return $this->getAllAttributes($drupaluser, $forbiddenAttributes); + $attributes = $this->getAllAttributes($drupaluser, $forbiddenAttributes); } else { foreach ($requested_attributes as $attribute) { $field_name = $attribute['field_name']; @@ -78,7 +79,10 @@ public function getAttributes($drupaluser, $requested_attributes): array } } } - + $event = new SetAttributesEvent($this, $drupaluser, $requested_attributes, $attributes); + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event_dispatcher->dispatch($event, SetAttributesEvent::EVENT_NAME); + $attributes = $event->getAttributes(); return $attributes; } @@ -116,7 +120,7 @@ protected function getAllAttributes($drupaluser, $forbiddenAttributes): array return $attributes; } - protected function getPropertyName($attribute_definition) + public function getPropertyName($attribute_definition) { $property_name = 'value'; if (!empty($attribute_definition['field_property'])) { @@ -126,7 +130,7 @@ protected function getPropertyName($attribute_definition) return $property_name; } - protected function getAttributeName($attribute_definition) + public function getAttributeName($attribute_definition) { if (!empty($attribute_definition['attribute_name'])) { return $attribute_definition['attribute_name']; diff --git a/src/Event/SetAttributesEvent.php b/src/Event/SetAttributesEvent.php new file mode 100644 index 0000000..6a1077e --- /dev/null +++ b/src/Event/SetAttributesEvent.php @@ -0,0 +1,71 @@ +drupalHelper; + } + + /** + * Get the requested attributes. + */ + public function getRequestedAttributes(): array + { + return $this->requestedAttributes; + } + + /** + * Get user who logged in. + */ + public function getUser(): UserInterface + { + return $this->user; + } + + /** + * Get the attributes set. + */ + public function getAttributes(): array + { + return $this->attributes; + } + + /** + * Set the attributes. + */ + public function setAttributes(array $attributes) + { + $this->attributes = $attributes; + } +} From 1104f7b6e6bda39c93331930a095eec96cb01dd9 Mon Sep 17 00:00:00 2001 From: James Hall Date: Tue, 6 Feb 2024 08:15:19 +0000 Subject: [PATCH 2/2] Adds psr-4 composer config. --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index 95b3153..4e578e7 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,11 @@ "phpunit/phpunit": "^9.0 | ^10.0", "squizlabs/php_codesniffer": "^3.0" }, + "autoload": { + "psr-4": { + "SimpleSAML\\Module\\drupalauth\\": "src/" + } + }, "autoload-dev": { "classmap": ["src/", "tests/"] },