Skip to content

Commit

Permalink
[FEATURE] Added NotUsernameValidator
Browse files Browse the repository at this point in the history
Signed-off-by: Torben Hansen <derhansen@gmail.com>
  • Loading branch information
derhansen committed Mar 25, 2023
1 parent 45ba2f3 commit f9c6b90
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Classes/PasswordPolicy/Validator/NotUsernameValidator.php
@@ -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') !== '') {

Check failure on line 34 in Classes/PasswordPolicy/Validator/NotUsernameValidator.php

View workflow job for this annotation

GitHub Actions / build (8.1, 0)

Call to an undefined method TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData::getData().
$usernameValues[] = strtolower($contextData->getData('currentUsername'));

Check failure on line 35 in Classes/PasswordPolicy/Validator/NotUsernameValidator.php

View workflow job for this annotation

GitHub Actions / build (8.1, 0)

Call to an undefined method TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData::getData().
}

$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')
);
}
}
6 changes: 6 additions & 0 deletions Resources/Private/Language/locallang.xlf
Expand Up @@ -9,6 +9,12 @@
<trans-unit id="error.pwnedPassword" resname="error.pwnedPassword">
<source>This password appeared in a data breach and is found %d times in the haveibeenpwned.com database. Please choose a different password.</source>
</trans-unit>
<trans-unit id="requirement.notUsername" resname="requirement.notUsername">
<source>Must not contain the username</source>
</trans-unit>
<trans-unit id="error.notUsername" resname="error.notUsername">
<source>The username must not be used in the password.</source>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit f9c6b90

Please sign in to comment.