diff --git a/config/v14/typo3-142.php b/config/v14/typo3-142.php index 993370006..1bac5bb58 100644 --- a/config/v14/typo3-142.php +++ b/config/v14/typo3-142.php @@ -7,6 +7,7 @@ use Ssch\TYPO3Rector\TYPO314\v2\MigrateAssetCollectorUseNonceOptionToCspRector; use Ssch\TYPO3Rector\TYPO314\v2\MigrateFormEngineAdditionalHiddenFieldsRector; use Ssch\TYPO3Rector\TYPO314\v2\MigrateTcaOptionAllowedRecordTypesForPageTypesRector; +use Ssch\TYPO3Rector\TYPO314\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->import(__DIR__ . '/../config.php'); @@ -14,4 +15,5 @@ $rectorConfig->rule(MigrateTcaOptionAllowedRecordTypesForPageTypesRector::class); $rectorConfig->rule(MigrateFormEngineAdditionalHiddenFieldsRector::class); $rectorConfig->rule(AddRequestArgumentToPageRendererMethodsRector::class); + $rectorConfig->rule(MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector::class); }; diff --git a/rules/TYPO314/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector.php b/rules/TYPO314/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector.php new file mode 100644 index 000000000..80d8bd120 --- /dev/null +++ b/rules/TYPO314/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector.php @@ -0,0 +1,187 @@ + \TYPO3\CMS\Core\Session\Backend\RedisSessionBackend::class, + 'options' => [ + 'database' => 0, + 'hostname' => 'redis', + 'port' => 6379, + 'password' => [ + 'user' => 'redis', + 'pass' => 'redis', + ], + ], +]; +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [ + 'backend' => \TYPO3\CMS\Core\Session\Backend\RedisSessionBackend::class, + 'options' => [ + 'database' => 0, + 'hostname' => 'redis', + 'port' => 6379, + 'password' => 'redis', + 'username' => 'redis', + ], +]; +CODE_SAMPLE + ), + ]); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Assign::class]; + } + + /** + * @param Assign $node + */ + public function refactor(Node $node): ?Node + { + if (! $node->var instanceof ArrayDimFetch) { + return null; + } + + if (! $node->expr instanceof Array_) { + return null; + } + + $path = $this->resolveSessionConfigurationPath($node->var); + if ($path === null) { + return null; + } + + $optionsArray = $node->expr; + if ($path === '') { + $optionsItem = $this->findArrayItem($node->expr, 'options'); + if (! $optionsItem instanceof ArrayItem) { + return null; + } + + $optionsArray = $optionsItem->value; + } + + if (! $optionsArray instanceof Array_) { + return null; + } + + if (! $this->migratePassword($optionsArray)) { + return null; + } + + return $node; + } + + private function migratePassword(Array_ $optionsArray): bool + { + $passwordItem = $this->findArrayItem($optionsArray, 'password'); + if (! $passwordItem instanceof ArrayItem || ! $passwordItem->value instanceof Array_) { + return false; + } + + $userItem = $this->findArrayItem($passwordItem->value, 'user'); + $passItem = $this->findArrayItem($passwordItem->value, 'pass'); + if (! $passItem instanceof ArrayItem) { + return false; + } + + $passwordItem->value = $passItem->value; + + if (! $userItem instanceof ArrayItem) { + return true; + } + + $usernameItem = $this->findArrayItem($optionsArray, 'username'); + if ($usernameItem instanceof ArrayItem) { + $usernameItem->value = $userItem->value; + } else { + $optionsArray->items[] = new ArrayItem($userItem->value, new String_('username')); + } + + return true; + } + + /** + * Returns '' for TYPO3_CONF_VARS.SYS.session. and 'options' for its options sub key. + */ + private function resolveSessionConfigurationPath(ArrayDimFetch $arrayDimFetch): ?string + { + $path = []; + $currentNode = $arrayDimFetch; + + while ($currentNode instanceof ArrayDimFetch) { + if (! $currentNode->dim instanceof String_) { + return null; + } + + $path[] = $currentNode->dim->value; + $currentNode = $currentNode->var; + } + + if (! $this->isName($currentNode, 'GLOBALS')) { + return null; + } + + $path = array_reverse($path); + $pathLength = count($path); + if ($pathLength !== 4 && $pathLength !== 5) { + return null; + } + + if ($path[0] !== 'TYPO3_CONF_VARS' || $path[1] !== 'SYS' || $path[2] !== 'session') { + return null; + } + + if (! in_array($path[3], ['BE', 'FE'], true)) { + return null; + } + + if ($pathLength === 4) { + return ''; + } + + return $path[4] === 'options' ? 'options' : null; + } + + private function findArrayItem(Array_ $array, string $key): ?ArrayItem + { + foreach ($array->items as $item) { + if ($item instanceof ArrayItem && $item->key instanceof String_ && $item->key->value === $key) { + return $item; + } + } + + return null; + } +} diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/backend_session.php.inc b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/backend_session.php.inc new file mode 100644 index 000000000..ca8ba98c1 --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/backend_session.php.inc @@ -0,0 +1,39 @@ + RedisSessionBackend::class, + 'options' => [ + 'database' => 0, + 'hostname' => 'redis', + 'port' => 6379, + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], + ], +]; + +?> +----- + RedisSessionBackend::class, + 'options' => [ + 'database' => 0, + 'hostname' => 'redis', + 'port' => 6379, + 'password' => 'secret', + 'username' => 'redis', + ], +]; + +?> diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/existing_username.php.inc b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/existing_username.php.inc new file mode 100644 index 000000000..1240850bb --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/existing_username.php.inc @@ -0,0 +1,34 @@ + RedisSessionBackend::class, + 'options' => [ + 'username' => 'redis', + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], + ], +]; + +?> +----- + RedisSessionBackend::class, + 'options' => [ + 'username' => 'redis', + 'password' => 'secret', + ], +]; + +?> diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/frontend_session_options.php.inc b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/frontend_session_options.php.inc new file mode 100644 index 000000000..c442c22af --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/frontend_session_options.php.inc @@ -0,0 +1,39 @@ + 'redis', + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['options'] = [ + 'password' => [ + 'pass' => 'secret', + ], +]; + +?> +----- + 'redis', + 'password' => 'secret', + 'username' => 'redis', +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['options'] = [ + 'password' => 'secret', +]; + +?> diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_already_migrated.php.inc b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_already_migrated.php.inc new file mode 100644 index 000000000..ce3bc4cca --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_already_migrated.php.inc @@ -0,0 +1,23 @@ + RedisSessionBackend::class, + 'options' => [ + 'username' => 'redis', + 'password' => 'secret', + ], +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['options'] = [ + 'password' => 'secret', +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['options'] = [ + 'password' => [ + 'user' => 'redis', + ], +]; + +?> diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_unrelated_paths.php.inc b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_unrelated_paths.php.inc new file mode 100644 index 000000000..444957372 --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/Fixture/skip_unrelated_paths.php.inc @@ -0,0 +1,34 @@ + [ + 'user' => 'redis', + 'pass' => 'secret', + ], +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['foo'] = [ + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['options']['password'] = [ + 'user' => 'redis', + 'pass' => 'secret', +]; +$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['BE']['options'] = [ + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], +]; +$config['TYPO3_CONF_VARS']['SYS']['session']['BE']['options'] = [ + 'password' => [ + 'user' => 'redis', + 'pass' => 'secret', + ], +]; + +?> diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRectorTest.php b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRectorTest.php new file mode 100644 index 000000000..1b481b707 --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRectorTest.php @@ -0,0 +1,31 @@ +doTestFile($filePath); + } + + /** + * @return \Iterator> + */ + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/config/configured_rule.php b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/config/configured_rule.php new file mode 100644 index 000000000..22830813b --- /dev/null +++ b/tests/Rector/v14/v2/MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../config/config_test.php'); + $rectorConfig->rule(MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector::class); +};