Skip to content

Commit

Permalink
Reroll of drupalauth#95
Browse files Browse the repository at this point in the history
DrupalHelper.php has shifted location.
  • Loading branch information
stesi561 committed May 20, 2024
1 parent 57d623a commit c647ee2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"phpunit/phpunit": "^5|^6|^7|^8|^9",
"squizlabs/php_codesniffer": "^2.0.0|^3.0.0"
},
"autoload": {
"psr-4": {
"SimpleSAML\\Module\\drupalauth\\": "src/"
}
},
"autoload-dev": {
"classmap": ["lib/", "tests/lib/"]
},
Expand Down
12 changes: 8 additions & 4 deletions lib/DrupalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace SimpleSAML\Module\drupalauth;

use Drupal\Core\DrupalKernel;
use SimpleSAML\Module\drupalauth\Event\SetAttributesEvent;
use Symfony\Component\HttpFoundation\Request;

class DrupalHelper
Expand Down Expand Up @@ -40,7 +41,7 @@ public function getAttributes($drupaluser, $requested_attributes)
$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'];
Expand Down Expand Up @@ -80,7 +81,10 @@ public function getAttributes($drupaluser, $requested_attributes)
}
}
}

$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;
}

Expand Down Expand Up @@ -118,7 +122,7 @@ protected function getAllAttributes($drupaluser, $forbiddenAttributes)
return $attributes;
}

protected function getPropertyName($attribute_definition)
public function getPropertyName($attribute_definition)
{
$property_name = 'value';
if (!empty($attribute_definition['field_property'])) {
Expand All @@ -128,7 +132,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'];
Expand Down
71 changes: 71 additions & 0 deletions src/Event/SetAttributesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace SimpleSAML\Module\drupalauth\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\user\UserInterface;
use SimpleSAML\Module\drupalauth\DrupalHelper;

/**
* Event that is fired after SAML attributes are set
*/
class SetAttributesEvent extends Event
{
public const EVENT_NAME = 'simplesamlphp_drupalauth_set_attributes';


/**
* Contstruct the event.
*
* @param \SimpleSAML\Module\drupalauth\DrupalHelper $drupalHelper
* @param \Drupal\user\UserInterface $user
* @param array $attributes
*/
public function __construct(
protected readonly DrupalHelper $drupalHelper,
protected readonly UserInterface $user,
protected readonly array $requestedAttributes,
protected array $attributes
) {
}

/**
* Get the drupal helper.
*/
public function getDrupalHelper(): DrupalHelper
{
return $this->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;
}
}

0 comments on commit c647ee2

Please sign in to comment.