Skip to content

Reroll of PR 95 #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 1.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}