-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector
Resolves: #3545
- Loading branch information
1 parent
c4796ce
commit ee9669b
Showing
19 changed files
with
378 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/Rector/v12/v0/flexform/MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v12\v0\flexform; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use DOMNodeList; | ||
use DOMXPath; | ||
use Ssch\TYPO3Rector\Contract\FileProcessor\FlexForms\Rector\FlexFormRectorInterface; | ||
use Ssch\TYPO3Rector\Helper\ArrayUtility; | ||
use Ssch\TYPO3Rector\Helper\FlexFormHelperTrait; | ||
use Ssch\TYPO3Rector\Helper\StringUtility; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Feature-97159-NewTCATypeLink.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\flexform\MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector\MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRectorTest | ||
*/ | ||
final class MigratePasswordAndSaltedPasswordToPasswordTypeFlexFormRector implements FlexFormRectorInterface | ||
{ | ||
use FlexFormHelperTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const PASSWORD = 'password'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private const SALTED_PASSWORD = 'saltedPassword'; | ||
|
||
private bool $domDocumentHasBeenChanged = false; | ||
|
||
public function transform(DOMDocument $domDocument): bool | ||
{ | ||
$xpath = new DOMXPath($domDocument); | ||
|
||
/** @var DOMNodeList<DOMElement> $elements */ | ||
$elements = $xpath->query('//config'); | ||
|
||
if ($elements->count() === 0) { | ||
return false; | ||
} | ||
|
||
foreach ($elements as $element) { | ||
$this->refactorColumn($domDocument, $element); | ||
} | ||
|
||
return $this->domDocumentHasBeenChanged; | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Migrate password and salted password to password type', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
<T3DataStructure> | ||
<ROOT> | ||
<sheetTitle>aTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<password_field> | ||
<label>Password</label> | ||
<config> | ||
<type>input</type> | ||
<eval>trim,password,saltedPassword</eval> | ||
</config> | ||
</password_field> | ||
<another_password_field> | ||
<label>Password</label> | ||
<config> | ||
<type>input</type> | ||
<eval>trim,password</eval> | ||
</config> | ||
</another_password_field> | ||
</el> | ||
</ROOT> | ||
</T3DataStructure> | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
<T3DataStructure> | ||
<ROOT> | ||
<sheetTitle>aTitle</sheetTitle> | ||
<type>array</type> | ||
<el> | ||
<password_field> | ||
<label>Password</label> | ||
<config> | ||
<type>password</type> | ||
</config> | ||
</password_field> | ||
<another_password_field> | ||
<label>Password</label> | ||
<config> | ||
<type>password</type> | ||
<hashed>false</hashed> | ||
</config> | ||
</another_password_field> | ||
</el> | ||
</ROOT> | ||
</T3DataStructure> | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
private function refactorColumn(DOMDocument $domDocument, ?DOMElement $configElement): void | ||
{ | ||
if (! $configElement instanceof DOMElement) { | ||
return; | ||
} | ||
|
||
if (! $this->isConfigType($configElement, 'input')) { | ||
return; | ||
} | ||
|
||
if (! $this->hasKey($configElement, 'eval')) { | ||
return; | ||
} | ||
|
||
$evalDomElement = $this->extractDomElementByKey($configElement, 'eval'); | ||
if (! $evalDomElement instanceof DOMElement) { | ||
return; | ||
} | ||
|
||
$evalListValue = $evalDomElement->nodeValue; | ||
if (! is_string($evalListValue)) { | ||
return; | ||
} | ||
|
||
if (! StringUtility::inList($evalListValue, self::PASSWORD) | ||
&& ! StringUtility::inList($evalListValue, self::SALTED_PASSWORD) | ||
) { | ||
return; | ||
} | ||
|
||
// Set the TCA type to "password" | ||
$this->changeTcaType($domDocument, $configElement, self::PASSWORD); | ||
|
||
// Remove 'max' and 'search' config | ||
$this->removeChildElementFromDomElementByKey($configElement, 'max'); | ||
$this->removeChildElementFromDomElementByKey($configElement, 'search'); | ||
|
||
$evalList = ArrayUtility::trimExplode(',', $evalListValue, true); | ||
|
||
// Disable password hashing, if eval=password is used standalone | ||
if (in_array('password', $evalList, true) && ! in_array('saltedPassword', $evalList, true)) { | ||
$configElement->appendChild($domDocument->createElement('hashed', '0')); | ||
} | ||
|
||
if (in_array('null', $evalList, true)) { | ||
// Set "eval" to "null", since it's currently defined and the only allowed "eval" for type=password | ||
$evalDomElement->nodeValue = ''; | ||
$evalDomElement->appendChild($domDocument->createTextNode('null')); | ||
} elseif ($evalDomElement->parentNode instanceof DOMElement) { | ||
// 'eval' is empty, remove whole configuration | ||
$evalDomElement->parentNode->removeChild($evalDomElement); | ||
} | ||
|
||
$this->domDocumentHasBeenChanged = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.