Skip to content
Snippets Groups Projects
Commit 9d7f6aa4 authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

abstract event subscriber for get attribute events for more convenient handling

parent a744fdcc
No related branches found
No related tags found
No related merge requests found
Pipeline #212795 passed
......@@ -113,7 +113,7 @@ class AuthorizationDataMuxer
}
$wasFound = false;
$value = null;
$value = $defaultValue;
  • Reiter, Christoph :snake: @987FCF504483CBC8 ·
    Owner

    This changes the initial event value from the real one to the default one, and events can no longer see if an attribute is set or not, is that intended?

    I guess I should have written some tests :/

    Edited by Reiter, Christoph
  • Please register or sign in to reply
foreach ($this->authorizationDataProviders as $authorizationDataProvider) {
$availableAttributes = $this->getProviderAvailableAttributes($authorizationDataProvider);
if (!in_array($attributeName, $availableAttributes, true)) {
......@@ -124,15 +124,11 @@ class AuthorizationDataMuxer
continue;
}
$value = $userAttributes[$attributeName];
$wasFound = true;
break;
}
$event = new GetAttributeEvent($this, $attributeName, $userIdentifier);
if ($wasFound) {
$event->setValue($value);
}
$event = new GetAttributeEvent($this, $attributeName, $value, $userIdentifier);
$event->setAttributeValue($value);
// Avoid endless recursions by only emitting an event for each attribtue only once
if (!in_array($attributeName, $this->attributeStack, true)) {
......@@ -141,6 +137,6 @@ class AuthorizationDataMuxer
array_pop($this->attributeStack);
}
return $event->getValue($defaultValue);
return $event->getAttributeValue();
}
}
......@@ -29,24 +29,22 @@ class GetAttributeEvent extends Event
/**
* @var string
*/
private $name;
private $attributeName;
/**
* @var mixed
*/
private $value;
private $attributeValue;
/**
* @var bool
* @param mixed $attributeValue
*/
private $hasValue;
public function __construct(AuthorizationDataMuxer $mux, string $name, ?string $userIdentifier)
public function __construct(AuthorizationDataMuxer $mux, string $attributeName, $attributeValue, ?string $userIdentifier)
{
$this->mux = $mux;
$this->userIdentifier = $userIdentifier;
$this->name = $name;
$this->hasValue = false;
$this->attributeName = $attributeName;
$this->attributeValue = $attributeValue;
}
/**
......@@ -61,29 +59,27 @@ class GetAttributeEvent extends Event
public function getAttributeName(): string
{
return $this->name;
return $this->attributeName;
}
/**
* @param mixed $value
* @param mixed $attributeValue
*/
public function setValue($value): void
public function setAttributeValue($attributeValue): void
{
$this->value = $value;
$this->hasValue = true;
$this->attributeValue = $attributeValue;
}
/**
* @param mixed $default
*
* @return mixed
*/
public function getValue($default = null)
public function getAttributeValue()
{
if (!$this->hasValue) {
return $default;
return $this->attributeValue;
}
return $this->value;
public function getUserIdentifier(): ?string
{
return $this->userIdentifier;
}
}
......@@ -27,23 +27,18 @@ class GetAvailableAttributesEvent extends Event
}
/**
* @return string[]
* @param string[] $attributes
*/
public function getAttributes(): array
public function addAttributes(array $attributes): void
{
return $this->attributes;
$this->attributes = array_merge($this->attributes, $attributes);
}
/**
* @param string[] $attributes
* @return string[]
*/
public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}
public function addAttribute(string $name): void
public function getAttributes(): array
{
$this->attributes[] = $name;
return $this->attributes;
}
}
<?php
declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Authorization\EventSubscriber;
use Dbp\Relay\CoreBundle\Authorization\Event\GetAttributeEvent;
use Dbp\Relay\CoreBundle\Authorization\Event\GetAvailableAttributesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class AbstractGetAttributeSubscriber implements EventSubscriberInterface
{
/** @var GetAttributeEvent|null */
private $event;
public static function getSubscribedEvents(): array
{
return [
GetAvailableAttributesEvent::class => 'onGetAvailableAttributes',
GetAttributeEvent::class => 'onGetAttributeEvent',
];
}
public function onGetAvailableAttributes(GetAvailableAttributesEvent $event)
{
$event->addAttributes($this->getAvailableAttributes());
}
public function onGetAttributeEvent(GetAttributeEvent $event)
{
try {
$this->event = $event;
$attributeName = $event->getAttributeName();
if (in_array($attributeName, $this->getAvailableAttributes(), true)) {
$event->setAttributeValue($this->getUserAttributeValue($event->getUserIdentifier(), $attributeName, $event->getAttributeValue()));
}
} finally {
$this->event = null;
}
}
/**
* @param mixed|null $defaultValue
*
* @return mixed|null
*/
public function getAttribute(string $attributeName, $defaultValue = null)
{
return $this->event->getAttribute($attributeName, $defaultValue);
}
/*
* @return string[]
*/
abstract protected function getAvailableAttributes(): array;
/**
* @param mixed|null $attributeValue
*
* @return mixed|null
*/
abstract protected function getUserAttributeValue(?string $userIdentifier, string $attributeName, $attributeValue);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment