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

improved handling of proxy api required functions and arguments;...

improved handling of proxy api required functions and arguments; authorization: rights and attributes config attributes now optional
parent 9202b365
Branches
Tags v0.1.54
No related merge requests found
Pipeline #202950 failed
...@@ -49,8 +49,8 @@ class UserAuthorizationChecker ...@@ -49,8 +49,8 @@ class UserAuthorizationChecker
public function setConfig(array $config) public function setConfig(array $config)
{ {
$this->loadExpressions($config[self::RIGHTS_CONFIG_ATTRIBUTE], $this->rightExpressions); $this->loadExpressions($config[self::RIGHTS_CONFIG_ATTRIBUTE] ?? [], $this->rightExpressions);
$this->loadExpressions($config[self::ATTRIBUTES_CONFIG_ATTRIBUTE], $this->attributeExpressions); $this->loadExpressions($config[self::ATTRIBUTES_CONFIG_ATTRIBUTE] ?? [], $this->attributeExpressions);
} }
public function init() public function init()
......
...@@ -4,18 +4,18 @@ declare(strict_types=1); ...@@ -4,18 +4,18 @@ declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\ProxyApi; namespace Dbp\Relay\CoreBundle\ProxyApi;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Exception; use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Response;
abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterface abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterface
{ {
protected const NAMESPACE = '';
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
{ {
return [ return [
ProxyDataEvent::NAME.'.'.static::NAMESPACE => 'onProxyDataEvent', ProxyDataEvent::NAME.'.'.static::getSubscribedNamespace() => 'onProxyDataEvent',
]; ];
} }
...@@ -30,10 +30,11 @@ abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterf ...@@ -30,10 +30,11 @@ abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterf
$arguments = $proxyData->getArguments(); $arguments = $proxyData->getArguments();
$returnValue = null; $returnValue = null;
if ($this->isFunctionDefined($functionName) === false) { $requiredFunctionArguments = static::getAvailableFunctionSignatures()[$functionName] ?? null;
throw new BadRequestException(sprintf('unknown function "%s" under namespace "%s"', $functionName, static::NAMESPACE)); if ($requiredFunctionArguments === null) {
} elseif ($this->areAllRequiredArgumentsDefined($functionName, $arguments) === false) { throw new BadRequestException(sprintf('unknown function "%s" under namespace "%s"', $functionName, static::getSubscribedNamespace()));
throw new BadRequestException(sprintf('incomplete argument list for function "%s" under namespace "%s"', $functionName, static::NAMESPACE)); } elseif ($this->areAllRequiredArgumentsDefined($requiredFunctionArguments, array_keys($arguments)) === false) {
throw new BadRequestException(sprintf('incomplete argument list for function "%s" under namespace "%s"', $functionName, static::getSubscribedNamespace()));
} }
try { try {
...@@ -45,9 +46,27 @@ abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterf ...@@ -45,9 +46,27 @@ abstract class AbstractProxyDataEventSubscriber implements EventSubscriberInterf
$proxyData->setData($returnValue); $proxyData->setData($returnValue);
} }
abstract protected function isFunctionDefined(string $functionName): bool; /**
* Must be overridden by deriving classes to indicate, which namespace they subscribe for.
*/
protected static function getSubscribedNamespace(): string
{
throw ApiError::withDetails(Response::HTTP_INTERNAL_SERVER_ERROR, 'proxy data event subscribers must subscribe for a namespace');
}
abstract protected function areAllRequiredArgumentsDefined(string $functionName, array $arguments): bool; /**
* Must be overridden by deriving classes to indicate, which functions are available and which mandatory arguments they have. The format is a follows:
* ['func1' => ['arg1', 'arg2'], 'func2' => []].
*/
protected static function getAvailableFunctionSignatures(): array
{
throw ApiError::withDetails(Response::HTTP_INTERNAL_SERVER_ERROR, 'proxy data event subscribers must provide a list of available function signatures');
}
abstract protected function callFunction(string $functionName, array $arguments); abstract protected function callFunction(string $functionName, array $arguments);
private function areAllRequiredArgumentsDefined(array $requiredFunctionArguments, array $arguments): bool
{
return count(array_intersect($requiredFunctionArguments, $arguments)) === count($requiredFunctionArguments);
}
} }
...@@ -38,18 +38,17 @@ class ProxyDataEventSubscriber extends AbstractProxyDataEventSubscriber ...@@ -38,18 +38,17 @@ class ProxyDataEventSubscriber extends AbstractProxyDataEventSubscriber
return self::$isCurrentlyActive; return self::$isCurrentlyActive;
} }
protected function isFunctionDefined(string $functionName): bool protected static function getSubscribedNamespace(): string
{ {
return return self::NAMESPACE;
$functionName === self::GET_AVAILABLE_ATTRIBUTES_FUNCTION_NAME ||
$functionName === self::GET_USER_ATTRIBUTES_FUNCTION_NAME;
} }
protected function areAllRequiredArgumentsDefined(string $functionName, array $arguments): bool protected static function getAvailableFunctionSignatures(): array
{ {
return return [
$functionName !== self::GET_USER_ATTRIBUTES_FUNCTION_NAME || self::GET_AVAILABLE_ATTRIBUTES_FUNCTION_NAME => [],
!Tools::isNullOrEmpty($arguments[self::USER_ID_PARAMETER_NAME] ?? null); self::GET_USER_ATTRIBUTES_FUNCTION_NAME => [self::USER_ID_PARAMETER_NAME],
];
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment