Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/v14/typo3-142.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
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');
$rectorConfig->rule(MigrateAssetCollectorUseNonceOptionToCspRector::class);
$rectorConfig->rule(MigrateTcaOptionAllowedRecordTypesForPageTypesRector::class);
$rectorConfig->rule(MigrateFormEngineAdditionalHiddenFieldsRector::class);
$rectorConfig->rule(AddRequestArgumentToPageRendererMethodsRector::class);
$rectorConfig->rule(MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO314\v2;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\Contract\DocumentedRuleInterface;
use Rector\RuleDoc\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/14.2/Deprecation-107802-DeprecateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackend.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRectorTest
*/
final class MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector extends AbstractRector implements DocumentedRuleInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate usage of array in password for authentication in Redis session backend', [
new CodeSample(
<<<'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' => [
'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<class-string<Node>>
*/
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.<BE|FE> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
'backend' => RedisSessionBackend::class,
'options' => [
'database' => 0,
'hostname' => 'redis',
'port' => 6379,
'password' => [
'user' => 'redis',
'pass' => 'secret',
],
],
];

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
'backend' => RedisSessionBackend::class,
'options' => [
'database' => 0,
'hostname' => 'redis',
'port' => 6379,
'password' => 'secret',
'username' => 'redis',
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
'backend' => RedisSessionBackend::class,
'options' => [
'username' => 'redis',
'password' => [
'user' => 'redis',
'pass' => 'secret',
],
],
];

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
'backend' => RedisSessionBackend::class,
'options' => [
'username' => 'redis',
'password' => 'secret',
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['backend'] = RedisSessionBackend::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['options'] = [
'hostname' => 'redis',
'password' => [
'user' => 'redis',
'pass' => 'secret',
],
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['options'] = [
'password' => [
'pass' => 'secret',
],
];

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['backend'] = RedisSessionBackend::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['FE']['options'] = [
'hostname' => 'redis',
'password' => 'secret',
'username' => 'redis',
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE']['options'] = [
'password' => 'secret',
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

use TYPO3\CMS\Core\Session\Backend\RedisSessionBackend;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
'backend' => 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',
],
];

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v2\MigrateUsageOfArrayInPasswordForAuthenticationInRedisSessionBackendRector\Fixture;

$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['XY']['options'] = [
'password' => [
'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',
],
];

?>
Loading