-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Added NotUsernameValidator
Signed-off-by: Torben Hansen <derhansen@gmail.com>
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Extension "add_pwd_policy" for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Derhansen\AddPwdPolicy\PasswordPolicy\Validator; | ||
|
||
use TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator; | ||
use TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData; | ||
|
||
class NotUsernameValidator extends AbstractPasswordValidator | ||
{ | ||
private const IDENTIFIER = 'notUsername'; | ||
|
||
public function validate(string $password, ?ContextData $contextData = null): bool | ||
{ | ||
$lang = $this->getLanguageService(); | ||
|
||
if (!$contextData) { | ||
return true; | ||
} | ||
|
||
$usernameValues = []; | ||
if ($contextData->getNewUsername() !== '') { | ||
$usernameValues[] = strtolower($contextData->getNewUsername()); | ||
} | ||
|
||
if ($contextData->getData('currentUsername') !== '') { | ||
$usernameValues[] = strtolower($contextData->getData('currentUsername')); | ||
} | ||
|
||
$isValid = true; | ||
|
||
foreach ($usernameValues as $usernameValue) { | ||
if (str_contains(strtolower($password), $usernameValue)) { | ||
$this->addErrorMessage( | ||
self::IDENTIFIER, | ||
$lang->sL('LLL:EXT:add_pwd_policy/Resources/Private/Language/locallang.xlf:error.notUsername') | ||
); | ||
|
||
$isValid = false; | ||
break; | ||
} | ||
} | ||
|
||
return $isValid; | ||
} | ||
|
||
public function initializeRequirements(): void | ||
{ | ||
$this->addRequirement( | ||
self::IDENTIFIER, | ||
$this->getLanguageService()->sL('LLL:EXT:add_pwd_policy/Resources/Private/Language/locallang.xlf:requirement.notUsername') | ||
); | ||
} | ||
} |
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