DigiComp.FlowSessionLock/Classes/Aspects/ReadOnlyFilter.php

144 lines
4 KiB
PHP
Raw Normal View History

2021-08-26 15:05:37 +02:00
<?php
2022-05-02 09:56:10 +02:00
declare(strict_types=1);
2021-08-26 15:05:37 +02:00
namespace DigiComp\FlowSessionLock\Aspects;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\Builder\ClassNameIndex;
2022-03-15 09:32:42 +01:00
use Neos\Flow\Aop\Exception as NeosFlowAopException;
use Neos\Flow\Aop\Exception\InvalidPointcutExpressionException;
2021-08-26 15:05:37 +02:00
use Neos\Flow\Aop\Pointcut\PointcutFilterComposite;
use Neos\Flow\Aop\Pointcut\PointcutFilterInterface;
use Neos\Flow\Configuration\ConfigurationManager;
2022-03-15 09:32:42 +01:00
use Neos\Flow\Configuration\Exception\InvalidConfigurationTypeException;
2021-08-26 15:05:37 +02:00
use Neos\Flow\Security\Authorization\Privilege\Method\MethodTargetExpressionParser;
/**
* @Flow\Proxy(false)
* @Flow\Scope("singleton")
*/
class ReadOnlyFilter implements PointcutFilterInterface
{
2022-03-15 09:32:42 +01:00
/**
* @var ConfigurationManager
*/
2021-08-26 15:05:37 +02:00
protected ConfigurationManager $configurationManager;
2022-03-15 09:32:42 +01:00
/**
* @var MethodTargetExpressionParser
*/
2021-08-26 15:05:37 +02:00
protected MethodTargetExpressionParser $methodTargetExpressionParser;
/**
* @var PointcutFilterComposite[]
*/
2022-03-15 09:32:42 +01:00
protected ?array $pointcutFilterComposites = null;
2021-08-26 15:05:37 +02:00
2022-03-15 09:32:42 +01:00
/**
* @param ConfigurationManager $configurationManager
*/
2021-08-26 15:05:37 +02:00
public function injectConfigurationManager(ConfigurationManager $configurationManager): void
{
$this->configurationManager = $configurationManager;
}
2022-03-15 09:32:42 +01:00
/**
* @param MethodTargetExpressionParser $methodTargetExpressionParser
*/
2021-08-26 15:05:37 +02:00
public function injectMethodTargetExpressionParser(MethodTargetExpressionParser $methodTargetExpressionParser): void
{
$this->methodTargetExpressionParser = $methodTargetExpressionParser;
}
/**
* @inheritDoc
2022-03-15 09:32:42 +01:00
* @throws InvalidConfigurationTypeException
* @throws InvalidPointcutExpressionException
* @throws NeosFlowAopException
2021-08-26 15:05:37 +02:00
*/
public function matches($className, $methodName, $methodDeclaringClassName, $pointcutQueryIdentifier): bool
{
2022-03-15 09:32:42 +01:00
$this->buildPointcutFilters();
foreach ($this->pointcutFilterComposites as $pointcutFilterComposite) {
if (
$pointcutFilterComposite->matches(
$className,
$methodName,
$methodDeclaringClassName,
$pointcutQueryIdentifier
)
) {
return true;
2021-08-26 15:05:37 +02:00
}
}
2022-03-15 09:32:42 +01:00
return false;
2021-08-26 15:05:37 +02:00
}
/**
* @inheritDoc
*/
public function hasRuntimeEvaluationsDefinition(): bool
{
return false;
}
/**
* @inheritDoc
*/
public function getRuntimeEvaluationsDefinition(): array
{
return [];
}
/**
* @inheritDoc
2022-03-15 09:32:42 +01:00
* @throws InvalidConfigurationTypeException
* @throws InvalidPointcutExpressionException
* @throws NeosFlowAopException
2021-08-26 15:05:37 +02:00
*/
public function reduceTargetClassNames(ClassNameIndex $classNameIndex): ClassNameIndex
{
2022-03-15 09:32:42 +01:00
$this->buildPointcutFilters();
2021-08-26 15:05:37 +02:00
$result = new ClassNameIndex();
2022-03-15 09:32:42 +01:00
foreach ($this->pointcutFilterComposites as $pointcutFilterComposite) {
$result->applyUnion($pointcutFilterComposite->reduceTargetClassNames($classNameIndex));
2021-08-26 15:05:37 +02:00
}
2022-03-15 09:32:42 +01:00
2021-08-26 15:05:37 +02:00
return $result;
}
2022-03-15 09:32:42 +01:00
/**
* @throws InvalidConfigurationTypeException
* @throws InvalidPointcutExpressionException
* @throws NeosFlowAopException
*/
2021-08-26 15:05:37 +02:00
protected function buildPointcutFilters(): void
{
2022-03-15 09:32:42 +01:00
if ($this->pointcutFilterComposites !== null) {
return;
}
$this->pointcutFilterComposites = [];
foreach (
$this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
'DigiComp.FlowSessionLock.readOnlyExpressions'
) as $key => $pointcutExpression
) {
if ($pointcutExpression === null) {
2021-08-26 15:05:37 +02:00
continue;
}
2022-03-15 09:32:42 +01:00
$this->pointcutFilterComposites[] = $this->methodTargetExpressionParser->parse(
$pointcutExpression,
2021-08-26 15:05:37 +02:00
'Settings.yaml at "DigiComp.FlowSessionLock.readOnlyExpressions", key: "' . $key . '"'
);
}
}
}