diff --git a/src/Authorization/AuthorizationDataMuxer.php b/src/Authorization/AuthorizationDataMuxer.php index 3cf2865d70dd4536c805b65f17935477524b87b0..ce6aed51a7859a4348b945903fa7fa48bc4d589b 100644 --- a/src/Authorization/AuthorizationDataMuxer.php +++ b/src/Authorization/AuthorizationDataMuxer.php @@ -112,7 +112,6 @@ class AuthorizationDataMuxer throw new AuthorizationException(sprintf('attribute \'%s\' undefined', $attributeName), AuthorizationException::ATTRIBUTE_UNDEFINED); } - $wasFound = false; $value = $defaultValue; foreach ($this->authorizationDataProviders as $authorizationDataProvider) { $availableAttributes = $this->getProviderAvailableAttributes($authorizationDataProvider); @@ -130,7 +129,7 @@ class AuthorizationDataMuxer $event = new GetAttributeEvent($this, $attributeName, $value, $userIdentifier); $event->setAttributeValue($value); - // Avoid endless recursions by only emitting an event for each attribtue only once + // Avoid endless recursions by only emitting an event for each attribute only once if (!in_array($attributeName, $this->attributeStack, true)) { array_push($this->attributeStack, $attributeName); $this->eventDispatcher->dispatch($event); diff --git a/src/Authorization/EventSubscriber/AbstractGetAttributeSubscriber.php b/src/Authorization/EventSubscriber/AbstractGetAttributeSubscriber.php index 2b30bd60e7b28834e4fbef93f22ff247f9472099..bb02caf1270b0198d71a9037ca5716dfd03a3ace 100644 --- a/src/Authorization/EventSubscriber/AbstractGetAttributeSubscriber.php +++ b/src/Authorization/EventSubscriber/AbstractGetAttributeSubscriber.php @@ -23,18 +23,19 @@ abstract class AbstractGetAttributeSubscriber implements EventSubscriberInterfac public function onGetAvailableAttributes(GetAvailableAttributesEvent $event) { - $event->addAttributes($this->getAvailableAttributes()); + $event->addAttributes($this->getNewAttributes()); } 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())); - } + + $event->setAttributeValue(in_array($attributeName, $this->getNewAttributes(), true) ? + $this->getNewAttributeValue($event->getUserIdentifier(), $attributeName, $event->getAttributeValue()) : + $this->updateExistingAttributeValue($event->getUserIdentifier(), $attributeName, $event->getAttributeValue()) + ); } finally { $this->event = null; } @@ -50,15 +51,25 @@ abstract class AbstractGetAttributeSubscriber implements EventSubscriberInterfac return $this->event->getAttribute($attributeName, $defaultValue); } + /** + * @param mixed|null $attributeValue The current attribute value + * + * @return mixed|null The updated attribute value + */ + protected function updateExistingAttributeValue(?string $userIdentifier, string $attributeName, $attributeValue) + { + return $attributeValue; + } + /* - * @return string[] + * @return string[] The array of new attribute names that this subscriber provides */ - abstract protected function getAvailableAttributes(): array; + abstract protected function getNewAttributes(): array; /** - * @param mixed|null $attributeValue + * @param mixed|null $defaultValue the default value if provided explicitly in the authorization expression, else null * - * @return mixed|null + * @return mixed|null the value for the new attribute with the given name for the given user */ - abstract protected function getUserAttributeValue(?string $userIdentifier, string $attributeName, $attributeValue); + abstract protected function getNewAttributeValue(?string $userIdentifier, string $attributeName, $defaultValue); }